So finden Sie Primzahlen in PythonPython

Python-Programme
Anonymous
 So finden Sie Primzahlen in Python

Post 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!

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post