![]() |
|
Syntactic Confectionery Delight | |
PerlMonks |
Determining if a rational number terminatesby blackle (Beadle) |
on Nov 29, 2012 at 15:56 UTC ( [id://1006283]=obfuscated: print w/replies, xml ) | Need Help?? |
Hello all. A friend of mine had participated in a programming competition for his school. He did pretty well, but he couldn't get one question. The question was, given the numerator and denominator of a rational number, determine if the decimal expansion terminates. This is to say, if I gave you 1/3, you would say it doesn't because the expansion is "0.3333..." Likewise, if I gave you 1/10, you would say it does terminate because the expansion is "0.1" My friend had tried to solve this problem with string operations, but I found a better way. According to the Wikipedia article for repeating decimals, rational numbers that terminate are in the form a/b -> b = 2^c*5^d where c and d are natural numbers. Given this identity, I developed the following (obfu) one-liner:
Given $ARGV[0] = a; $ARGV[1] = b the program will say "Y" if the decimal terminates and "N" if it doesn't. The spoiler below reveals how the program works: <Reveal this spoiler or all in this thread>
Back to
Obfuscated Code
|
|