示例#1
0
#!/usr/bin/env python

# Copyright (c) 2008 by Steingrim Dovland <*****@*****.**>

from euler import findfirst, trueforall

# 2520 is the smallest number that can be divided by each of the numbers
# from 1 to 10 without any remainder.
#
# What is the smallest number that is evenly divisible by all of the
# numbers from 1 to 20?

MAX = 1000*1000*1000

def is_magic(x, numbers):
    return trueforall(numbers, lambda e: x % e == 0)

print findfirst(xrange(20, MAX, 20), lambda x: is_magic(x, range(1, 21)))

示例#2
0
#!/usr/bin/env python

# Copyright (c) 2008 by Steingrim Dovland <*****@*****.**>

from itertools import count, izip
from euler import permutations, findfirst

# A permutation is an ordered arrangement of objects. For example, 3124
# is one possible permutation of the digits 1, 2, 3 and 4. If all of the
# permutations are listed numerically or alphabetically, we call it
# lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
#
# 012   021   102   120   201   210
#
# What is the millionth lexicographic permutation of the digits 0, 1, 2,
# 3, 4, 5, 6, 7, 8 and 9?

p = permutations('0123456789')
x = findfirst(izip(count(1), p), lambda x: x[0] == 1*1000*1000)
print ''.join(x[1])

示例#3
0
from itertools import count, izip
from euler import fib, findfirst

# The Fibonacci sequence is defined by the recurrence relation:
#
#     Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1.
#
# Hence the first 12 terms will be:
#
#     F1 = 1
#     F2 = 1
#     F3 = 2
#     F4 = 3
#     F5 = 5
#     F6 = 8
#     F7 = 13
#     F8 = 21
#     F9 = 34
#     F10 = 55
#     F11 = 89
#     F12 = 144
#
# The 12th term, F12, is the first term to contain three digits.
#
# What is the first term in the Fibonacci sequence to contain 1000 digits?

# we need to increase with two because fib() starts with 1 and 2, not 1 and 1
LEN = 1000
print findfirst(izip(count(), fib()), lambda x: len(str(x[1])) >= LEN)[0] + 1
示例#4
0
from euler import findfirst, permutations, NumberGenerator, OrderedGeneratorCache

# The cube, 41063625 (345^3), can be permuted to produce two other
# cubes: 56623104 (384^3) and 66430125 (405^3). In fact, 41063625 is the
# smallest cube which has exactly three permutations of its digits which
# are also cube.
#
# Find the smallest cube for which exactly five permutations of its
# digits are cube.

def int_permutations(x):
    return map(int, map(''.join, permutations(str(x))))

def count_cube_permutations(x):
    count = 0
    checked = set()
    for p in int_permutations(x):
        if p in checked: 
            continue
        checked.add(p)
        if cube_cache.cached(p):
            count += 1
    return count


cube_cache = OrderedGeneratorCache( x ** 3 for x in NumberGenerator(start=2) )
cube_generator = ( x ** 3 for x in NumberGenerator(start=2) )
cube_counter = ( (x, count_cube_permutations(x)) for x in cube_generator )

print findfirst(cube_counter, lambda t: t[1] == 5)
示例#5
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2008 by Steingrim Dovland <*****@*****.**>

from euler import findfirst, trueforall, NumberGenerator

# It can be seen that the number, 125874, and its double, 251748,
# contain exactly the same digits, but in a different order.
#
# Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and
# 6x, contain the same digits.


def has_same_digits(x, y):
    x, y = str(x), str(y)
    if len(x) != len(y):
        return False
    x, y = list(x), list(y)
    return sorted(x) == sorted(y)


def is_magic(n):
    numbers = [n * x for x in xrange(2, 7)]
    return trueforall(numbers, lambda x: has_same_digits(n, x))


print findfirst(NumberGenerator(start=2), is_magic)
示例#6
0
# Let us list the factors of the first seven triangle numbers:
#
#     1: 1
#     3: 1,3
#     6: 1,2,3,6
#    10: 1,2,5,10
#    15: 1,3,5,15
#    21: 1,3,7,21
#    28: 1,2,4,7,14,28
#
# We can see that the 7th triangle number, 28, is the first triangle
# number to have over five divisors.
#
# Which is the first triangle number to have over five-hundred divisors?

def triangle_numbers(upto=None):
    count = 1
    number = 1
    while not upto or upto > number:
        yield number
        count += 1
        number += count

def pe(x):
    d = DefaultDict(1) # set it to 1 so we don't have to map(+1) later
    for i in primefactors(x): d[i] += 1
    return d.values()

print findfirst(triangle_numbers(), lambda t: reduce(mul, pe(t), 1) > 500)