示例#1
0
from euler import range_infinite

res=0
for i in range_infinite(1):
    for j in range_infinite(1):
        if len(str(j**i))==i:
            print j,"^",i,"=",j**i
            res+=1
        if len(str(j**i))>i:
            break
    if i>1000: break

print res

#49
示例#2
0
from euler import isPrime, range_infinite, primes

primes = primes(10**5)

for i in range_infinite(35, 2):
    if not isPrime(i):
        flag = True
        for p in primes:
            if p < i:
                found = False
                for j in range_infinite(1, 1):
                    if p + 2 * j * j > i:
                        break
                    elif p + 2 * j * j == i:
                        found = True
                        break
                if found:
                    break
            else:
                flag = False
                break
        if not flag:
            print i
            break

#5777
示例#3
0
from euler import range_infinite


def products(n, min_divisor=2):
    """Generate expressions of n as a product of ints >= min_divisor."""
    if n == 1:
        yield []
    for divisor in range(min_divisor, n + 1):
        if n % divisor == 0:
            for product in products(n // divisor, divisor):
                yield product + [divisor]


max = 12000
prodSums = {}

for i in range_infinite(4):
    for p in products(i):
        k = i + len(p) - sum(p)
        if k >= 2 and k <= max and k not in prodSums:
            prodSums[k] = i
    if len(prodSums) == max - 1:
        break

print sum(set(prodSums.values()))

#7587457
示例#4
0
"""
Analizando el problema, vemos que para una cuadricula mxn, tenemos n*m piezas diferentes,
de 1x1 hay n*m
de 1x2 hay (n-1)*m
de 1x3 hay (n-2)*m, etc

de 2x1 hay n*(m-1), etc

Osea que una cuadricula nxm tendra sum(sum(i*j)) con j de m a 1 e i de n a 1=(n(n+1)/2)*(m(m+1)/2)
"""
from euler import range_infinite


def countGrid(n, m):
    return (n * (n + 1) * m * (m + 1)) / 4


#difference,n,m
nearest = (10**9, 0, 0)

for n in range(1, 5000):
    for m in range_infinite(1):
        count = countGrid(n, m)
        if abs(2000000 - count) < nearest[0]:
            nearest = (abs(2000000 - count), n, m)
        elif count > 2000000:
            break

print nearest
print nearest[1] * nearest[2]
示例#5
0
from euler import range_infinite
from math import sqrt, ceil

partition = {0: 1}


def p(n):
    if n < 0:
        return 0
    elif n in partition:
        return partition[n]
    else:
        ret = 0
        r = range(int(ceil(-(sqrt(24 * n + 1) - 1) / 6)),
                  int((sqrt(24 * n + 1) + 1) / 6) + 1)
        r.remove(0)
        for k in r:
            ret += (-1)**(abs(k) - 1) * p(n - (k * (3 * k - 1)) / 2)
        ret = ret % 10**6
        if n not in partition:
            partition[n] = ret
        return ret


for i in range_infinite():
    if p(i) == 0:
        print i
        break

#55374
示例#6
0
from euler import range_infinite, isPrime

total = 1
primes = 0
for n in range_infinite(3, 2):
    total += 4
    if isPrime((n - 2) * (n - 2) + n - 1):
        primes += 1
    if isPrime((n - 1) * (n - 1) + 1):
        primes += 1
    if isPrime((n - 1) * (n - 1) + n):
        primes += 1

    if (primes + 0.0) / total < 0.1:
        print n
        break

#26241
示例#7
0
d1<=d2 <=> b<=a
d1<=d3 <=> c<=a

Fijamos a, que lo cojemos de una terna, b+c=p[1], y conseguimos el rango de b, sabiendo b<=a, c<=a, c=p[1]-b
p[1]-a<=b<=a

También b>0, que lo tenemos gracias a que las ternas están ordenadas y c>0 => b<=p[1]

Además no queremos cuboides repetidos, así que b<=c => b>=p[1]/2
"""

from euler import pythagorean_triplets, range_infinite
import itertools
from math import sqrt

for n in range_infinite(1800):
    trips1 = [(x[0], x[1], x[2]) for x in pythagorean_triplets(n * 10)
              if x[0] <= n and x[1] <= 2 * n]
    trips2 = [(x[1], x[0], x[2]) for x in pythagorean_triplets(n * 10)
              if x[1] <= n and x[0] <= 2 * n]

    trips = trips1 + trips2

    tot = 0
    for p in trips:
        a = p[0]
        tot += len(range(max(p[1] - a, int((p[1] + 1) / 2)), min(a + 1, p[1])))

    print n, tot
    if tot > 10**6:
        print n