From 5c8d0da171998599a68b2d9b95a129f920152b32 Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Thu, 13 Apr 2023 20:54:16 +0530 Subject: [PATCH 01/19] Added solution for problem 148 in Project Euler --- project_euler/problem_148/__init__.py | 0 project_euler/problem_148/sol1.py | 64 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 project_euler/problem_148/__init__.py create mode 100644 project_euler/problem_148/sol1.py diff --git a/project_euler/problem_148/__init__.py b/project_euler/problem_148/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py new file mode 100644 index 000000000000..3062d0270495 --- /dev/null +++ b/project_euler/problem_148/sol1.py @@ -0,0 +1,64 @@ +""" +Project Euler Prbolem 148 : https://projecteuler.net/problem=148 +Author: Sai Teja Manchi +Problem Statement: +We can easily verify that none of the entries in the first seven rows of Pascal's triangle are divisible by 7: + + 1 + 1 1 + 1 2 1 + 1 3 3 1 + 1 4 6 4 1 + 1 5 10 10 5 1 +1 6 15 20 15 6 1 +However, if we check the first one hundred rows, we will find that only 2361 of the 5050 entries are not divisible by 7. + +Find the number of entries which are not divisible by 7 in the first one billion (109) rows of Pascal's triangle. + +""" + +def solution(n: int = 10**9) -> int: + """ + To evaluate the solution, use solution() + >>> solution(3) + 6 + >>> solution(10) + 40 + >>> solution(100) + 2361 + """ + + #Initializing pascal row and count + pascal_row = [1, 2] + cnt = 6 + + #To keep track of length of the pascal row + l = 2 + + for i in range(3, n): + j = 1 + + #Generating the next pascal row + while j < l: + pascal_row[j-1] = (pascal_row[j - 1] + pascal_row[j]) % 7 + if pascal_row[j - 1] != 0: + cnt += 2 + j += 1 + + #Adding the middle element for even rows + if i % 2 == 0: + pascal_row[-1] = pascal_row[-1] * 2 + l += 1 + if pascal_row[-1] % 7 != 0: + cnt += 1 + else: + del pascal_row[-1] + pascal_row.insert(0, 1) + + #Adding 2 for the Additonal 1's in the new pascal row + cnt += 2 + + return cnt + +if __name__ == "__main__": + print(f"{solution()}") \ No newline at end of file From 7a585740f66a49b549e67f059d0b518135502ade Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Thu, 13 Apr 2023 21:17:38 +0530 Subject: [PATCH 02/19] Added solution for problem 148 in Project Euler --- project_euler/problem_148/sol1.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 3062d0270495..a54568a132f4 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -1,6 +1,8 @@ """ -Project Euler Prbolem 148 : https://projecteuler.net/problem=148 +Project Euler Problem 148 : https://projecteuler.net/problem=148 + Author: Sai Teja Manchi + Problem Statement: We can easily verify that none of the entries in the first seven rows of Pascal's triangle are divisible by 7: @@ -15,6 +17,11 @@ Find the number of entries which are not divisible by 7 in the first one billion (109) rows of Pascal's triangle. +Solution: +We Generate each row in the pascal triangle one-by-one. +Since Pascal's triangle is vertically-symmetric we only need to generate half of the values. +We then count the values which are not divisible by 7. +We only store the remainders(when divided by 7) in the list to reduce memory usage.. """ def solution(n: int = 10**9) -> int: @@ -51,11 +58,12 @@ def solution(n: int = 10**9) -> int: l += 1 if pascal_row[-1] % 7 != 0: cnt += 1 + #Deleting the last element for odd rows since 1 is added at beginning else: del pascal_row[-1] pascal_row.insert(0, 1) - #Adding 2 for the Additonal 1's in the new pascal row + #Adding 2 to the count for the Additonal 1's in the new pascal row cnt += 2 return cnt From 449f3cc78281c7aba56725ae7f5487728de20fed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Apr 2023 16:53:18 +0000 Subject: [PATCH 03/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_148/sol1.py | 32 ++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index a54568a132f4..9d4a5e7ac4a6 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -24,6 +24,7 @@ We only store the remainders(when divided by 7) in the list to reduce memory usage.. """ + def solution(n: int = 10**9) -> int: """ To evaluate the solution, use solution() @@ -34,39 +35,40 @@ def solution(n: int = 10**9) -> int: >>> solution(100) 2361 """ - - #Initializing pascal row and count + + # Initializing pascal row and count pascal_row = [1, 2] cnt = 6 - - #To keep track of length of the pascal row + + # To keep track of length of the pascal row l = 2 - + for i in range(3, n): j = 1 - - #Generating the next pascal row + + # Generating the next pascal row while j < l: - pascal_row[j-1] = (pascal_row[j - 1] + pascal_row[j]) % 7 + pascal_row[j - 1] = (pascal_row[j - 1] + pascal_row[j]) % 7 if pascal_row[j - 1] != 0: cnt += 2 j += 1 - - #Adding the middle element for even rows + + # Adding the middle element for even rows if i % 2 == 0: pascal_row[-1] = pascal_row[-1] * 2 l += 1 if pascal_row[-1] % 7 != 0: cnt += 1 - #Deleting the last element for odd rows since 1 is added at beginning + # Deleting the last element for odd rows since 1 is added at beginning else: del pascal_row[-1] pascal_row.insert(0, 1) - - #Adding 2 to the count for the Additonal 1's in the new pascal row + + # Adding 2 to the count for the Additonal 1's in the new pascal row cnt += 2 - + return cnt + if __name__ == "__main__": - print(f"{solution()}") \ No newline at end of file + print(f"{solution()}") From c0c3e3a5965e1645da38baaad0795d646d6ed6c8 Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Sat, 15 Apr 2023 01:25:16 +0530 Subject: [PATCH 04/19] Added solution to problem 148 in Project Euler --- project_euler/problem_148/sol1.py | 65 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index a54568a132f4..1d6f3ca31675 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -4,27 +4,35 @@ Author: Sai Teja Manchi Problem Statement: -We can easily verify that none of the entries in the first seven rows of Pascal's triangle are divisible by 7: +We can easily verify that none of the entries in the +first seven rows of Pascal's triangle are divisible by 7: - 1 - 1 1 - 1 2 1 - 1 3 3 1 - 1 4 6 4 1 - 1 5 10 10 5 1 -1 6 15 20 15 6 1 -However, if we check the first one hundred rows, we will find that only 2361 of the 5050 entries are not divisible by 7. + 1 + 1 1 + 1 2 1 + 1 3 3 1 + 1 4 6 4 1 + 1 5 10 10 5 1 +1 6 15 20 15 6 1 +However, if we check the first one hundred rows, we will find that +only 2361 of the 5050 entries are not divisible by 7. -Find the number of entries which are not divisible by 7 in the first one billion (109) rows of Pascal's triangle. +Find the number of entries which are not divisible by 7 +in the first one billion (109) rows of Pascal's triangle. Solution: We Generate each row in the pascal triangle one-by-one. -Since Pascal's triangle is vertically-symmetric we only need to generate half of the values. +Since Pascal's triangle is vertically-symmetric, +we only need to generate half of the values We then count the values which are not divisible by 7. We only store the remainders(when divided by 7) in the list to reduce memory usage.. + +Note: In the original problem, we need to calucalte for 10^9 + but we took 10^6 here by default. """ -def solution(n: int = 10**9) -> int: + +def solution(pascal_row_count: int = 10**5) -> int: """ To evaluate the solution, use solution() >>> solution(3) @@ -34,39 +42,40 @@ def solution(n: int = 10**9) -> int: >>> solution(100) 2361 """ - - #Initializing pascal row and count + + # Initializing pascal row and count pascal_row = [1, 2] cnt = 6 - - #To keep track of length of the pascal row + + # To keep track of length of the pascal row l = 2 - - for i in range(3, n): + + for i in range(3, pascal_row_count): j = 1 - - #Generating the next pascal row + + # Generating the next pascal row while j < l: - pascal_row[j-1] = (pascal_row[j - 1] + pascal_row[j]) % 7 + pascal_row[j - 1] = (pascal_row[j - 1] + pascal_row[j]) % 7 if pascal_row[j - 1] != 0: cnt += 2 j += 1 - - #Adding the middle element for even rows + + # Adding the middle element for even rows if i % 2 == 0: pascal_row[-1] = pascal_row[-1] * 2 l += 1 if pascal_row[-1] % 7 != 0: cnt += 1 - #Deleting the last element for odd rows since 1 is added at beginning + # Deleting the last element for odd rows since 1 is added at beginning else: del pascal_row[-1] pascal_row.insert(0, 1) - - #Adding 2 to the count for the Additonal 1's in the new pascal row + + # Adding 2 to the count for the Additonal 1's in the new pascal row cnt += 2 - + return cnt + if __name__ == "__main__": - print(f"{solution()}") \ No newline at end of file + print(f"{solution()}") From 922bc8ac6376576e5ec6048bebcb213638acdb96 Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Sat, 15 Apr 2023 23:13:39 +0530 Subject: [PATCH 05/19] Added Solution for Problem 148 in Project Euler --- project_euler/problem_148/sol1.py | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 project_euler/problem_148/sol1.py diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py new file mode 100644 index 000000000000..558860b53b5d --- /dev/null +++ b/project_euler/problem_148/sol1.py @@ -0,0 +1,77 @@ +""" +Project Euler Problem 148 : https://projecteuler.net/problem=148 +Author: Sai Teja Manchi +Problem Statement: +We can easily verify that none of the entries in the +first seven rows of Pascal's triangle are divisible by 7: + 1 + 1 1 + 1 2 1 + 1 3 3 1 + 1 4 6 4 1 + 1 5 10 10 5 1 +1 6 15 20 15 6 1 +However, if we check the first one hundred rows, we will find that +only 2361 of the 5050 entries are not divisible by 7. +Find the number of entries which are not divisible by 7 +in the first one billion (109) rows of Pascal's triangle. + +Solution: +We iteratively Generate each row in the pascal triangle one-by-one. +Since Pascal's triangle is vertically-symmetric, +We only need to generate half of the values +We then count the values which are not divisible by 7. +We only store the remainders(when divided by 7) in the list to reduce memory usage. + +Note: In the original problem, we need to calucalte for 10^9 + but we took 10^6 here by default. +""" + + +def solution(pascal_row_count: int = 10**6) -> int: + """ + To evaluate the solution, use solution() + >>> solution(3) + 6 + >>> solution(10) + 40 + >>> solution(100) + 2361 + """ + + # Initializing pascal row and count + pascal_row = [1, 2] + cnt = 6 + + # To keep track of length of the pascal row + l = 2 + + for i in range(3, pascal_row_count): + j = 1 + + # Generating the next pascal row + while j < l: + pascal_row[j - 1] = (pascal_row[j - 1] + pascal_row[j]) % 7 + if pascal_row[j - 1] != 0: + cnt += 2 + j += 1 + + # Adding the middle element for even rows + if i % 2 == 0: + pascal_row[-1] = pascal_row[-1] * 2 + l += 1 + if pascal_row[-1] % 7 != 0: + cnt += 1 + # Deleting the last element for odd rows since 1 is added at beginning + else: + del pascal_row[-1] + pascal_row.insert(0, 1) + + # Adding 2 to the count for the Additonal 1's in the new pascal row + cnt += 2 + + return cnt + + +if __name__ == "__main__": + print(f"{solution()}") \ No newline at end of file From ba60c2372e27e926d48f547d18822fb7057054fb Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Sun, 16 Apr 2023 01:06:19 +0530 Subject: [PATCH 06/19] Added Solution for Problem 148 in Project Euler --- project_euler/problem_148/sol1.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 558860b53b5d..2f603422b840 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -17,18 +17,18 @@ in the first one billion (109) rows of Pascal's triangle. Solution: -We iteratively Generate each row in the pascal triangle one-by-one. +We iteratively generate each row in the pascal triangle one-by-one. Since Pascal's triangle is vertically-symmetric, -We only need to generate half of the values +We only need to generate half of the values. We then count the values which are not divisible by 7. We only store the remainders(when divided by 7) in the list to reduce memory usage. -Note: In the original problem, we need to calucalte for 10^9 - but we took 10^6 here by default. +Note: In the original problem, we need to calucalte for 10^9 rows + but we took 10^5 rows here by default. """ -def solution(pascal_row_count: int = 10**6) -> int: +def solution(pascal_row_count: int = 10**5) -> int: """ To evaluate the solution, use solution() >>> solution(3) @@ -41,7 +41,7 @@ def solution(pascal_row_count: int = 10**6) -> int: # Initializing pascal row and count pascal_row = [1, 2] - cnt = 6 + count = 6 # To keep track of length of the pascal row l = 2 @@ -53,7 +53,7 @@ def solution(pascal_row_count: int = 10**6) -> int: while j < l: pascal_row[j - 1] = (pascal_row[j - 1] + pascal_row[j]) % 7 if pascal_row[j - 1] != 0: - cnt += 2 + count += 2 j += 1 # Adding the middle element for even rows @@ -61,16 +61,16 @@ def solution(pascal_row_count: int = 10**6) -> int: pascal_row[-1] = pascal_row[-1] * 2 l += 1 if pascal_row[-1] % 7 != 0: - cnt += 1 + count += 1 # Deleting the last element for odd rows since 1 is added at beginning else: del pascal_row[-1] pascal_row.insert(0, 1) # Adding 2 to the count for the Additonal 1's in the new pascal row - cnt += 2 + count += 2 - return cnt + return count if __name__ == "__main__": From 04f30b3ac1a0aed9a4ea5d77a44fa82afe1d9b48 Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Sun, 16 Apr 2023 01:13:58 +0530 Subject: [PATCH 07/19] Added Solution for Problem 148 in Project Euler --- project_euler/problem_148/sol1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 2f603422b840..9328473bec05 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -74,4 +74,5 @@ def solution(pascal_row_count: int = 10**5) -> int: if __name__ == "__main__": - print(f"{solution()}") \ No newline at end of file + print(f"{solution()}") + \ No newline at end of file From 8c1f2c6685c562ce95e9c3c35d9c04cd8e12793a Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Sun, 16 Apr 2023 01:16:34 +0530 Subject: [PATCH 08/19] Added Solution for Problem 148 in Project Euler --- project_euler/problem_148/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 9328473bec05..38437a30e2b7 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -75,4 +75,3 @@ def solution(pascal_row_count: int = 10**5) -> int: if __name__ == "__main__": print(f"{solution()}") - \ No newline at end of file From 41c6d412977f8286780eb3c21c18501845a7eeac Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Sun, 16 Apr 2023 01:30:20 +0530 Subject: [PATCH 09/19] Added Solution for Problem 148 in Project Euler --- project_euler/problem_148/sol1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 38437a30e2b7..99d8db4b25d0 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -23,12 +23,12 @@ We then count the values which are not divisible by 7. We only store the remainders(when divided by 7) in the list to reduce memory usage. -Note: In the original problem, we need to calucalte for 10^9 rows +Note: In the original problem, we need to calcualte for 10^9 rows but we took 10^5 rows here by default. """ -def solution(pascal_row_count: int = 10**5) -> int: +def solution(pascal_row_count: int = 10**6) -> int: """ To evaluate the solution, use solution() >>> solution(3) @@ -67,7 +67,7 @@ def solution(pascal_row_count: int = 10**5) -> int: del pascal_row[-1] pascal_row.insert(0, 1) - # Adding 2 to the count for the Additonal 1's in the new pascal row + # Adding 2 to the count for the Additional 1's in the new pascal row count += 2 return count From 9d98bb1b2760fcda4bcca00346a455dc01d09e8a Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Mon, 17 Apr 2023 02:39:20 +0530 Subject: [PATCH 10/19] Added Solution for Problem 148 of Project Euler --- project_euler/problem_148/sol1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 99d8db4b25d0..49a2be0eedf3 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -23,12 +23,12 @@ We then count the values which are not divisible by 7. We only store the remainders(when divided by 7) in the list to reduce memory usage. -Note: In the original problem, we need to calcualte for 10^9 rows - but we took 10^5 rows here by default. +Note: In the original problem, we need to calculate for 10^9 rows + but we took 10^4 rows here by default. """ -def solution(pascal_row_count: int = 10**6) -> int: +def solution(pascal_row_count: int = 10**4) -> int: """ To evaluate the solution, use solution() >>> solution(3) From e4e89cac89e58d5f382d498e8f5566dcbe7993f5 Mon Sep 17 00:00:00 2001 From: Sai Teja Manchi Date: Mon, 24 Apr 2023 14:36:51 +0530 Subject: [PATCH 11/19] Added solution for problem 148 in Project Euler --- project_euler/problem_148/sol1.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 49a2be0eedf3..1a168f3b2e5e 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -28,7 +28,7 @@ """ -def solution(pascal_row_count: int = 10**4) -> int: +def solution(pascal_row_count: int = 10**9) -> int: """ To evaluate the solution, use solution() >>> solution(3) @@ -54,7 +54,7 @@ def solution(pascal_row_count: int = 10**4) -> int: pascal_row[j - 1] = (pascal_row[j - 1] + pascal_row[j]) % 7 if pascal_row[j - 1] != 0: count += 2 - j += 1 + j += 1 # Adding the middle element for even rows if i % 2 == 0: From 237b6c904569da76d6e244e79f3182b30b6100f8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 24 Apr 2023 09:09:53 +0000 Subject: [PATCH 12/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_148/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 1a168f3b2e5e..a594f569000b 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -54,7 +54,7 @@ def solution(pascal_row_count: int = 10**9) -> int: pascal_row[j - 1] = (pascal_row[j - 1] + pascal_row[j]) % 7 if pascal_row[j - 1] != 0: count += 2 - j += 1 + j += 1 # Adding the middle element for even rows if i % 2 == 0: From 4270291dfb36dc99cae274c294822d30783dd09e Mon Sep 17 00:00:00 2001 From: saitejamaanchi Date: Sun, 30 Apr 2023 21:15:27 +0530 Subject: [PATCH 13/19] Added an optimal solution for Problem 148 in project Euler --- project_euler/problem_148/sol1.py | 67 +++++++++---------------------- 1 file changed, 18 insertions(+), 49 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index a594f569000b..1c3bd0747698 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -15,19 +15,19 @@ only 2361 of the 5050 entries are not divisible by 7. Find the number of entries which are not divisible by 7 in the first one billion (109) rows of Pascal's triangle. - -Solution: -We iteratively generate each row in the pascal triangle one-by-one. -Since Pascal's triangle is vertically-symmetric, -We only need to generate half of the values. -We then count the values which are not divisible by 7. -We only store the remainders(when divided by 7) in the list to reduce memory usage. - -Note: In the original problem, we need to calculate for 10^9 rows - but we took 10^4 rows here by default. """ - - +def get_num_binomials(row_num: int) -> int: + """ + To compute the number of entries in the nth row of pascal triangle that are not divisble by 7. + Based on Lucas Theroem it is the product of (each digit in the base 7 n + 1) + Reference: https://brilliant.org/wiki/lucas-theorem/ + """ + cnt = 1 + while row_num > 0: + cnt *= ((row_num % 7) + 1) + row_num //= 7 + return cnt + def solution(pascal_row_count: int = 10**9) -> int: """ To evaluate the solution, use solution() @@ -38,40 +38,9 @@ def solution(pascal_row_count: int = 10**9) -> int: >>> solution(100) 2361 """ - - # Initializing pascal row and count - pascal_row = [1, 2] - count = 6 - - # To keep track of length of the pascal row - l = 2 - - for i in range(3, pascal_row_count): - j = 1 - - # Generating the next pascal row - while j < l: - pascal_row[j - 1] = (pascal_row[j - 1] + pascal_row[j]) % 7 - if pascal_row[j - 1] != 0: - count += 2 - j += 1 - - # Adding the middle element for even rows - if i % 2 == 0: - pascal_row[-1] = pascal_row[-1] * 2 - l += 1 - if pascal_row[-1] % 7 != 0: - count += 1 - # Deleting the last element for odd rows since 1 is added at beginning - else: - del pascal_row[-1] - pascal_row.insert(0, 1) - - # Adding 2 to the count for the Additional 1's in the new pascal row - count += 2 - - return count - - -if __name__ == "__main__": - print(f"{solution()}") + + result = 0 + for i in tqdm(range(pascal_row_count)): + result += get_num_binomials(i) % 7 + + return result \ No newline at end of file From ba29c52731a9ea6334cbfc358c78c964f75c073c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 30 Apr 2023 15:46:21 +0000 Subject: [PATCH 14/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_148/sol1.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 1c3bd0747698..cc00ae894c16 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -16,18 +16,21 @@ Find the number of entries which are not divisible by 7 in the first one billion (109) rows of Pascal's triangle. """ + + def get_num_binomials(row_num: int) -> int: """ To compute the number of entries in the nth row of pascal triangle that are not divisble by 7. - Based on Lucas Theroem it is the product of (each digit in the base 7 n + 1) + Based on Lucas Theroem it is the product of (each digit in the base 7 n + 1) Reference: https://brilliant.org/wiki/lucas-theorem/ """ cnt = 1 while row_num > 0: - cnt *= ((row_num % 7) + 1) + cnt *= (row_num % 7) + 1 row_num //= 7 return cnt - + + def solution(pascal_row_count: int = 10**9) -> int: """ To evaluate the solution, use solution() @@ -38,9 +41,9 @@ def solution(pascal_row_count: int = 10**9) -> int: >>> solution(100) 2361 """ - + result = 0 for i in tqdm(range(pascal_row_count)): result += get_num_binomials(i) % 7 - - return result \ No newline at end of file + + return result From 249a5074c603528b529a46b9a3d553490df52f92 Mon Sep 17 00:00:00 2001 From: saitejamaanchi Date: Sun, 30 Apr 2023 21:45:13 +0530 Subject: [PATCH 15/19] Added an optimal solution for Problem 148 in project Euler --- project_euler/problem_148/sol1.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 1c3bd0747698..9e0f620f3f84 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -16,18 +16,22 @@ Find the number of entries which are not divisible by 7 in the first one billion (109) rows of Pascal's triangle. """ + + def get_num_binomials(row_num: int) -> int: """ - To compute the number of entries in the nth row of pascal triangle that are not divisble by 7. - Based on Lucas Theroem it is the product of (each digit in the base 7 n + 1) + To compute the number of entries in the nth row of + pascal triangle that are not divisble by 7. + Based on Lucas Theroem it is the product of (each digit in the base 7 n + 1) Reference: https://brilliant.org/wiki/lucas-theorem/ """ cnt = 1 while row_num > 0: - cnt *= ((row_num % 7) + 1) + cnt *= (row_num % 7) + 1 row_num //= 7 return cnt - + + def solution(pascal_row_count: int = 10**9) -> int: """ To evaluate the solution, use solution() @@ -38,9 +42,9 @@ def solution(pascal_row_count: int = 10**9) -> int: >>> solution(100) 2361 """ - + result = 0 - for i in tqdm(range(pascal_row_count)): + for i in range(pascal_row_count): result += get_num_binomials(i) % 7 - - return result \ No newline at end of file + + return result From 31b183150e7145fdce388de5ecd6ea0fc427c029 Mon Sep 17 00:00:00 2001 From: saitejamaanchi Date: Sun, 30 Apr 2023 21:49:59 +0530 Subject: [PATCH 16/19] Added an optimal solution for Problem 148 in project Euler --- project_euler/problem_148/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index ea25b57aa193..ec7aea701dd3 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -47,6 +47,6 @@ def solution(pascal_row_count: int = 10**9) -> int: result = 0 for i in range(pascal_row_count): - result += get_num_binomials(i) % 7 + result += get_num_binomials(i) return result From cf0c3b1756d8228fae6a962833689bbfb9707507 Mon Sep 17 00:00:00 2001 From: saitejamaanchi Date: Sun, 30 Apr 2023 22:10:41 +0530 Subject: [PATCH 17/19] Added doctests for get_num_binomials function in Problem 148 in project Euler --- project_euler/problem_148/sol1.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index ec7aea701dd3..9f562cfe8a02 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -26,6 +26,12 @@ def get_num_binomials(row_num: int) -> int: pascal triangle that are not divisble by 7. Based on Lucas Theroem it is the product of (each digit in the base 7 n + 1) Reference: https://brilliant.org/wiki/lucas-theorem/ + >>> get_num_binomials(3) + 4 + >>> get_num_binomials(6) + 7 + >>> get_num_binomials(10) + 8 """ cnt = 1 while row_num > 0: From 3854de8b0108412c91e5108b2bbb92bdbd6fa20e Mon Sep 17 00:00:00 2001 From: saitejamaanchi Date: Mon, 1 May 2023 02:23:08 +0530 Subject: [PATCH 18/19] Reformatted code --- project_euler/problem_148/sol1.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 9f562cfe8a02..2c7fb2884dcf 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -17,7 +17,6 @@ in the first one billion (109) rows of Pascal's triangle. """ - def get_num_binomials(row_num: int) -> int: """ To compute the number of entries in the nth row of @@ -39,7 +38,6 @@ def get_num_binomials(row_num: int) -> int: row_num //= 7 return cnt - def solution(pascal_row_count: int = 10**9) -> int: """ To evaluate the solution, use solution() From 0b706b91b57d9ef0bd36a02a455f335df1aded7e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 30 Apr 2023 20:54:38 +0000 Subject: [PATCH 19/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- project_euler/problem_148/sol1.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/project_euler/problem_148/sol1.py b/project_euler/problem_148/sol1.py index 2c7fb2884dcf..9f562cfe8a02 100644 --- a/project_euler/problem_148/sol1.py +++ b/project_euler/problem_148/sol1.py @@ -17,6 +17,7 @@ in the first one billion (109) rows of Pascal's triangle. """ + def get_num_binomials(row_num: int) -> int: """ To compute the number of entries in the nth row of @@ -38,6 +39,7 @@ def get_num_binomials(row_num: int) -> int: row_num //= 7 return cnt + def solution(pascal_row_count: int = 10**9) -> int: """ To evaluate the solution, use solution()