vendredi 15 octobre 2010

Have you ever seen the rain?

Euler #47

Euler #46

#!/usr/bin/env python

from libeuler import is_prime, primes
from math import sqrt

if __name__ == '__main__':
    P = primes(100000)
    n = 1
    while True:
        found = False
        for p in P[1:]:
            a2 = n - 0.5*(p - 1)
            if a2 < 0:
                break
            
            if sqrt(a2) == float(int(sqrt(a2))):
                print "%d -> %d + 2*%d^2" % (2*n + 1, p, sqrt(a2))
                found = True
                n += 1
                break

        if not found and not (2*n+1) in P:
            print 2*n + 1
            break

Euler #44

Euler #43

Euler #42

#!/usr/bin/env python

def word_value(w):
    return sum(map(lambda x: ord(x) - ord('A') + 1, w))

def triangle_numbers(l):
    res = []
    n = 1
    t = 1
    while t < l:
        t = n*(n+1)/2
        res += [t]
        n += 1
    return res

if __name__ == '__main__':
    f = open('words.txt', 'r')
    words = f.read()
    f.close()
    words = map(word_value, map(lambda x: x[1:-1], words.split(',')))
    triangles = triangle_numbers(max(words))
    n = 0
    for w in words:
        if w in triangles: n += 1
    print n
    

Euler #41

#!/usr/bin/env python

from libeuler import is_prime, pandigital

if __name__ == '__main__':
    res = []
    for i in xrange(1, 10):
        res += filter(lambda x: is_prime(x), pandigital(i))
    print max(res)

Euler #40

#!/usr/bin/env python

def create_fraction():
    f = ''
    cnt = 1
    while len(f) < 1000000:
        f += str(cnt)
        cnt += 1
    return f

if __name__ == '__main__':
    fraction = create_fraction()
    d1 = int(fraction[0])
    d10 = int(fraction[9])
    d100 = int(fraction[99])
    d1000 = int(fraction[999])
    d10000 = int(fraction[9999])
    d100000 = int(fraction[99999])
    d1000000 = int(fraction[999999])
    print d1 * d10 * d100 * d1000 * d10000 * d100000 * d1000000

Well, obviously we can use arrays and powers of 10

Euler #39

#!/usr/bin/env python

from math import sqrt

def n_triangles(p):
    res = []
    for a in xrange(1, p):
        for b in xrange(a, p):
            c = sqrt(a*a + b*b)
            if a + b + c == p:
                res += [(a, b, c)]
    return res

for p in xrange(3, 1000):
    t = n_triangles(p)
    if len(t) > 0:
        print p, len(t), t

Euler #38

#!/usr/bin/env python

from libeuler import is_pandigital

def concatenated_product(n, l):
    return reduce(lambda x, y: str(x) + str(y), map(lambda x: x*n, l), '')

if __name__ == '__main__':
    r = []
    n = 1
    m = 9
    while True:
        p = concatenated_product(n, range(1, m+1))
        print "%d -> %s" % (n, p), range(1, m+1)
        if len(p) > 9:
            m -= 1
            if m < 2:
                break
            continue
        if is_pandigital(int(p)):
            r += [int(p)]
        n += 1
    print max(r)

Euler #36

#!/usr/bin/env python

def is_palyndrome(s): return (s == reduce(lambda x,y: x+y, reversed(s), ''))

def right_number(n):
    c1 = is_palyndrome(str(n))
    c2 = is_palyndrome(bin(n)[2:])
    if c1 and c2:
        return n
    else:
        return 0
    
if __name__ == '__main__':
    print reduce(lambda x, y: x+y, map(right_number, range(1000000)), 0)