From f59b677a566fb5502a44f0d158ee280967c0c2cc Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 12:27:47 +0530 Subject: [PATCH 01/18] Create __init__.py --- project_euler/problem_35/__init__.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 project_euler/problem_35/__init__.py diff --git a/project_euler/problem_35/__init__.py b/project_euler/problem_35/__init__.py new file mode 100644 index 000000000000..31eceee29cd3 --- /dev/null +++ b/project_euler/problem_35/__init__.py @@ -0,0 +1 @@ +#\n From 791d220f300c0a2bb3ba930fd875b7c44cab9232 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 12:29:12 +0530 Subject: [PATCH 02/18] Update __init__.py --- project_euler/problem_35/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_35/__init__.py b/project_euler/problem_35/__init__.py index 31eceee29cd3..792d6005489e 100644 --- a/project_euler/problem_35/__init__.py +++ b/project_euler/problem_35/__init__.py @@ -1 +1 @@ -#\n +# From ebadae5893648ffca853410b323475a3b93d3e4f Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 12:29:55 +0530 Subject: [PATCH 03/18] Add files via upload --- project_euler/problem_35/sol1.py | 82 ++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 project_euler/problem_35/sol1.py diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py new file mode 100644 index 000000000000..423e1a4c633c --- /dev/null +++ b/project_euler/problem_35/sol1.py @@ -0,0 +1,82 @@ +""" +The number,197, is called a circular prime because all rotations of the digits: +197, 971, and 719, are themselves prime. + +There are thirteen such primes below 100: 2,3, 5, 7, 11, 13, 17, 31, 37, 71,73, +79, and 97. + +How many circular primes are there below one million? +""" + +""" +To solve this problem in an efficient manner, we will first mark all the primes +below 1 million using the Seive of Eratosthenes. + +Then, out of all the primes, we will rule out the numbers which contain an even +digit. + +After this we will generate each circular combination of the number and check +if all are prime. +""" + +seive = [True] * 1000001 +i = 2 +while i * i <= 1000000: + if seive[i] == True: + for j in range(i * i, 1000001, i): + seive[j] = False + i += 1 + + +def is_prime(n): + """ + Returns True if n is prime, + False otherwise, for 2<=n<=1000000 + >>> is_prime(87) + False + >>> is_prime(23) + True + >>> is_prime(25363) + False + """ + return seive[n] + + +def even_digit(n): + """ + Returns True if n contains an even digit + otherwise False + >>> even_digit(0) + True + >>> even_digit(975317933) + False + >>> even_digit(-245679) + True + """ + digits = "02468" + for i in digits: + if i in str(n): + return True + return False + + +def compute(): + """ + Returns the total count of all numbers + below 1 million, which are circular primes + >>> compute() + 55 + """ + limit = 1000000 + count = 1 # count already includes the number 2. + for num in range(3, limit + 1, 2): + if is_prime(num) and not even_digit(num): + str_num = str(num) + list_nums = [int(str_num[j:] + str_num[:j]) for j in range(len(str_num))] + if all(list(map(is_prime, list_nums))): + count += 1 + return count + + +if __name__ == "__main__": + print(compute()) From dea8fe58905a7d4c109c66ad8bcf9211899a2ed3 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 12:40:21 +0530 Subject: [PATCH 04/18] Update sol1.py --- project_euler/problem_35/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 423e1a4c633c..a7d64c88769c 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -22,7 +22,7 @@ seive = [True] * 1000001 i = 2 while i * i <= 1000000: - if seive[i] == True: + if seive[i]: for j in range(i * i, 1000001, i): seive[j] = False i += 1 From c983a9c6119c0cae680034289b6f5c0595d3815d Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 12:50:51 +0530 Subject: [PATCH 05/18] Update sol1.py to include type hints --- project_euler/problem_35/sol1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index a7d64c88769c..d670d7d488a6 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -28,7 +28,7 @@ i += 1 -def is_prime(n): +def is_prime(n: int) -> bool: """ Returns True if n is prime, False otherwise, for 2<=n<=1000000 @@ -42,7 +42,7 @@ def is_prime(n): return seive[n] -def even_digit(n): +def even_digit(n: int) -> bool: """ Returns True if n contains an even digit otherwise False @@ -60,7 +60,7 @@ def even_digit(n): return False -def compute(): +def compute() -> int: """ Returns the total count of all numbers below 1 million, which are circular primes From 98c2a4e3dfd5dfb5fe924189864f0616bbe743ba Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 13:01:36 +0530 Subject: [PATCH 06/18] Update CONTRIBUTING.md to fix typo --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2761be61aa8b..63f60c51f8e6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -59,7 +59,7 @@ We want your work to be readable by others; therefore, we encourage you to note - Expand acronyms because __gcd()__ is hard to understand but __greatest_common_divisor()__ is not. - Please follow the [Python Naming Conventions](https://pep8.org/#prescriptive-naming-conventions) so variable_names and function_names should be lower_case, CONSTANTS in UPPERCASE, ClassNames should be CamelCase, etc. -- We encourage the use of Python [f-strings](https://realpython.com/python-f-strings/#f-strings-a-new-and-improved-way-to-format-strings-in-python) where the make the code easier to read. +- We encourage the use of Python [f-strings](https://realpython.com/python-f-strings/#f-strings-a-new-and-improved-way-to-format-strings-in-python) where they make the code easier to read. - Please consider running [__psf/black__](https://github.com/python/black) on your Python file(s) before submitting your pull request. This is not yet a requirement but it does make your code more readable and automatically aligns it with much of [PEP 8](https://www.python.org/dev/peps/pep-0008/). There are other code formatters (autopep8, yapf) but the __black__ formatter is now hosted by the Python Software Foundation. To use it, From 2a3f796d976e880904ac68626cd18ffd1f902c1c Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 13:02:22 +0530 Subject: [PATCH 07/18] Update CONTRIBUTING.md --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 63f60c51f8e6..2761be61aa8b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -59,7 +59,7 @@ We want your work to be readable by others; therefore, we encourage you to note - Expand acronyms because __gcd()__ is hard to understand but __greatest_common_divisor()__ is not. - Please follow the [Python Naming Conventions](https://pep8.org/#prescriptive-naming-conventions) so variable_names and function_names should be lower_case, CONSTANTS in UPPERCASE, ClassNames should be CamelCase, etc. -- We encourage the use of Python [f-strings](https://realpython.com/python-f-strings/#f-strings-a-new-and-improved-way-to-format-strings-in-python) where they make the code easier to read. +- We encourage the use of Python [f-strings](https://realpython.com/python-f-strings/#f-strings-a-new-and-improved-way-to-format-strings-in-python) where the make the code easier to read. - Please consider running [__psf/black__](https://github.com/python/black) on your Python file(s) before submitting your pull request. This is not yet a requirement but it does make your code more readable and automatically aligns it with much of [PEP 8](https://www.python.org/dev/peps/pep-0008/). There are other code formatters (autopep8, yapf) but the __black__ formatter is now hosted by the Python Software Foundation. To use it, From 06ab574b35bfe760738184372f5820efe721b423 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 22:15:20 +0530 Subject: [PATCH 08/18] Update project_euler/problem_35/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_35/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index d670d7d488a6..479e5ed5a330 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -60,7 +60,7 @@ def even_digit(n: int) -> bool: return False -def compute() -> int: +def find_circular_primes(limit: int=1000000) -> int: """ Returns the total count of all numbers below 1 million, which are circular primes From 21e0d139eb0f87843e92f7b5d39c9b71754c1ac7 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 22:15:38 +0530 Subject: [PATCH 09/18] Update project_euler/problem_35/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_35/sol1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 479e5ed5a330..b8c968e60767 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -1,5 +1,6 @@ """ -The number,197, is called a circular prime because all rotations of the digits: +The number 197 is called a circular prime because all rotations of the digits: + 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2,3, 5, 7, 11, 13, 17, 31, 37, 71,73, From 2739862317b7bda188463bbcd6f72374ff186adb Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 22:15:47 +0530 Subject: [PATCH 10/18] Update project_euler/problem_35/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_35/sol1.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index b8c968e60767..64ad0f2fb98a 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -3,7 +3,8 @@ 197, 971, and 719, are themselves prime. -There are thirteen such primes below 100: 2,3, 5, 7, 11, 13, 17, 31, 37, 71,73, +There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, + 79, and 97. How many circular primes are there below one million? From 3bc2f90bbbb6b43cb298de95e7718b490314c3e9 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 22:20:00 +0530 Subject: [PATCH 11/18] Update project_euler/problem_35/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_35/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 64ad0f2fb98a..17fd6c067d5e 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -4,7 +4,6 @@ 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, - 79, and 97. How many circular primes are there below one million? From 2ff80b57cd411390c2438d71ecd8f998944ce87c Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 22:20:10 +0530 Subject: [PATCH 12/18] Update project_euler/problem_35/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_35/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 17fd6c067d5e..2ed0e43da4b7 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -1,6 +1,5 @@ """ The number 197 is called a circular prime because all rotations of the digits: - 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, From 40f2113ba140747bda4a341a45d0ff46a3159d37 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 23:43:38 +0530 Subject: [PATCH 13/18] Update project_euler/problem_35/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_35/sol1.py | 1 - 1 file changed, 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 2ed0e43da4b7..4389fa6dbe1d 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -67,7 +67,6 @@ def find_circular_primes(limit: int=1000000) -> int: >>> compute() 55 """ - limit = 1000000 count = 1 # count already includes the number 2. for num in range(3, limit + 1, 2): if is_prime(num) and not even_digit(num): From a08127045680cd8d3c9b744de126dcaeda582cdb Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sat, 15 Aug 2020 23:43:49 +0530 Subject: [PATCH 14/18] Update project_euler/problem_35/sol1.py Co-authored-by: Christian Clauss --- project_euler/problem_35/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 4389fa6dbe1d..6b74067c55dc 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -78,4 +78,4 @@ def find_circular_primes(limit: int=1000000) -> int: if __name__ == "__main__": - print(compute()) + print(f"len(find_circular_primes()) = }") From 2cdf28e6c1e0c6a6a651d35554975e757bf49c70 Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sun, 16 Aug 2020 00:06:43 +0530 Subject: [PATCH 15/18] Update sol1.py --- project_euler/problem_35/sol1.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 6b74067c55dc..728a4ab20b89 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -53,18 +53,14 @@ def even_digit(n: int) -> bool: >>> even_digit(-245679) True """ - digits = "02468" - for i in digits: - if i in str(n): - return True - return False + return any([True for i in "02468" if i in str(n)]) -def find_circular_primes(limit: int=1000000) -> int: +def find_circular_primes(limit: int = 1000000) -> int: """ Returns the total count of all numbers below 1 million, which are circular primes - >>> compute() + >>> find_circular_primes(1000000) 55 """ count = 1 # count already includes the number 2. @@ -72,10 +68,10 @@ def find_circular_primes(limit: int=1000000) -> int: if is_prime(num) and not even_digit(num): str_num = str(num) list_nums = [int(str_num[j:] + str_num[:j]) for j in range(len(str_num))] - if all(list(map(is_prime, list_nums))): + if all([is_prime(i) for i in list_nums]): count += 1 return count if __name__ == "__main__": - print(f"len(find_circular_primes()) = }") + print(f"len(find_circular_primes()) = {find_circular_primes(1000000)}") From fc82e5257b70931aa01b00010d2ce98d0db7bdf0 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 15 Aug 2020 23:30:56 +0200 Subject: [PATCH 16/18] Update sol1.py --- project_euler/problem_35/sol1.py | 50 ++++++++++++++------------------ 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 728a4ab20b89..548ae4fbca84 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -1,23 +1,16 @@ """ The number 197 is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. - There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. - How many circular primes are there below one million? -""" -""" To solve this problem in an efficient manner, we will first mark all the primes -below 1 million using the Seive of Eratosthenes. - -Then, out of all the primes, we will rule out the numbers which contain an even -digit. - -After this we will generate each circular combination of the number and check -if all are prime. +below 1 million using the Seive of Eratosthenes. Then, out of all these primes, +we will rule out the numbers which contain an even digit. After this we will +generate each circular combination of the number and check if all are prime. """ +from typing import List seive = [True] * 1000001 i = 2 @@ -30,8 +23,7 @@ def is_prime(n: int) -> bool: """ - Returns True if n is prime, - False otherwise, for 2<=n<=1000000 + For 2 <= n <= 1000000, return True if n is prime. >>> is_prime(87) False >>> is_prime(23) @@ -42,35 +34,35 @@ def is_prime(n: int) -> bool: return seive[n] -def even_digit(n: int) -> bool: +def contains_an_even_digit(n: int) -> bool: """ - Returns True if n contains an even digit - otherwise False - >>> even_digit(0) + Return True if n contains an even digit. + >>> contains_an_even_digit(0) True - >>> even_digit(975317933) + >>> contains_an_even_digit(975317933) False - >>> even_digit(-245679) + >>> contains_an_even_digit(-245679) True """ - return any([True for i in "02468" if i in str(n)]) + return any(digit in "02468" for digit in str(n)) -def find_circular_primes(limit: int = 1000000) -> int: +def find_circular_primes(limit: int = 1000000) -> List[int]: """ - Returns the total count of all numbers - below 1 million, which are circular primes - >>> find_circular_primes(1000000) + Return circular primes below limit. + >>> len(find_circular_primes(100)) + 13 + >>> len(find_circular_primes(1000000)) 55 """ - count = 1 # count already includes the number 2. + result = [2] # result already includes the number 2. for num in range(3, limit + 1, 2): - if is_prime(num) and not even_digit(num): + if is_prime(num) and not contains_an_even_digit(num): str_num = str(num) list_nums = [int(str_num[j:] + str_num[:j]) for j in range(len(str_num))] - if all([is_prime(i) for i in list_nums]): - count += 1 - return count + if all(is_prime(i) for i in list_nums): + result.append(num) + return result if __name__ == "__main__": From 82ef08157133920773c5fc65befcbb01645c91ce Mon Sep 17 00:00:00 2001 From: Kushagra Bansal Date: Sun, 16 Aug 2020 08:28:10 +0530 Subject: [PATCH 17/18] Update sol1.py --- project_euler/problem_35/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 548ae4fbca84..8c32033ecd29 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -66,4 +66,4 @@ def find_circular_primes(limit: int = 1000000) -> List[int]: if __name__ == "__main__": - print(f"len(find_circular_primes()) = {find_circular_primes(1000000)}") + print(f"len(find_circular_primes()) = {len(find_circular_primes(1000000))}") From 0930fd42bfcd1d7319fa2f7972f1f4424b353038 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 16 Aug 2020 07:43:56 +0200 Subject: [PATCH 18/18] Fix the print(f"string") --- project_euler/problem_35/sol1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_35/sol1.py b/project_euler/problem_35/sol1.py index 8c32033ecd29..c47eb7d82f54 100644 --- a/project_euler/problem_35/sol1.py +++ b/project_euler/problem_35/sol1.py @@ -66,4 +66,4 @@ def find_circular_primes(limit: int = 1000000) -> List[int]: if __name__ == "__main__": - print(f"len(find_circular_primes()) = {len(find_circular_primes(1000000))}") + print(f"{len(find_circular_primes()) = }")