mirror of
https://github.com/correl/euler.git
synced 2024-11-23 19:19:53 +00:00
Problem 026
This commit is contained in:
parent
2079aa4199
commit
4cf65ca7a0
1 changed files with 52 additions and 28 deletions
76
e026.py
76
e026.py
|
@ -27,47 +27,71 @@ def coprime(a, b):
|
||||||
primes = [p for p in primes_a if p in primes_b]
|
primes = [p for p in primes_a if p in primes_b]
|
||||||
return bool(primes)
|
return bool(primes)
|
||||||
|
|
||||||
def period_length(numerator, denominator):
|
def long_division(dividend, divisor):
|
||||||
|
dividend = str(dividend).replace('.', '')
|
||||||
|
i = -1
|
||||||
|
remainder = 0
|
||||||
|
while True:
|
||||||
|
while remainder < divisor:
|
||||||
|
i += 1
|
||||||
|
remainder *= 10
|
||||||
|
try:
|
||||||
|
remainder += int(dividend[i])
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
if remainder == 0:
|
||||||
|
return
|
||||||
|
digit = str(remainder // divisor)
|
||||||
|
remainder = remainder % divisor
|
||||||
|
yield digit, remainder
|
||||||
|
|
||||||
|
def get_cycle_length(numerator, denominator):
|
||||||
"""Determine the period length of a non-terminating decimal"""
|
"""Determine the period length of a non-terminating decimal"""
|
||||||
|
|
||||||
length = None
|
length = None
|
||||||
numerator = Decimal(numerator)
|
numerator = Decimal(numerator)
|
||||||
denominator = Decimal(denominator)
|
denominator = Decimal(denominator)
|
||||||
if numerator == 1:
|
|
||||||
if denominator == 1:
|
|
||||||
return 1
|
|
||||||
primes = pfactor(int(denominator))
|
|
||||||
|
|
||||||
"""
|
if denominator == 1:
|
||||||
From Wikipedia:
|
return 1
|
||||||
Terminating decimals represent rational numbers of the form
|
|
||||||
k/2^n5^m
|
|
||||||
|
|
||||||
However, a terminating decimal also has a representation as a
|
"""
|
||||||
repeating decimal, obtained by decreasing the final (nonzero)
|
From Wikipedia:
|
||||||
digit by one and appending an infinitely repeating sequence of
|
Terminating decimals represent rational numbers of the form
|
||||||
nines. 1 = 0.999999... and 1.585 = 1.584999999... are two
|
k/2^n5^m
|
||||||
examples of this.
|
|
||||||
"""
|
However, a terminating decimal also has a representation as a
|
||||||
if len([p for p in primes if p not in [2, 5]]) == 0:
|
repeating decimal, obtained by decreasing the final (nonzero)
|
||||||
return 1
|
digit by one and appending an infinitely repeating sequence of
|
||||||
|
nines. 1 = 0.999999... and 1.585 = 1.584999999... are two
|
||||||
|
examples of this.
|
||||||
|
"""
|
||||||
|
if coprime(denominator, 10):
|
||||||
|
return 1
|
||||||
|
|
||||||
|
digits = []
|
||||||
|
for digit in long_division(numerator, denominator):
|
||||||
|
if digit in digits:
|
||||||
|
cycle = digits[digits.index(digit):]
|
||||||
|
return len(cycle)
|
||||||
|
digits.append(digit)
|
||||||
|
|
||||||
if coprime(denominator, 10):
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
raise Exception('Non-reciprocal period detection not yet implemented')
|
raise Exception('Non-reciprocal period detection not yet implemented')
|
||||||
return length
|
return length
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
MAX = 10
|
MAX = 1000
|
||||||
longest_cycle = 0
|
longest_cycle = (0, 0)
|
||||||
for i in xrange((MAX), 1, -1):
|
for i in xrange((MAX), 1, -1):
|
||||||
# The period of 1/k for integer k is always ² k ? 1.
|
# The period of 1/k for integer k is always <= k - 1
|
||||||
if longest_cycle >= i:
|
if longest_cycle[1] >= i:
|
||||||
break
|
break
|
||||||
|
|
||||||
cycle = period_length(1, i)
|
cycle = get_cycle_length(1, i)
|
||||||
fraction = Decimal(1) / Decimal(i)
|
if longest_cycle[1] < cycle:
|
||||||
print '1/{0} = {1} ({2})'.format(i, fraction, cycle), fraction.is_subnormal()
|
longest_cycle = (i, cycle)
|
||||||
|
(i, cycle) = longest_cycle
|
||||||
|
print 'Longest cycle length is', cycle, 'for i =', i
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue