For inputting 7:
"n = 7
(7, False, 2.4080276489257812e-05)
"Oops, try again. Your function fails on is_prime(7). It returns False when it should return True."
Every number above 5 seems to be returning false.
I was running this code in Thonny, and as I was in debug mode, stepping through each line, Thonny crashed when it gets to the filter. I don't know what's wrong. I want the filter to step through every number between 1 and x, such that the number is not divisible by 2, 3, or 5.
Note: I am currently on Classes in the CodeAcademy course. However, I was working on Project Euler Questions and I wanted to make a good program that checks if a number is prime quickly. I am trying to do it quickly by eliminating all multiples of 2, 3, and 5.
import time ##This function takes in a number ##If the number is prime, output is True ##If the number is composite, output is False def is_prime(x): str_x = str(x) list_str_x = list(str_x) list_x = [int(i) for i in list_str_x] #checks multiples of 2 if x % 2 == 0: if x != 2: return False else: return True #checks multiples of 3 elif x % 3 == 0: if x != 3: return False else: return True #checks multiples of 5 elif x % 5 == 0: if x != 5: return False else: return True #checks numbers above 5 elif x > 5: for i in filter(lambda j: (j % 2 != 0) and (j % 3 != 0) and (j % 5 != 0), range(1,x)): if x % i == 0: return False else: return True #checks 1, 0, and negatives elif x < 2: return False x = int(input("n = ")) t1 = time.time() ans = is_prime(x) t2 = time.time() print(x, ans, t2 - t1)