I am trying to get a function to determine if N is a prime number. I am new to Python and I know this may not be the most efficient way to solve this but this is my attempt
def is_prime(N):
k = [] #Creates a new list k
for i in range(2,N): #For each i from 2 -> N
r = N%i # r is the remainder of N % i
k.append(r) # appends each remainder r to list k
if (i == N-1): #Once index equals N-1, print list k
print(k)
#For each element j in list k, check if each element in list k is 0
for j in range (len(k)):
if k[j] != 0: <---PROBLEM
return True
else:
return False
print(is_prime(15))
The logic that I have is that when a number is divisible by 1 and itself, and not divisible by any other numbers from 2 to N-1, then it is a prime number. In the code above, I am having trouble comparing the values of each element in list k. I want to determine if the value of each element k[j] == 0. If each element k[j] != 0 and N%1 == 0 and N%N == 0, then it is a prime number!
Any ideas to how I can solve this problem? Please refer to the link below to get a visualization of my code! http://goo.gl/IVIRz7