Python/data_structures/hashing/number_theory/prime_numbers.py

30 lines
678 B
Python
Raw Normal View History

2018-03-20 23:48:58 +00:00
#!/usr/bin/env python3
"""
module to operations with prime numbers
"""
def check_prime(number):
2019-10-05 05:14:13 +00:00
"""
it's not the best solution
"""
2019-10-05 05:14:13 +00:00
special_non_primes = [0, 1, 2]
if number in special_non_primes[:2]:
return 2
elif number == special_non_primes[-1]:
return 3
return all([number % i for i in range(2, number)])
2018-03-20 23:48:58 +00:00
def next_prime(value, factor=1, **kwargs):
value = factor * value
first_value_val = value
2019-10-05 05:14:13 +00:00
2018-03-20 23:48:58 +00:00
while not check_prime(value):
value += 1 if not ("desc" in kwargs.keys() and kwargs["desc"] is True) else -1
2019-10-05 05:14:13 +00:00
2018-03-20 23:48:58 +00:00
if value == first_value_val:
return next_prime(value + 1, **kwargs)
return value