Advertisement
makispaiktis

Problem 2 - Prime between perfect squares

Jul 5th, 2021 (edited)
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. # Function 1 - Check the existence of a prime between the 2 arguments
  2. def checkPrimeExistence(n1, n2):
  3.     MAX = max(n1, n2)
  4.     MIN = min(n1, n2)
  5.     exists = False
  6.     solution = 0
  7.     for n in range(MIN, MAX+1):
  8.         isPrime = True
  9.         for i in range(2, int(n/2)):
  10.             if n % i == 0:
  11.                 isPrime = False
  12.                 break
  13.         if isPrime:
  14.             exists = True
  15.             solution = n
  16.     if exists:
  17.         print(str(MIN) + " - " + str(MAX) + " ----> " + str(solution))
  18.         return True
  19.     else:
  20.         print("**********************************************************")
  21.         print("There is no prime between " + str(MIN) + " and " + str(MAX))
  22.         print("**********************************************************")
  23.         return False
  24.  
  25.  
  26. # MAIN FUNCTION
  27. limit = 300
  28. for i in range(2, limit):
  29.     n1 = i ** 2
  30.     n2 = (i+1) ** 2
  31.     flag = checkPrimeExistence(n1, n2)
  32.     if flag == False:
  33.         # This means that there is no prime between the 2 limits
  34.         print("Finally, there is no prime between " + str(n1) + "^2 and " + str(n2) + "^2")
  35.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement