A unit fraction is a fraction whose numerator is 1. The unit fraction whose denominator is 2 to 10 is expressed in decimal as follows.
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
0.1 (6) is the number 0.166666 ... and has a one-digit recurring node. A 1/7 recurring node has six digits.
Find d such that the recurring node of the decimal part is the longest in 1 / d where d <1000. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2026
(1) The length of the recurring node in the decimal part of 1 / d is the point where d is the minimum n that divides 10 ^ n-1. (2) Factors such as 2, 5 do not contribute to the length of the circular theory of the decimal part. I wrote the following code on the premise of the above two points (see supplement). Since the three points of get_prime_boolean and get_prime_listm get_primes are stored in mymath, you may refer to them.
def get_prime_boolean(max):
bool = [False,False]+[True]*(max-1)
bool[4::2] = [False] * (len(bool[4::2]))
p = 3
p_max = int(math.sqrt(max))+1
while p<=p_max:
if bool[p]:
bool[p*2::p] = [False] * (len(bool[p*2::p]))
p+=2
return bool
def get_prime_list(bool):
length = len(bool)
return [i for i in range(2,length) if bool[i]]
def get_primes(max):
bool = get_prime_boolean(max)
list = get_prime_list(bool)
return {'bool':bool,'list':list}
def pe26():
MAX = 1000
seq = range(MAX)
ans = seq[3::10] + seq[7::10] + seq[9::10] + seq[11::10]
t = 9
while len(ans) > 1:
i = 0
while i < len(ans):
if (t%ans[i]) == 0:
ans.pop(i)
else:
i += 1
t = t*10 + 9
print ans[0]
import math
print math.log(t,10)
pe26()
Recommended Posts