From 7a9179152784794e01e27a44fec9c8fe3b025249 Mon Sep 17 00:00:00 2001 From: KiranAkadas Date: Mon, 5 Oct 2020 00:14:20 +0530 Subject: [PATCH 1/8] Create sol.py --- project_euler/problem_80/sol.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 project_euler/problem_80/sol.py diff --git a/project_euler/problem_80/sol.py b/project_euler/problem_80/sol.py new file mode 100644 index 000000000000..3b377abcff48 --- /dev/null +++ b/project_euler/problem_80/sol.py @@ -0,0 +1,28 @@ +import math +import decimal +from math import floor + +def solution(): + n=100 + p=100 + tot=0 + + for i in range(1,n+1): + sm=0 + decimal.getcontext().prec=p+10 + fv=decimal.Decimal(i).sqrt() + if fv-floor(fv)==0: + continue + else: + fv=str(fv).replace('.','') + fv=fv[0:p] + fv=int(fv) + j=0 + while(fv!=0): + sm=sm+(fv%10) + fv=fv//10 + tot+=sm + return tot + +res=solution() +print(res) From 2daaee991959093fe214f8e2bf1272676ff67731 Mon Sep 17 00:00:00 2001 From: KiranAkadas Date: Mon, 5 Oct 2020 00:30:26 +0530 Subject: [PATCH 2/8] Update sol.py --- project_euler/problem_80/sol.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_80/sol.py b/project_euler/problem_80/sol.py index 3b377abcff48..331607cc6450 100644 --- a/project_euler/problem_80/sol.py +++ b/project_euler/problem_80/sol.py @@ -2,7 +2,7 @@ import decimal from math import floor -def solution(): +def solution() -> int: n=100 p=100 tot=0 @@ -24,5 +24,5 @@ def solution(): tot+=sm return tot -res=solution() -print(res) +import doctest +doctest.testmod() From 7081267353f31afec74f1955f5352b0fcbf5516d Mon Sep 17 00:00:00 2001 From: KiranAkadas Date: Mon, 5 Oct 2020 00:31:15 +0530 Subject: [PATCH 3/8] Update sol.py --- project_euler/problem_80/sol.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_80/sol.py b/project_euler/problem_80/sol.py index 331607cc6450..431457d04a3c 100644 --- a/project_euler/problem_80/sol.py +++ b/project_euler/problem_80/sol.py @@ -1,4 +1,3 @@ -import math import decimal from math import floor @@ -17,12 +16,11 @@ def solution() -> int: fv=str(fv).replace('.','') fv=fv[0:p] fv=int(fv) - j=0 while(fv!=0): sm=sm+(fv%10) fv=fv//10 tot+=sm return tot -import doctest -doctest.testmod() +res=solution() +print(res) From 847071e1e6b4337963350c6d374fa6a3736e8f03 Mon Sep 17 00:00:00 2001 From: KiranAkadas Date: Mon, 5 Oct 2020 00:35:32 +0530 Subject: [PATCH 4/8] Update sol.py --- project_euler/problem_80/sol.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/project_euler/problem_80/sol.py b/project_euler/problem_80/sol.py index 431457d04a3c..c65bb4433e55 100644 --- a/project_euler/problem_80/sol.py +++ b/project_euler/problem_80/sol.py @@ -2,25 +2,25 @@ from math import floor def solution() -> int: - n=100 - p=100 - tot=0 + n = 100 + p = 100 + tot = 0 - for i in range(1,n+1): - sm=0 - decimal.getcontext().prec=p+10 - fv=decimal.Decimal(i).sqrt() - if fv-floor(fv)==0: + for i in range(1, n + 1): + sm = 0 + decimal.getcontext().prec = p + 10 + fv = decimal.Decimal(i).sqrt() + if fv - floor(fv) == 0: continue else: - fv=str(fv).replace('.','') - fv=fv[0:p] - fv=int(fv) - while(fv!=0): - sm=sm+(fv%10) - fv=fv//10 - tot+=sm + fv = str(fv).replace(".", "") + fv = fv[0:p] + fv = int(fv) + while fv != 0: + sm = sm + (fv%10) + fv = fv // 10 + tot += sm return tot -res=solution() +res = solution() print(res) From 31f6b30ba176aa02ca5db2c82e6194ec7bbbf384 Mon Sep 17 00:00:00 2001 From: KiranAkadas Date: Mon, 5 Oct 2020 00:38:46 +0530 Subject: [PATCH 5/8] Update sol.py --- project_euler/problem_80/sol.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_80/sol.py b/project_euler/problem_80/sol.py index c65bb4433e55..10aae5fe21d2 100644 --- a/project_euler/problem_80/sol.py +++ b/project_euler/problem_80/sol.py @@ -1,6 +1,7 @@ import decimal from math import floor + def solution() -> int: n = 100 p = 100 @@ -17,10 +18,11 @@ def solution() -> int: fv = fv[0:p] fv = int(fv) while fv != 0: - sm = sm + (fv%10) + sm = sm + (fv % 10) fv = fv // 10 tot += sm return tot + res = solution() print(res) From 4d89cc30cbc027100d0ad4102063021800c80c23 Mon Sep 17 00:00:00 2001 From: KiranAkadas Date: Mon, 5 Oct 2020 00:52:00 +0530 Subject: [PATCH 6/8] added doctest --- project_euler/problem_80/sol.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_80/sol.py b/project_euler/problem_80/sol.py index 10aae5fe21d2..6f693bfa1455 100644 --- a/project_euler/problem_80/sol.py +++ b/project_euler/problem_80/sol.py @@ -1,8 +1,24 @@ import decimal from math import floor +""" +Square root digital expansion +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. + +>>> solution() +40886 +""" def solution() -> int: + """Return the total of the digital sums of the first one hundred decimal digits for all the irrational square roots. + + >>> solution() + 40886 + """ + n = 100 p = 100 tot = 0 @@ -24,5 +40,6 @@ def solution() -> int: return tot -res = solution() -print(res) +if __name__ == "__main__": + import doctest + doctest.testmod() From 319347e74afbfffc4e9e8b89b17d2f0280c21077 Mon Sep 17 00:00:00 2001 From: KiranAkadas Date: Mon, 5 Oct 2020 00:55:55 +0530 Subject: [PATCH 7/8] Update sol.py --- project_euler/problem_80/sol.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_80/sol.py b/project_euler/problem_80/sol.py index 6f693bfa1455..6b26e87eae87 100644 --- a/project_euler/problem_80/sol.py +++ b/project_euler/problem_80/sol.py @@ -1,24 +1,29 @@ import decimal from math import floor + """ Square root digital expansion -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. +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. +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. +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. >>> solution() 40886 """ + def solution() -> int: """Return the total of the digital sums of the first one hundred decimal digits for all the irrational square roots. >>> solution() 40886 """ - + n = 100 p = 100 tot = 0 @@ -41,5 +46,5 @@ def solution() -> int: if __name__ == "__main__": - import doctest - doctest.testmod() + res=solution() + print(res) From 73cf68052c0bee0e4222aa57d987781b2c822b1b Mon Sep 17 00:00:00 2001 From: KiranAkadas Date: Mon, 5 Oct 2020 00:58:50 +0530 Subject: [PATCH 8/8] Update sol.py --- project_euler/problem_80/sol.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_80/sol.py b/project_euler/problem_80/sol.py index 6b26e87eae87..6b249bc212d0 100644 --- a/project_euler/problem_80/sol.py +++ b/project_euler/problem_80/sol.py @@ -3,14 +3,15 @@ """ Square root digital expansion -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. +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. +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. +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. >>> solution() 40886 @@ -18,7 +19,8 @@ def solution() -> int: - """Return the total of the digital sums of the first one hundred decimal digits for all the irrational square roots. + """Return the total of the digital sums of the first one hundred decimal digits for + all the irrational square roots. >>> solution() 40886 @@ -46,5 +48,5 @@ def solution() -> int: if __name__ == "__main__": - res=solution() + res = solution() print(res)