7 de agosto de 2012

Project Euler 26

#!/usr/bin/env python
#True if n is prime
#(wrote it like this 'cause
#of the blog's width.)
def p(n):
    if n==2:
        return True
    if n%2==0:
        return False
    m=int(n**.5)
    for i in xrange(3,m+1,2):
        if n%i==0:
            return False
    return True
#list of primes p, 1
#(used just primes because of this #http://goo.gl/YvdiU ) plst=[x for x in xrange(2,1001) if p(x)] #used the last two lines #of this http://goo.gl/r1tkn def periodo(p): for k in xrange(1,p): if (10**k-1)%p==0: return k lst={} #dictionary with this format: #{cyclelength:primenumber} for e in plst: lst[periodo(e)]=e print lst[max(lst)]