Page 1 of 1

So finden Sie Primzahlen in Python

Posted: 16 May 2025, 19:15
by Anonymous
Ich bin neu in Python. Ich versuche, die Primzahlen in einem bestimmten Bereich zu zählen. Einige der Antworten, die Entwickler geteilt haben, sind wie folgt: < /p>
import math
def count_primes(num):
out = []

for i in range(3,num,2):
if all(i%j!=0 for j in range(3,int(math.sqrt(i))+1,2)):
out.append(i)

print(out)
< /code>
Ich habe ein solches geschrieben: < /p>
import math
def count_primes(num):
out = []
for i in range(3,num,2):
for j in range(3, int(math.sqrt(i))+1,2):
if i%j != 0:
out.append(i)
print(out)
< /code>
Aber es funktioniert nicht. Könnte mir bitte jemand helfen. Geschätzt!