Author | Message | Time |
---|---|---|
Yoni | I've been sitting here, studying for yet another one of my finals, and my mind drifted off to math, as it often does. I've come up with a riddle. Given are the n'th power of 2: 2^n, and the decimal representation of an integer: X. My riddle is: How do you check whether X is divisible by 2^n? The restriction to make this interesting is that you can't perform any division or modulo (so you can't just calculate and check whether X / 2^n is an integer). You may use addition, subtraction, multiplication. [Edit: Grammar] | May 29, 2004, 8:11 PM |
Adron | while(X >= pow(2, n)) X = X - pow(2, n); if(X == 0) printf("It was divisible!\n"); edit: removed X = 0 and 2**n pseudocode, to avoid complaints | May 29, 2004, 9:25 PM |
Adron | Alternative: Calculate X * 5**n and see if the number ends in n zeroes. If it does, you win. | May 29, 2004, 9:30 PM |
Adron | Finally, after solving it with subtraction and multiplication, solution with addition: for( Y = 0; Y < X; Y += pow(2, n)); if(Y == X) printf("Divisible!\n"); else printf("Not divisible!\n"); | May 29, 2004, 9:33 PM |
Yoni | Your 1st and 3rd posts were an alternative modulo and weren't my point. Your 2nd post is exactly what I meant. Good job ;) (Somehow, I knew it'd be you who would solve it first / at all, but oh well...) | May 29, 2004, 11:44 PM |
Adron | [quote author=Yoni link=board=36;threadid=7029;start=0#msg62697 date=1085874268] Your 1st and 3rd posts were an alternative modulo and weren't my point. Your 2nd post is exactly what I meant. Good job ;) (Somehow, I knew it'd be you who would solve it first / at all, but oh well...) [/quote] Actually, isn't #2 just an alternative modulo too...? I turn the question of mod2 into a question of mod10? I took the hint for that from the word "decimal" in your problem specification. Good one ;) | May 30, 2004, 10:08 AM |
Yoni | Yes, it's mod 10^n, hence I said "decimal representation" to have that implicitly allowed. | May 31, 2004, 12:22 AM |
Adron | Hmm, can't see how the representation you put it in makes any difference ... if the number was in binary, would shifting it right and checking whether you shift out zeroes or ones not be a way of doing a division? Seems like one to me... Also, when calculating X * 5^n, you'll most probably be turning it into binary in a calculator or computer anyway ;) | May 31, 2004, 1:19 AM |
Yoni | May 31, 2004, 1:31 AM | |
Adron | Yes, but what do you call the operation "count the zeros"? | May 31, 2004, 1:32 AM |
Yoni | The "cheating unless I say so" operation. | May 31, 2004, 1:33 AM |
CrAzY | Just a little tip... You know Multiplication is the opisite of Divion... Just flip the devisor and times that. EX: 8 / 2 = 4 Then 8 * .5 = 4 Should help out a bit.. | June 4, 2004, 6:59 PM |
Yoni | True, but the point was to use only integers, so you can't multiply by 0.5. Else it'd be too obvious. I did at first say something like "oh, and you can't multiply by 0.5^n" in the first post, but that's too much of a clue so I edited it out. Consider the difference between 0.5^n and 5^n. The first is also equal to 5^n/10^n. The second leaves the zeroes intact. Yes? | June 5, 2004, 12:01 PM |