## Your code should check if each number in the list is a prime number check_prime = [26, 39, 51, 53, 57, 79, 85] ## write your code here ## HINT: You can use the modulo operator to find a facto prime_numbers = [] counter_prime = 0 for prime in check_prime: if (prime % 2 == 0): print("{} is not a prime number".format(prime)) elif (prime % 3 == 0): print("{} is not a prime number".format(prime)) elif(prime % 5 == 0): print("{} is not a prime number".format(prime)) elif(prime % 7 == 0): print("{} is not a prime number".format(prime)) else: prime_numbers.append(prime) print("{} is a prime number".format(prime)) counter_prime += 1 print("These are the prime numbers : {}".format(prime_numbers)) ## Altenatively, check_prime = [26, 39, 51, 53, 57, 79, 85] # iterate through the check_prime list for num in check_prime: # search for factors, iterating through numbers ranging from 2 to the number itself for i in range(2, num): # number is not prime if modulo is 0 if (num % i) == 0: print("{} is NOT a prime number, because {} is a factor of {}".format(num, i, num)) break # otherwise keep checking until we've searched all possible factors, and then declare it prime if i == num -1: print("{} IS a prime number".format(num))
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter