forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol64.py
40 lines (32 loc) · 733 Bytes
/
sol64.py
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
31
32
33
34
35
36
37
38
39
40
#import time
import time
# square root function
from math import sqrt
# time at the start of program execution
start = time.time()
# function to calculate the continued fraction
def cf(n):
mn = 0.0
dn = 1.0
a0 = int(sqrt(n))
an = int(sqrt(n))
period = 0
if a0 != sqrt(n):
while an != 2*a0:
mn = dn*an - mn
dn = (n - mn**2)/dn
an = int((a0 + mn)/dn)
period += 1
return period
# counter
counter = 0
# for loop from 0 to 10000
for i in xrange(10000):
if cf(i) % 2 != 0:
counter += 1
# number of instances
print counter
# time at the end of program execution
end = time.time()
# total time taken to run the program
print end - start