Report divisibility with arithmetic integer operations only

-6

Given two numbers, the first is greater than the second, check whether the first number is divisible by the second without a remainder. If divisible, answer "yes"(or any other string), otherwise "no"(or any other string you like).

Conditions: You cannot use conditional operators, loops, arrays, bitwise operations, functions and methods. You can use variables, all arithmetic integer operations (that is, integer division), even the operation of taking the remainder of a division.

Numbers can range from 1 to 1,000,000. Maximum running time is 1 second.

You can of course write in any language, but here is my non-golfed solution in C++:

int main() {
  int a,b; cin >> a >> b;
  int c = a%b;
  int n = (c+1-(c-1)((2(c-1)+1)%2))/2;
  int y = 121, e = 101, s = 115;

  cout << (char)(y-11*n) << (char)(e+10*n) << (char)(s-83*n) << endl;
}

Mouvre

Posted 2019-07-02T15:32:47.010

Reputation: 91

Question was closed 2019-07-02T17:06:42.310

"you can not use conditional operators, cycles, bitwise operations, functions and methods" I assume this includes builtins to check if b can divide a? And how about array/list indexing? Could I use (in pseudo-code): list = ["no","yes"]; int isDivisible = b can_divide_evenly a; print(list[isDivisible]);? – Kevin Cruijssen – 2019-07-02T15:40:10.187

@KevinCruijssen no arrays – Mouvre – 2019-07-02T15:47:27.233

3Welcome to Code Golf! Especially for a new user, do not post "Do X without Y" questions. Also, why are you restricting the output to "yes" and "no"? Thanks! – MilkyWay90 – 2019-07-02T16:12:17.363

@MilkyWay90 Cause it is strings, it may be any other strings, so just for example. – Mouvre – 2019-07-02T16:14:02.803

@Mouvre You should probably explicitly state that in your question – MilkyWay90 – 2019-07-02T16:14:40.833

2

So for a language where each byte is an instruction, which part is the "conditional"? The part that coerces the boolean, or the part that consumes the boolean? E.g. for this program which part violates the restriction: the = (check equality: push 0 or 1), the ? (skip n instructions), both (i.e. both commands are individually invalid), or the combination of both in sequence (i.e. invalid only if both are used)? What about commands that act differently based on the value of the top-of-stack (e.g. the J command)?

– Draco18s no longer trusts SE – 2019-07-02T16:19:54.320

3Aren't all unary or higher operators conditional, since their behaviour changes based on the value on which they are acting. – Expired Data – 2019-07-02T16:29:26.007

@ExpiredData I don't know about "all" but certainly a lot. (e.g. does x++ operate "differently" depending on the value of x? Certainly Math.sign(x) does, no argument there). – Draco18s no longer trusts SE – 2019-07-02T17:01:16.120

Well x++ is effectively the binary + between x and 1, and it's result is conditional on x, I.e imagine a program which took x and returned x+1, it can conceivably be programmed as a set of n bit if statements where n is the number of bits in your representation of x, or a countable infinity of if statements if the size of x is unbounded @draco18s – Expired Data – 2019-07-02T17:04:28.090

@ExpiredData Ah, I suppose you have a point there. – Draco18s no longer trusts SE – 2019-07-02T17:35:27.617

2

Welcome to the site. We really do recommend the sandbox for new challenges especially for new members. It saves you from getting too many downvotes for a question that could have been improved without downvotes harming your rep :-)

– ElPedro – 2019-07-02T20:25:27.230

Answers

1

05AB1E, 1 byte

Ö

Try it online!

Outputs 1 if divsible 0 if not


Old version with string must be Yes or No

05AB1E, 13 12 10 bytes

Ö'…Ü„nor.D

Ö              // Test to see if they are divisible
 '…Ü           // The string yes
     „no      // the string no
         r     // reverse the stack 
          .D   // push whether they are divisible copies of "yes"
               // Implicitly output the result 

Try it online!

Expired Data

Posted 2019-07-02T15:32:47.010

Reputation: 3 129

“…Ü“ can be '…Ü for -1 and "no" can be „no for -1. – Kevin Cruijssen – 2019-07-02T16:17:35.513

Pretty pointless to ban all those things but not builtins.. >.> Anyway, my first comment is also a tip for future challenges. :) '„… are all three builtins for 1, 2, or 3-char string builtins, or 1, 2, or 3 word dictionary strings. More info can be found in this first section here.

– Kevin Cruijssen – 2019-07-02T16:24:24.317

@KevinCruijssen thanks, I was trying to use that for reference but sometimes it's hard to pick things out! and yeah well this is a terrible challenge anyway, I think it should probably be closed. – Expired Data – 2019-07-02T16:26:35.547

1

Yeah, my tip answer is a bit large these days, because more was added in the process.. And you learn as you go along with it. Even now I sometimes come across builtins which are barely used and I didn't even knew were there.. PS: If you ever have any questions, feel free to ask any of us in the 05AB1E chat.

– Kevin Cruijssen – 2019-07-02T16:28:38.153

3

JavaScript (ES6), 17 bytes

Takes input as (a)(b). Returns "Y" for yes or "undefined" (as a string) for no.

a=>b=>"Y"[a%b]+''

Try it online!


JavaScript (ES6), 50 bytes

Takes input as (a)(b). Returns "yes" or "no", as per the original rules.

a=>b=>(22724-21872*((2*(a%b-1)+1)%2)).toString(36)

Try it online!

Arnauld

Posted 2019-07-02T15:32:47.010

Reputation: 111 334

1

APL (Dyalog Unicode), 17 3 bytesSBCS

Anonymous tacit infix function. Takes lesser and greater numbers as left and right arguments. Prints the string "0" if divisible and "1" if not.

1⌊|

Try it online!

| division remainder when dividing right argument by left argument (gives 0 for divisible or strictly positive integer for non-divisible)

1⌊ the minimum of that and 1 (gives 0 for divisible or 1 for non-divisible)

Adám

Posted 2019-07-02T15:32:47.010

Reputation: 37 779

Of course, this depends on if = is considered the "conditional" in the challenge specification, or whether the if(...) part is. – Draco18s no longer trusts SE – 2019-07-02T16:50:00.287

@Draco18s Good point. Fixed. – Adám – 2019-07-02T17:22:03.583

0

Runic Enchantments, 6 bytes

0ii%p@

Try it online!

Outputs 1 if divisible 0 if not.

Determines 0/1-ness by raising 0 to the power of the remainder, which returns 0 for all x except 0 (due to the indeterminate nature of 00 having been decided to be 1).

Version with string output

Runic Enchantments, 20 bytes

10ii%p-4´36*6X+d-E@

Try it online!

Utilizes the word dictionary to convert from a 0/1 value to the strings "yes" and "no" by multiplying the result by 436 and adding 47 (ok, it adds 60 and subtracts 13; the byte cost is ironically the same either way: 4X7++ vs. 6X+d- vs. 4´7+).

May be invalid due to using an array? There is certainly no array in the submission code but rather in the the interpreter due to how E is executed when the top of the stack is not a string.

Draco18s no longer trusts SE

Posted 2019-07-02T15:32:47.010

Reputation: 3 053

0

Befunge-98 (FBBI), 6 bytes

%1\k0.

Arguments should be pushed onto the stack, and requires the current IP delta to be (1,0). Prints 1 if divisible and 0 if not.

Try it online! Link includes I/O boilerplate.

attinat

Posted 2019-07-02T15:32:47.010

Reputation: 3 495