Home Project Euler Teaching Projects Gallery

Problem 26: Reciprocal cycles

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

 
                    1/2	= 	0.5
                    1/3	= 	0.(3)
                    1/4	= 	0.25
                    1/5	= 	0.2
                    1/6	= 	0.1(6)
                    1/7	= 	0.(142857)
                    1/8	= 	0.125
                    1/9	= 	0.(1)
                    1/10 =      0.1'
            

Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.

Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.


Algorthim:

Interesting problem. Brute force is obvious. But, with a little big of math, this problem is rather simple. In order to check if we have created recurring cycle of a fraction, for example, how do we know to stop when 1/7 = 0.1428571? How do we know when we should stop at 7? Why not at 1? Sure, 1 does show up at the beginning, but it may lead to 142857123?

Let k be the length of the recurring cycle, and let n be the denomiator, then, $$ 10^k * \frac{1}{n} $$ This represents a rational number with integer portion of the number as the repeating part. Then, if we subtract 1/n, we get the repeating portion! $$ 10^k * \frac{1}{n} - \frac{1}{n} = g $$ For some g, where g is an integer. Let's multiply by n: $$ 10^k - 1 = gn $$ Let's (mod n): $$ 10^k - 1 \equiv0\pmod{n}$$ So, all we have to check is when is 10^k - 1 divisibl by n!

How about cycles that have non-repeating portion? For example, 0.166666? We can take care of the non-repeats by getting rid of any factors of two's or five's in the number. So, we ahve two function dedicated to this: Two(), Five()

Code:

Result:

Answer: 983
Runtime: 144 ms