-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path064.py
More file actions
30 lines (24 loc) · 696 Bytes
/
064.py
File metadata and controls
30 lines (24 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python
import time
from decimal import *
start = time.clock()
getcontext().prec = 300
def continued_fraction(n):
a = Decimal(n).sqrt()
frst = int(a)
continued_fraction = []
for i in range(1,300):
continued_fraction += [int(a)]
b = a - int(a)
a = Decimal(1)/b
if int(a) == 2*frst: #then we're done
return i
print "Error: no expansion for {} found".format(n)
return 1
tot = 0
for j in range(2,10000):
if not int(j**0.5+0.5)**2 == j: #don't want any rational square roots
if continued_fraction(j) % 2 == 1: #if period length is odd
tot += 1
print "Number of odd square roots: {}".format(tot)
print "Time : {}".format(time.clock()-start)