elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


  Mostrar Mensajes
Páginas: [1]
1  Programación / Scripting / Re: Funcion isprime() [Python] en: 12 Junio 2009, 13:49 pm
mi versión:
Código:
import time, math

def calcularPrimos(tiempo):
    primos = [2]
    start = now = time.time()
    n = 3
    while (now-start) < tiempo :
        es_primo = True
        raiz = math.sqrt(n)
        for p in primos:
            if p > raiz :
                break
            if (n % p) == 0 :
                es_primo = False
                break
        if es_primo:
            primos.append(n)
        now = time.time()
        n += 2
    return primos

pr = calcularPrimos(5) #calculamos primos durante 5 segundos
print len(pr)

89684 primos en 5 segundos. En linux con firefox abierto.

Edit: por cierto, como se hace para que sombree la sintaxis
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines