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


Where 0.1(6) means 0.166666…, and has a 1digit recurring cycle. It can be seen that ^{1}/_{7} has a 6digit recurring cycle.
Find the value of d < 1000 for which ^{1}/_{d} contains the longest recurring cycle in its decimal fraction part.
Solution
This problem and the HackerRank version are looking for d < N, NOT d ≤ N. This specific problem has N fixed at 1000.
After much trial and error with brute force testing we found ourselves at this article Full Reptend Prime. It explains that a full reptend prime is a prime p for which 1/p will have the longest recurring cycle of p1 digits. It further explains a prime p is full reptend if
10^{k} ≡ 1 (mod p)
for k = p1 and no other k less than this. Here are some examples to help illustrate this concept:
(10^{1}, 10^{2}, 10^{3}, 10^{4}, 10^{5}, 10^{6}) ≡ (3, 2, 6, 4, 5, 1) (mod 7)
7 is a full reptend prime because 1 occurs only at the last position
(10^{1}, 10^{2}, 10^{3}, …, 10^{10}) ≡ (10, 1, 10, 1, 10, 1, 10, 1, 10, 1) (mod 11) (10^{1}, 10^{2}, 10^{3}, …, 10^{12}) ≡ (10, 9, 12, 3, 4, 1, 10, 9, 12, 3, 4, 1) (mod 13)
11 and 13 are NOT full reptend primes because 1 occurs before the last positions.
Summary
So, this problem can answered by finding the first full reptend prime number below a limit. As an example, for d < 100 the first reptend prime less than 100 is 97. Therefore, 1/97 will produce the longest recurring cycle in its decimal fraction part (96 digits) for all unit fractions with denominators less than 100.
For N < 8 edge cases
The algorithm works for all N greater than or equal to 8. When the N is less than 8 we return 3 because that is the smallest d that repeats with the longest recurring cycle. To illustrate why notice that 1/2 (0.5), 1/4 (0.25), 1/5 (0.2) never repeat; their cycle is 0. 1/3 (0.(3)), 1/6 (0.1(6)) both repeat with a cycle of 1 of which 3 is the smallest value of d. 3 is not a reptend prime.
HackerRank version
HackerRank Project Euler 26 increases the limit to d < 10000 and runs 1000 test cases. Same solution solves all the test cases.
Python 2.7 Source
Last Word
 Reference: The OnLine Encyclopedia of Integer Sequences (OEIS) A001913: Full reptend primes: primes with primitive root 10.
 for d < 2000: 1979
for d < 2500: 2473
for d < 10,000: 9967
for d < 25,000: 24989
for d < 100,000: 99989
for d < 1,000,000: 999983
A change from:
while (pow(10, c)  1) % p != 0:
to
while pow(10, c, p) != 1:
was a 400 times speed improvement.