-
Notifications
You must be signed in to change notification settings - Fork 0
/
question80.py
53 lines (30 loc) · 1.21 KB
/
question80.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
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Square root digital expansion
#Problem 80
#It is well known that if the square root of a natural number is not an integer, then it is irrational. The decimal expansion of such square roots is infinite without any repeating pattern at all.
#The square root of two is 1.41421356237309504880..., and the digital sum of the first one hundred decimal digits is 475.
#For the first one hundred natural numbers, find the total of the digital sums of the first one hundred decimal digits for all the irrational square roots.
#idea - make list of all squares, then find find decimal sums of others
import time
from decimal import *
import math
getcontext().prec = 102
s1=time.time()
squares = [i*i for i in range(2,101)]
#print(squares)
res = 0.0
for j in range(2,101):
if j not in squares:
#print(j)
temp = Decimal(j).sqrt()
temp2 = math.floor(temp)
temp = Decimal(temp) - temp2
#print(str(temp)[90:102])
#print(len(str(Decimal(temp))[2:102]))
res+= sum([int(char) for char in str(temp)[2:101]])
res+= temp2
print(res)
print("{}s".format(time.time() - s1))
# 40886.0 YAY
# 0.002087831497192383s