Created
May 18, 2012 23:58
-
-
Save mrklein/2728237 to your computer and use it in GitHub Desktop.
Project Euler #64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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