Skip to content

Instantly share code, notes, and snippets.

@mrklein
Created May 18, 2012 23:58
Show Gist options
  • Save mrklein/2728237 to your computer and use it in GitHub Desktop.
Save mrklein/2728237 to your computer and use it in GitHub Desktop.
Project Euler #64
#!/usr/bin/env python
def period(n):
a_0 = int(n ** 0.5)
if a_0 * a_0 == n:
return 0
b, b_0 = a_0, a_0
c, c_0 = n - a_0*a_0, n - a_0*a_0
res = 0
while True:
a = (a_0 + b) / c
b = a * c - b
c = (n - b * b) / c
res += 1
if b == b_0 and c == c_0:
break
return res
if __name__ == '__main__':
print sum(
map(lambda n: period(n) % 2,
xrange(2, 10001)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment