'I'm trying to code a function that finds prime numbers. Some of the "for" loops that I included in the code don't seem to be working
I have been learning how to code with Python for more or less two or three weeks and I came across an exercise that required me to find the number of primes numbers that exist up to a given number. If anyone could help that would be great.
My code seems to be working for the first few prime numbers (it excludes from the list created 4 and 6) but then it stops.
I am new to coding, so this could be wrong, but I think that for some reason one, or even both, of the "for" loops stopped working.
This is my code:
def count_primes(num):
primes = [2, 3]
for i in range(4,num+1):
cond = True
for x in range(0,len(primes)-1):
if i%primes[x] == 0:
cond = False
elif i%primes[x] > 0:
cond = True
if cond == True:
primes.append(i)
pass
print(primes)
return len(primes)
count_primes(100)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
