0
Why does this code only print true when i = 4, 9, 25, or 49?
def is_prime(x): IsPrime = True if x <= 1: IsPrime = False for i in range(2,x): if x % i == 0: IsPrime = False if IsPrime == False: return(False) else: return(True) def primes_range(x): return([i for i in range(x + 1) if is_prime(i) == True]) def facts(x): lst = [] for i in range(1,x + 1): for j in range(1,x + 1): if x / i == j: lst.append(i) return lst def prime_facts(x): b = primes_range(x) c = facts(x) d = [i for i in c if i in b] return(d) def is_semiprime(x): b = prime_facts(x) for i in b: for j in b: if i * j == x: return(True) else: return(False) for i in range(100): print(i, is_semiprime(i))
2 Answers
+ 1
def facts(x):
lst = []
for i in range(1,x + 1):
if x%i == 0:
lst.append(i)
return lst
def is_semiprime(x):
b = prime_facts(x)
for i in b:
for j in b:
if i * j == x:
return True
return False
P.S. This is one of my challenges, so tag me when you post your code.