mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Unify O(sqrt(N))
is_prime
functions under project_euler
(#6258)
* fixes #5434 * fixes broken solution * removes assert * removes assert * Apply suggestions from code review Co-authored-by: John Law <johnlaw.po@gmail.com> * Update project_euler/problem_003/sol1.py Co-authored-by: John Law <johnlaw.po@gmail.com>
This commit is contained in:
parent
81e30fd33c
commit
2104fa7aeb
|
@ -13,9 +13,11 @@ References:
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
def is_prime(num: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Returns boolean representing primality of given number num.
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
|
Returns boolean representing primality of given number (i.e., if the
|
||||||
|
result is true, then the number is indeed prime else it is not).
|
||||||
|
|
||||||
>>> is_prime(2)
|
>>> is_prime(2)
|
||||||
True
|
True
|
||||||
|
@ -26,23 +28,21 @@ def is_prime(num: int) -> bool:
|
||||||
>>> is_prime(2999)
|
>>> is_prime(2999)
|
||||||
True
|
True
|
||||||
>>> is_prime(0)
|
>>> is_prime(0)
|
||||||
Traceback (most recent call last):
|
False
|
||||||
...
|
|
||||||
ValueError: Parameter num must be greater than or equal to two.
|
|
||||||
>>> is_prime(1)
|
>>> is_prime(1)
|
||||||
Traceback (most recent call last):
|
False
|
||||||
...
|
|
||||||
ValueError: Parameter num must be greater than or equal to two.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if num <= 1:
|
if 1 < number < 4:
|
||||||
raise ValueError("Parameter num must be greater than or equal to two.")
|
# 2 and 3 are primes
|
||||||
if num == 2:
|
|
||||||
return True
|
return True
|
||||||
elif num % 2 == 0:
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
return False
|
return False
|
||||||
for i in range(3, int(math.sqrt(num)) + 1, 2):
|
|
||||||
if num % i == 0:
|
# All primes number are in format of 6k +/- 1
|
||||||
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -15,28 +15,36 @@ References:
|
||||||
from math import sqrt
|
from math import sqrt
|
||||||
|
|
||||||
|
|
||||||
def is_prime(num: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Determines whether the given number is prime or not
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
|
Returns boolean representing primality of given number (i.e., if the
|
||||||
|
result is true, then the number is indeed prime else it is not).
|
||||||
|
|
||||||
>>> is_prime(2)
|
>>> is_prime(2)
|
||||||
True
|
True
|
||||||
>>> is_prime(15)
|
>>> is_prime(3)
|
||||||
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
False
|
False
|
||||||
>>> is_prime(29)
|
>>> is_prime(2999)
|
||||||
True
|
True
|
||||||
>>> is_prime(0)
|
>>> is_prime(0)
|
||||||
False
|
False
|
||||||
|
>>> is_prime(1)
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if num == 2:
|
if 1 < number < 4:
|
||||||
|
# 2 and 3 are primes
|
||||||
return True
|
return True
|
||||||
elif num % 2 == 0:
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
return False
|
return False
|
||||||
else:
|
|
||||||
sq = int(sqrt(num)) + 1
|
# All primes number are in format of 6k +/- 1
|
||||||
for i in range(3, sq, 2):
|
for i in range(5, int(sqrt(number) + 1), 6):
|
||||||
if num % i == 0:
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -11,22 +11,39 @@ What is the 10001st prime number?
|
||||||
References:
|
References:
|
||||||
- https://en.wikipedia.org/wiki/Prime_number
|
- https://en.wikipedia.org/wiki/Prime_number
|
||||||
"""
|
"""
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
def is_prime(number: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Determines whether the given number is prime or not
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
|
Returns boolean representing primality of given number (i.e., if the
|
||||||
|
result is true, then the number is indeed prime else it is not).
|
||||||
|
|
||||||
>>> is_prime(2)
|
>>> is_prime(2)
|
||||||
True
|
True
|
||||||
>>> is_prime(15)
|
>>> is_prime(3)
|
||||||
False
|
|
||||||
>>> is_prime(29)
|
|
||||||
True
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
>>> is_prime(0)
|
||||||
|
False
|
||||||
|
>>> is_prime(1)
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
|
|
||||||
for i in range(2, int(number**0.5) + 1):
|
if 1 < number < 4:
|
||||||
if number % i == 0:
|
# 2 and 3 are primes
|
||||||
|
return True
|
||||||
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
|
return False
|
||||||
|
|
||||||
|
# All primes number are in format of 6k +/- 1
|
||||||
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -16,20 +16,37 @@ import math
|
||||||
|
|
||||||
|
|
||||||
def is_prime(number: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Determines whether a given number is prime or not
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
|
Returns boolean representing primality of given number (i.e., if the
|
||||||
|
result is true, then the number is indeed prime else it is not).
|
||||||
|
|
||||||
>>> is_prime(2)
|
>>> is_prime(2)
|
||||||
True
|
True
|
||||||
>>> is_prime(15)
|
>>> is_prime(3)
|
||||||
False
|
|
||||||
>>> is_prime(29)
|
|
||||||
True
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
>>> is_prime(0)
|
||||||
|
False
|
||||||
|
>>> is_prime(1)
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if number % 2 == 0 and number > 2:
|
if 1 < number < 4:
|
||||||
|
# 2 and 3 are primes
|
||||||
|
return True
|
||||||
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
return False
|
return False
|
||||||
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
|
|
||||||
|
# All primes number are in format of 6k +/- 1
|
||||||
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def prime_generator():
|
def prime_generator():
|
||||||
|
|
|
@ -11,12 +11,14 @@ References:
|
||||||
- https://en.wikipedia.org/wiki/Prime_number
|
- https://en.wikipedia.org/wiki/Prime_number
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from math import sqrt
|
import math
|
||||||
|
|
||||||
|
|
||||||
def is_prime(n: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Returns boolean representing primality of given number num.
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
|
Returns boolean representing primality of given number num (i.e., if the
|
||||||
|
result is true, then the number is indeed prime else it is not).
|
||||||
|
|
||||||
>>> is_prime(2)
|
>>> is_prime(2)
|
||||||
True
|
True
|
||||||
|
@ -26,13 +28,24 @@ def is_prime(n: int) -> bool:
|
||||||
False
|
False
|
||||||
>>> is_prime(2999)
|
>>> is_prime(2999)
|
||||||
True
|
True
|
||||||
|
>>> is_prime(0)
|
||||||
|
False
|
||||||
|
>>> is_prime(1)
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if 1 < n < 4:
|
if 1 < number < 4:
|
||||||
|
# 2 and 3 are primes
|
||||||
return True
|
return True
|
||||||
elif n < 2 or not n % 2:
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
return False
|
return False
|
||||||
return not any(not n % i for i in range(3, int(sqrt(n) + 1), 2))
|
|
||||||
|
# All primes number are in format of 6k +/- 1
|
||||||
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def solution(n: int = 2000000) -> int:
|
def solution(n: int = 2000000) -> int:
|
||||||
|
|
|
@ -16,8 +16,10 @@ from itertools import takewhile
|
||||||
|
|
||||||
|
|
||||||
def is_prime(number: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Returns boolean representing primality of given number num.
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
|
Returns boolean representing primality of given number num (i.e., if the
|
||||||
|
result is true, then the number is indeed prime else it is not).
|
||||||
|
|
||||||
>>> is_prime(2)
|
>>> is_prime(2)
|
||||||
True
|
True
|
||||||
|
@ -27,11 +29,24 @@ def is_prime(number: int) -> bool:
|
||||||
False
|
False
|
||||||
>>> is_prime(2999)
|
>>> is_prime(2999)
|
||||||
True
|
True
|
||||||
|
>>> is_prime(0)
|
||||||
|
False
|
||||||
|
>>> is_prime(1)
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if number % 2 == 0 and number > 2:
|
if 1 < number < 4:
|
||||||
|
# 2 and 3 are primes
|
||||||
|
return True
|
||||||
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
return False
|
return False
|
||||||
return all(number % i for i in range(3, int(math.sqrt(number)) + 1, 2))
|
|
||||||
|
# All primes number are in format of 6k +/- 1
|
||||||
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def prime_generator() -> Iterator[int]:
|
def prime_generator() -> Iterator[int]:
|
||||||
|
|
|
@ -23,21 +23,38 @@ n = 0.
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
def is_prime(k: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Determine if a number is prime
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
>>> is_prime(10)
|
Returns boolean representing primality of given number num (i.e., if the
|
||||||
False
|
result is true, then the number is indeed prime else it is not).
|
||||||
>>> is_prime(11)
|
|
||||||
|
>>> is_prime(2)
|
||||||
True
|
True
|
||||||
|
>>> is_prime(3)
|
||||||
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
>>> is_prime(0)
|
||||||
|
False
|
||||||
|
>>> is_prime(1)
|
||||||
|
False
|
||||||
|
>>> is_prime(-10)
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
if k < 2 or k % 2 == 0:
|
|
||||||
return False
|
if 1 < number < 4:
|
||||||
elif k == 2:
|
# 2 and 3 are primes
|
||||||
return True
|
return True
|
||||||
else:
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
for x in range(3, int(math.sqrt(k) + 1), 2):
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
if k % x == 0:
|
return False
|
||||||
|
|
||||||
|
# All primes number are in format of 6k +/- 1
|
||||||
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
"""
|
"""
|
||||||
|
Truncatable primes
|
||||||
|
Problem 37: https://projecteuler.net/problem=37
|
||||||
|
|
||||||
The number 3797 has an interesting property. Being prime itself, it is possible
|
The number 3797 has an interesting property. Being prime itself, it is possible
|
||||||
to continuously remove digits from left to right, and remain prime at each stage:
|
to continuously remove digits from left to right, and remain prime at each stage:
|
||||||
3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
|
3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
|
||||||
|
@ -11,28 +14,46 @@ NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
seive = [True] * 1000001
|
import math
|
||||||
seive[1] = False
|
|
||||||
i = 2
|
|
||||||
while i * i <= 1000000:
|
|
||||||
if seive[i]:
|
|
||||||
for j in range(i * i, 1000001, i):
|
|
||||||
seive[j] = False
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
|
|
||||||
def is_prime(n: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Returns True if n is prime,
|
|
||||||
False otherwise, for 1 <= n <= 1000000
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
>>> is_prime(87)
|
|
||||||
|
>>> is_prime(0)
|
||||||
False
|
False
|
||||||
>>> is_prime(1)
|
>>> is_prime(1)
|
||||||
False
|
False
|
||||||
>>> is_prime(25363)
|
>>> is_prime(2)
|
||||||
|
True
|
||||||
|
>>> is_prime(3)
|
||||||
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
|
>>> is_prime(87)
|
||||||
|
False
|
||||||
|
>>> is_prime(563)
|
||||||
|
True
|
||||||
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
>>> is_prime(67483)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
return seive[n]
|
|
||||||
|
if 1 < number < 4:
|
||||||
|
# 2 and 3 are primes
|
||||||
|
return True
|
||||||
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
|
return False
|
||||||
|
|
||||||
|
# All primes number are in format of 6k +/- 1
|
||||||
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def list_truncated_nums(n: int) -> list[int]:
|
def list_truncated_nums(n: int) -> list[int]:
|
||||||
|
|
|
@ -12,25 +12,45 @@ pandigital prime.
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import math
|
||||||
from itertools import permutations
|
from itertools import permutations
|
||||||
from math import sqrt
|
|
||||||
|
|
||||||
|
|
||||||
def is_prime(n: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Returns True if n is prime,
|
|
||||||
False otherwise.
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
>>> is_prime(67483)
|
|
||||||
|
>>> is_prime(0)
|
||||||
|
False
|
||||||
|
>>> is_prime(1)
|
||||||
|
False
|
||||||
|
>>> is_prime(2)
|
||||||
|
True
|
||||||
|
>>> is_prime(3)
|
||||||
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
|
>>> is_prime(87)
|
||||||
False
|
False
|
||||||
>>> is_prime(563)
|
>>> is_prime(563)
|
||||||
True
|
True
|
||||||
>>> is_prime(87)
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
>>> is_prime(67483)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
if n % 2 == 0:
|
|
||||||
|
if 1 < number < 4:
|
||||||
|
# 2 and 3 are primes
|
||||||
|
return True
|
||||||
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
return False
|
return False
|
||||||
for i in range(3, int(sqrt(n) + 1), 2):
|
|
||||||
if n % i == 0:
|
# All primes number are in format of 6k +/- 1
|
||||||
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
@ -19,30 +19,49 @@ prime and twice a square?
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
seive = [True] * 100001
|
import math
|
||||||
i = 2
|
|
||||||
while i * i <= 100000:
|
|
||||||
if seive[i]:
|
|
||||||
for j in range(i * i, 100001, i):
|
|
||||||
seive[j] = False
|
|
||||||
i += 1
|
|
||||||
|
|
||||||
|
|
||||||
def is_prime(n: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Returns True if n is prime,
|
|
||||||
False otherwise, for 2 <= n <= 100000
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
|
|
||||||
|
>>> is_prime(0)
|
||||||
|
False
|
||||||
|
>>> is_prime(1)
|
||||||
|
False
|
||||||
|
>>> is_prime(2)
|
||||||
|
True
|
||||||
|
>>> is_prime(3)
|
||||||
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
>>> is_prime(87)
|
>>> is_prime(87)
|
||||||
False
|
False
|
||||||
>>> is_prime(23)
|
>>> is_prime(563)
|
||||||
True
|
True
|
||||||
>>> is_prime(25363)
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
>>> is_prime(67483)
|
||||||
False
|
False
|
||||||
"""
|
"""
|
||||||
return seive[n]
|
|
||||||
|
if 1 < number < 4:
|
||||||
|
# 2 and 3 are primes
|
||||||
|
return True
|
||||||
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
|
return False
|
||||||
|
|
||||||
|
# All primes number are in format of 6k +/- 1
|
||||||
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
odd_composites = [num for num in range(3, len(seive), 2) if not is_prime(num)]
|
odd_composites = [num for num in range(3, 100001, 2) if not is_prime(num)]
|
||||||
|
|
||||||
|
|
||||||
def compute_nums(n: int) -> list[int]:
|
def compute_nums(n: int) -> list[int]:
|
||||||
|
|
|
@ -25,32 +25,46 @@ After that, bruteforce all passed candidates sequences using
|
||||||
The bruteforce of this solution will be about 1 sec.
|
The bruteforce of this solution will be about 1 sec.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import math
|
||||||
from itertools import permutations
|
from itertools import permutations
|
||||||
from math import floor, sqrt
|
|
||||||
|
|
||||||
|
|
||||||
def is_prime(number: int) -> bool:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
function to check whether the number is prime or not.
|
|
||||||
>>> is_prime(2)
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
True
|
|
||||||
>>> is_prime(6)
|
>>> is_prime(0)
|
||||||
False
|
False
|
||||||
>>> is_prime(1)
|
>>> is_prime(1)
|
||||||
False
|
False
|
||||||
>>> is_prime(-800)
|
>>> is_prime(2)
|
||||||
False
|
|
||||||
>>> is_prime(104729)
|
|
||||||
True
|
True
|
||||||
|
>>> is_prime(3)
|
||||||
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
|
>>> is_prime(87)
|
||||||
|
False
|
||||||
|
>>> is_prime(563)
|
||||||
|
True
|
||||||
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
>>> is_prime(67483)
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if number < 2:
|
if 1 < number < 4:
|
||||||
|
# 2 and 3 are primes
|
||||||
|
return True
|
||||||
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for i in range(2, floor(sqrt(number)) + 1):
|
# All primes number are in format of 6k +/- 1
|
||||||
if number % i == 0:
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -33,29 +33,46 @@ So we check individually each one of these before incrementing our
|
||||||
count of current primes.
|
count of current primes.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
from math import isqrt
|
import math
|
||||||
|
|
||||||
|
|
||||||
def is_prime(number: int) -> int:
|
def is_prime(number: int) -> bool:
|
||||||
"""
|
"""Checks to see if a number is a prime in O(sqrt(n)).
|
||||||
Returns whether the given number is prime or not
|
|
||||||
|
A number is prime if it has exactly two factors: 1 and itself.
|
||||||
|
|
||||||
|
>>> is_prime(0)
|
||||||
|
False
|
||||||
>>> is_prime(1)
|
>>> is_prime(1)
|
||||||
0
|
False
|
||||||
>>> is_prime(17)
|
>>> is_prime(2)
|
||||||
1
|
True
|
||||||
>>> is_prime(10000)
|
>>> is_prime(3)
|
||||||
0
|
True
|
||||||
|
>>> is_prime(27)
|
||||||
|
False
|
||||||
|
>>> is_prime(87)
|
||||||
|
False
|
||||||
|
>>> is_prime(563)
|
||||||
|
True
|
||||||
|
>>> is_prime(2999)
|
||||||
|
True
|
||||||
|
>>> is_prime(67483)
|
||||||
|
False
|
||||||
"""
|
"""
|
||||||
if number == 1:
|
|
||||||
return 0
|
|
||||||
|
|
||||||
if number % 2 == 0 and number > 2:
|
if 1 < number < 4:
|
||||||
return 0
|
# 2 and 3 are primes
|
||||||
|
return True
|
||||||
|
elif number < 2 or number % 2 == 0 or number % 3 == 0:
|
||||||
|
# Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
|
||||||
|
return False
|
||||||
|
|
||||||
for i in range(3, isqrt(number) + 1, 2):
|
# All primes number are in format of 6k +/- 1
|
||||||
if number % i == 0:
|
for i in range(5, int(math.sqrt(number) + 1), 6):
|
||||||
return 0
|
if number % i == 0 or number % (i + 2) == 0:
|
||||||
return 1
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def solution(ratio: float = 0.1) -> int:
|
def solution(ratio: float = 0.1) -> int:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user