Skip to content

Commit

Permalink
naive 64 - floating point problem
Browse files Browse the repository at this point in the history
  • Loading branch information
3kwa committed Jan 25, 2012
1 parent ca6f872 commit ae1649d
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions 064.py
@@ -0,0 +1,39 @@
from math import sqrt


def step(value):
return int(value), ( value + int(value) ) / (value ** 2 - int(value) ** 2)

def recursion(value, seen=[]):
integer, new = step(value)
# floating point precision an issue must truncate/round
# 3 was chosen empirically 10 ** -4 precision in assessing periodicity
truncated = round(new, 1)
if truncated in seen:
return len(seen)
return recursion(new, seen + [truncated])

def period(N):
"""
>>> [period(n) for n in (2, 3, 5, 6, 7, 8, 10, 11, 12, 13)]
[1, 2, 1, 2, 4, 2, 1, 2, 2, 5]
>>> period(23)
4
"""
value = sqrt(N)
if int(value)==value:
return 0
return recursion(sqrt(N))

def solution(max):
"""
>>> solution(13)
4
"""
return sum(1 for n in range(2, max + 1) if period(n) % 2 != 0)

if __name__ == '__main__':
import doctest
doctest.testmod()

print solution(10000)

0 comments on commit ae1649d

Please sign in to comment.