In: Computer Science
Write a Python program that print out the list of couples of prime numbers that are less than 50, but their sum is bigger than 40. For instance(29,13)or(37,17),etc. Your program should print all couples
Have a look at the below code. I have put comments wherever required for better understanding.
# function to check if a number is prime or not
def is_prime(n):
for i in range(2, n):
if n % i == 0:
return False
return True
# create an empty array to store prime numbers
primes = []
# find all prime number is range of 1,50
for i in range(2,51):
if is_prime(i):
primes.append(i)
# create an array to store couple primes
couples = []
n = len(primes)
# run an nested loop to find all pairs
for i in range(n):
for j in range(i+1,n):
# check if sum is greater tha 40
if ((primes[i]+primes[j])>40):
# if true then add the pair to the list
couples.append((primes[i],primes[j]))
# display the list
print(couples)
Happy Learning!