projekt
Test for primality.
This file consists of two methods for determination of primality
First is ''primes_sieve()'' Deterministic method of finding a prime number. Method is more is more suited for creating list of primes.
Second function ''is_prime'' also deterministic function is designed for faster operation when searching for primality of given number.
1""" 2Test for primality. 3 4This file consists of two methods for determination of primality 5 6First is ''primes_sieve()'' 7Deterministic method of finding a prime number. 8Method is more is more suited for creating list of primes. 9 10Second function ''is_prime'' 11also deterministic function 12is designed for faster operation when searching for primality of given number. 13""" 14 15import math 16 17 18def primes_sieve(limit: int) -> bool: 19 """ 20 Input variable vstup greater than 100 is tested for primality. 21 22 List of prime numbers is generated up to the upper limit 23 given by the parameter ``limit``. 24 Function then tests if ``limit`` is in the generated list. 25 26 :param limit: number that is tested for primality 27 :return: function returns true if parameter limit is prime number 28 """ 29 limitn = limit + 1 30 primes = [i for i in range(2, limitn)] 31 32 for i in primes: 33 factors = range(i, limitn, i) 34 for f in factors[1:]: 35 if f in primes: 36 primes.remove(f) 37 if limit in primes: 38 return True 39 else: 40 return False 41 42 43def is_prime(vstup: int) -> bool: 44 """ 45 Input variable vstup lower than 100 is tested for primality. 46 47 Function reduces number of modulators to save time. 48 Python math library needs to imported before deployment of this function. 49 50 :param vstup: number that is tested for primality 51 :return: function returns true if vstup is primal number 52 """ 53 if vstup <= 1: 54 return False 55 if vstup % 2 == 0: 56 return vstup == 2 57 58 max_div = math.floor(math.sqrt(vstup)) 59 for i in range(3, 1 + max_div, 2): 60 if vstup % i == 0: 61 return False 62 return True 63 64 65def main(vstup): 66 """Choose an algorithm for primality test.""" 67 if vstup >= 100: 68 if is_prime(vstup): 69 print("Your number is prime.") 70 else: 71 print("Your number isn't prime.") 72 elif 100 > vstup > 1: 73 if primes_sieve(vstup): 74 print("Your number is prime. Sieve method was used.") 75 else: 76 print("Your number isn't prime. Sieve method was used.") 77 else: 78 print("Please enter a positive number!") 79 80 81if __name__ == '__main__': 82 try: 83 main(int(input("Please enter a number: "))) 84 except ValueError: 85 print("Please enter a positive number!")
19def primes_sieve(limit: int) -> bool: 20 """ 21 Input variable vstup greater than 100 is tested for primality. 22 23 List of prime numbers is generated up to the upper limit 24 given by the parameter ``limit``. 25 Function then tests if ``limit`` is in the generated list. 26 27 :param limit: number that is tested for primality 28 :return: function returns true if parameter limit is prime number 29 """ 30 limitn = limit + 1 31 primes = [i for i in range(2, limitn)] 32 33 for i in primes: 34 factors = range(i, limitn, i) 35 for f in factors[1:]: 36 if f in primes: 37 primes.remove(f) 38 if limit in primes: 39 return True 40 else: 41 return False
Input variable vstup greater than 100 is tested for primality.
List of prime numbers is generated up to the upper limit
given by the parameter limit.
Function then tests if limit is in the generated list.
Parameters
- limit: number that is tested for primality
Returns
function returns true if parameter limit is prime number
44def is_prime(vstup: int) -> bool: 45 """ 46 Input variable vstup lower than 100 is tested for primality. 47 48 Function reduces number of modulators to save time. 49 Python math library needs to imported before deployment of this function. 50 51 :param vstup: number that is tested for primality 52 :return: function returns true if vstup is primal number 53 """ 54 if vstup <= 1: 55 return False 56 if vstup % 2 == 0: 57 return vstup == 2 58 59 max_div = math.floor(math.sqrt(vstup)) 60 for i in range(3, 1 + max_div, 2): 61 if vstup % i == 0: 62 return False 63 return True
Input variable vstup lower than 100 is tested for primality.
Function reduces number of modulators to save time. Python math library needs to imported before deployment of this function.
Parameters
- vstup: number that is tested for primality
Returns
function returns true if vstup is primal number
66def main(vstup): 67 """Choose an algorithm for primality test.""" 68 if vstup >= 100: 69 if is_prime(vstup): 70 print("Your number is prime.") 71 else: 72 print("Your number isn't prime.") 73 elif 100 > vstup > 1: 74 if primes_sieve(vstup): 75 print("Your number is prime. Sieve method was used.") 76 else: 77 print("Your number isn't prime. Sieve method was used.") 78 else: 79 print("Please enter a positive number!")
Choose an algorithm for primality test.