Advertisement
informaticage

Semi efficient primality test in range python

May 2nd, 2021
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.43 KB | None | 0 0
  1. import math
  2. upper_bound = int(input("Upperbound: "))
  3.  
  4. primes = []
  5. for num in range(2, upper_bound + 1):
  6.     # Controlliamo se num รจ primo
  7.     is_prime = True
  8.     for divisor in range(2, int(math.sqrt(num)) + 1):
  9.         if(num % divisor == 0):
  10.             # Se sono qui anche solo una volta allora non ho un numero primo
  11.             is_prime = False
  12.             break
  13.     if(is_prime):
  14.         primes.append(num)
  15.  
  16. print(primes)
  17.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement