-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; }
"you can not use conditional operators, cycles, bitwise operations, functions and methods" I assume this includes builtins to check if
b
can dividea
? 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
– Draco18s no longer trusts SE – 2019-07-02T16:19:54.320=
(check equality: push 0 or 1), the?
(skipn
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. theJ
command)?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 ofx
? CertainlyMath.sign(x)
does, no argument there). – Draco18s no longer trusts SE – 2019-07-02T17:01:16.120Well 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