ahojte mam taky problemik program mi nevie urcit ci je alebo nieje prvocislo.
mam promennu kde ja zvysky
vysledok1=[]
....
a potom mam
if '0' not in vysledok1:
print "Cislo",N,"je prvocislo"
elif '0' in vysledok1:
print "Cislo",N,"nieje prvocislo"
Fórum › Python
Prvocisla

To myky: Nějak jsem nepochopil co přesně chceš, ale jsetli je to algoritmus na zjisteni prvočísla tka ten jsme zrovna včera vymyslel :)
def prv(cislo):
"""
Vrací hodnotu True pokud je argument prvočíslem.
"""
cislo = abs(cislo)
for i in range(2,cislo-1):
if cislo % i == 0:
return False
return True
Tirio
moje skripty v Pythonu najdete na http://www.beta.tym.cz/index.php?n=Py.Skripty
moje skripty v Pythonu najdete na http://www.beta.tym.cz/index.php?n=Py.Skripty
Stačilo by testovat zda je číslo dělitelné nějakým jiným prvočíslem, ale to se mi nepovedlo vyřešit :(
Tirio
moje skripty v Pythonu najdete na http://www.beta.tym.cz/index.php?n=Py.Skripty
moje skripty v Pythonu najdete na http://www.beta.tym.cz/index.php?n=Py.Skripty
def je_prvocislo(cislo):
if (cislo%2) == 0 and not (cislo==2 or cislo ==-2):
return (False, 2)
elif (cislo%3) == 0 and not (cislo==3 or cislo ==-3):
return (False, 3)
for i in range(4,cislo/2):
if (cislo%i)==0:
return (False, i)
return (True, 1)
def prvocisla(do, od=1):
vysledek = list()
for i in range(od,do):
je, cim = je_prvocislo(i)
if je :
vysledek.append(i)
if len(vysledek)==0:
return False
else:
return vysledek
def prvo_rozklad(cislo, l=[]):
(je, cim) = je_prvocislo(cislo)
if je:
l.append(cislo)
return l
else:
p1 = cislo/cim
l.append(cim)
return prvo_rozklad(p1, l)
Nedávno sem se nudil, tak sem napsal tohle ... a je tam i rozklad na prvočísla ... ale k tomu jsem musel použít rekurzivní funkci, protože jsem si nemohl vzpomenout, jestli má python něco jako while a nechtělo se mně to hledat.

Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 31 hostů
Podobná vlákna
Kod prvocisla — založil robin
Prvocisla v intervalu — založil zemlra
Prvočísla z matice — založil Lukáš
Hledani prvocisla — založil loleklel
Prvočísla -Pascal — založil El97za
Moderátoři diskuze