[Python] numeri primi
Mauro Casini
mauro a iperbole.bologna.it
Mar 2 Ago 2011 21:15:43 CEST
Marco Beri <marcoberi a gmail.com> writes:
> Usando un algoritmo scritto (in Python) da un amico:
>
>>>> timeit.timeit("import km;km.sieve(1000000)", number=1)
> 0.12970614433288574
>>>> timeit.timeit("import km;km.sieve(10000000)", number=1)
> 1.3863430023193359
>>>> timeit.timeit("import km;km.sieve(100000000)", number=1)
> 14.972478151321411
>
> Bravo il mio amico, eh? :-)
Si puņ fare di meglio:
In [25]: timeit.timeit('num.primes(1000000)', 'import num', number=1)
Out[25]: 1.2874603271484375e-05
In [26]: timeit.timeit('num.primes(1000000000)', 'import num', number=1)
Out[26]: 1.3113021850585938e-05
In [27]: timeit.timeit('num.primes(1000000000000)', 'import num', number=1)
Out[27]: 1.1920928955078125e-05
In [28]: timeit.timeit('num.primes(1000000000000000)', 'import num', number=1)
Out[28]: 1.4066696166992188e-05
tutto scritto in python e perfettamente funzionante :)
.
.
.
.
In [29]: num.primes(1000000)
Out[29]: <generator object primes at 0x4cb9dfa0>
In [31]: timeit.timeit('list(num.primes(1000000))', 'import num', number=1)
Out[31]: 1.0322129726409912
In [32]: timeit.timeit('list(num.primes(10000000))', 'import num', number=1)
Out[32]: 4.1961090564727783
In [33]: timeit.timeit('list(num.primes(100000000))', 'import num', number=1)
Out[33]: 35.926841974258423
ciao,
Mauro
Maggiori informazioni sulla lista
Python