コード例 #1
0
ファイル: 012.py プロジェクト: hooloong/project-euler-1
def func(n):
    global primes, sieves, SIZE, PRIME_CNT
    factors = 1
    while n % 2 == 0:
        factors += 1
        n /= 2
    if factors >= 2: factors -= 1
    isqrtn = isqrt(n)
    while isqrtn >= PRIME_CNT:
        SIZE = 10 * SIZE
        primes, sieves = get_primes_by_sieve(SIZE)
        PRIME_CNT = len(primes)
    for i in range(1, isqrtn):
        cnt = 1
        p = primes[i]
        while n % p == 0:
            cnt += 1
            n /= p
        factors *= cnt
        if n == 1: break
    return factors
コード例 #2
0
ファイル: 012.py プロジェクト: hooloong/project-euler-1
def func(n):
    global primes, sieves, SIZE, PRIME_CNT
    factors = 1
    while n % 2 == 0:
        factors += 1
        n /= 2
    if factors >= 2: factors -= 1
    isqrtn = isqrt(n)
    while isqrtn >= PRIME_CNT:
        SIZE = 10*SIZE
        primes, sieves = get_primes_by_sieve(SIZE)
        PRIME_CNT = len(primes)
    for i in range(1,isqrtn):
        cnt = 1
        p = primes[i]
        while n % p == 0:
            cnt += 1
            n /= p
        factors *= cnt
        if n == 1: break
    return factors
コード例 #3
0
ファイル: problem146.py プロジェクト: paulmcquad/projecteuler
#What is the sum of all such integers n below 150 million?

#Answer:
#676333270
#https://github.com/skydark/project-euler/blob/master/146.py
from time import time
t = time()
from mathplus import get_primes_by_sieve, chain

M = 150000000  #1000000
PM = M
dp = (1, 3, 7, 9, 13, 27)
ndp = (19, 21)
test = [10, 80, 130, 200]
circle = 210
P, _ = get_primes_by_sieve(PM)
testset = P[
    4:
    50]  #(11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 67, 71, 73, 79, 83, 89, 97)
#print time()-t


def is_prime2(n):
    for p in P:
        if p > n: break
        n2 = (n % p)**2
        for d in dp:
            if (n2 + d) % p == 0: return False
    n2 = n * n
    for d in ndp:
        nn = n2 + d
コード例 #4
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way:

#28 = 22 + 23 + 24
#33 = 32 + 23 + 24
#49 = 52 + 23 + 24
#47 = 22 + 33 + 24

#How many numbers below fifty million can be expressed as the sum of a prime square, prime cube, and prime fourth power?

#Answer:
	#1097343

from time import time; t=time()
from mathplus import get_primes_by_sieve, isqrt, pow

M = 50000000

sqrtM = isqrt(M)
primes, _ = get_primes_by_sieve(sqrtM)
p2 = [p*p for p in primes]
p3 = [p**3 for p in primes[:int(pow(M, 1.0/3))]]
p4 = [p**4 for p in primes[:isqrt(sqrtM)+1]]
pool = set(p+q for p in p3 for q in p4 if p+q < M)
pool = set(p+q for p in pool for q in p2 if p+q < M)
print(len(pool))#, time()-t
コード例 #5
0
ファイル: 058.py プロジェクト: hooloong/project-euler-1
#41 20  7  8  9 10 27
#42 21 22 23 24 25 26
#43 44 45 46 47 48 49

#It is interesting to note that the odd squares lie along the bottom right diagonal, but what is more interesting is that 8 out of the 13 numbers lying along both diagonals are prime; that is, a ratio of 8/13  62%.

#If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?

#Answer:
#26241

#from time import time; t=time()
from mathplus import isqrt, sieve, get_primes_by_sieve

M = 23000
primes, sieves = get_primes_by_sieve(M, odd_only=True)
max_prime = primes[-1]


def is_prime(n):
    if n <= M: return sieves[n]
    for i in primes:
        if n % i == 0: return False
    for i in range(max_prime + 2, isqrt(n), 2):
        if n % i == 0: return False
    return True


a, b = 0, 1
n2 = 1
double_n = 2
コード例 #6
0
ファイル: problem233.py プロジェクト: paulmcquad/projecteuler
def pe233():
    # Unreadable... = =||| #FML
    primes, _ = get_primes_by_sieve(N // (5**3 * 13**2) + 1, odd_only=True)
    p4k1 = []
    p4k3 = []
    for p in primes:
        if p % 4 == 1:
            p4k1.append(p)
        else:
            p4k3.append(p)
    len_c = N // (5**3 * 13**2 * 17) + 1
    choices = [False] * len_c
    choices[1] = True
    for p in p4k3:
        if p >= len_c: break
        choices[p] = True
    for i in range(3, len_c, 2):
        if choices[i]: continue
        l = isqrt(i)
        for p in primes:
            if p > l:
                break
            if i % p == 0:
                if p % 4 == 3:
                    choices[i] = choices[i // p]
                break
    for i in range(1, (len_c + 1) // 2):
        choices[i * 2] = choices[i]
    cs = [0] * len_c
    ss = 0
    for i, f in enumerate(choices):
        if f: ss += i
        cs[i] = ss
    s = 0
    Ndd = N // (5 * 5 * 13)
    Nd5 = N // 5
    for p1 in p4k1:
        k = p1**3
        if k > Ndd: break
        for p2 in p4k1:
            if p2 == p1: continue
            k2 = k * p2**2
            if k2 > Nd5: break
            for p3 in p4k1:
                if p3 == p1 or p3 == p2: continue
                k3 = k2 * p3
                if k3 > N: break
                s += k3 * cs[N // k3]
    for p1 in p4k1:
        k = p1**7
        if k > Nd5: break
        for p2 in p4k1:
            if p2 == p1: continue
            k2 = k * p2**3
            if k2 > N: break
            s += k2 * cs[N // k2]
    for p1 in p4k1:
        k = p1**10
        if k > Nd5: break
        for p2 in p4k1:
            if p2 == p1: continue
            k2 = k * p2**2
            if k2 > N: break
            s += k2 * cs[N // k2]
    return s
コード例 #7
0
ファイル: 070.py プロジェクト: hooloong/project-euler-1
#Interestingly, φ(87109)=79180, and it can be seen that 87109 is a permutation of 79180.

#Find the value of n, 1 < n < 107, for which φ(n) is a permutation of n and the ratio n/φ(n) produces a minimum.

#Answer:
	#8319823

from time import time; t=time()
#import psyco; psyco.full()

#from gen_primes import get_primes
from mathplus import get_primes_by_sieve

M = 10000000
primes, _ = get_primes_by_sieve(M, odd_only=True)
#primes = get_primes(M, odd_only=True)
#print time()-t
phis = list(range(M))
for i in range(2, M, 2):
    phis[i] = 0
for p in primes:
    if p >= M: break
    for i in range(p, M, p):
        phis[i] -= phis[i]//p

#print time()-t
def is_permutation(x, y):
    sx, sy = str(x), str(y)
    return len(sx) == len(sy) and ''.join(sorted(sx)) == ''.join(sorted(sy))
コード例 #8
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#Let pn be the nth prime: 2, 3, 5, 7, 11, ..., and let r be the remainder when (pn−1)n + (pn+1)n is divided by pn2.

#For example, when n = 3, p3 = 5, and 43 + 63 = 280 ≡ 5 mod 25.

#The least value of n for which the remainder first exceeds 109 is 7037.

#Find the least value of n for which the remainder first exceeds 1010.

#Answer:
#21035
#https://github.com/skydark/project-euler/blob/master/123.py
from time import time
t = time()
from mathplus import get_primes_by_sieve, takewhile

M = 10**10
L = 1000000

p, sieves = get_primes_by_sieve(L)

for n in takewhile(lambda x: 2 * x * p[x - 1] <= M, range(7037, L, 2)):
    pass

print(n + 2)  #, time()-t
コード例 #9
0
ファイル: 012.py プロジェクト: hooloong/project-euler-1
#We can see that 28 is the first triangle number to have over five divisors.

#What is the value of the first triangle number to have over five hundred divisors?

#Answer:
#76576500

from time import time
t = time()
from mathplus import get_primes_by_sieve, isqrt
#from mathplus import factorization, reduce, op

LEAST = 500

SIZE = 100
primes, sieves = get_primes_by_sieve(SIZE)
PRIME_CNT = len(primes)


def func(n):
    global primes, sieves, SIZE, PRIME_CNT
    factors = 1
    while n % 2 == 0:
        factors += 1
        n /= 2
    if factors >= 2: factors -= 1
    isqrtn = isqrt(n)
    while isqrtn >= PRIME_CNT:
        SIZE = 10 * SIZE
        primes, sieves = get_primes_by_sieve(SIZE)
        PRIME_CNT = len(primes)
コード例 #10
0
ファイル: 012.py プロジェクト: hooloong/project-euler-1
#We can see that 28 is the first triangle number to have over five divisors.

#What is the value of the first triangle number to have over five hundred divisors?

#Answer:
	#76576500

from time import time; t=time()
from mathplus import get_primes_by_sieve, isqrt
#from mathplus import factorization, reduce, op

LEAST = 500

SIZE = 100
primes, sieves = get_primes_by_sieve(SIZE)
PRIME_CNT = len(primes)

def func(n):
    global primes, sieves, SIZE, PRIME_CNT
    factors = 1
    while n % 2 == 0:
        factors += 1
        n /= 2
    if factors >= 2: factors -= 1
    isqrtn = isqrt(n)
    while isqrtn >= PRIME_CNT:
        SIZE = 10*SIZE
        primes, sieves = get_primes_by_sieve(SIZE)
        PRIME_CNT = len(primes)
    for i in range(1,isqrtn):
コード例 #11
0
ファイル: 060.py プロジェクト: hooloong/project-euler-1
#!/usr/bin/python
# -*- coding: utf-8 -*-

#The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.

#Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.

#Answer:
	#26033

from time import time; t=time()
from mathplus import isqrt, log10, get_primes_by_sieve, dropwhile

L = 10000

primes, sieves = get_primes_by_sieve(L, odd_only=True)
max_prime = primes[-1]

def is_prime(n):
    if n <= L: return sieves[n]
    for i in primes:
        if n % i == 0: return False
    for i in range(max_prime+2, isqrt(n), 2):
        if n % i == 0: return False
    return True

def concat(i, j):
    return 10**(int(log10(j))+1)*i+j

s = L * 5
checks = {}
コード例 #12
0
ファイル: problem233.py プロジェクト: paulmcquad/projecteuler
def pe233():
    # Unreadable... = =||| #FML
    primes, _ = get_primes_by_sieve(N//(5**3*13**2)+1, odd_only=True)
    p4k1 = []
    p4k3 = []
    for p in primes:
        if p % 4 == 1:
            p4k1.append(p)
        else:
            p4k3.append(p)
    len_c = N // (5**3*13**2*17) + 1
    choices = [False] * len_c
    choices[1] = True
    for p in p4k3:
        if p >= len_c: break
        choices[p] = True
    for i in range(3, len_c, 2):
        if choices[i]: continue
        l = isqrt(i)
        for p in primes:
            if p > l:
                break
            if i % p == 0:
                if p % 4 == 3:
                    choices[i] = choices[i//p]
                break
    for i in range(1, (len_c+1)//2):
        choices[i*2] = choices[i]
    cs = [0] * len_c
    ss = 0
    for i, f in enumerate(choices):
        if f: ss += i
        cs[i] = ss
    s = 0
    Ndd = N // (5*5*13)
    Nd5 = N // 5
    for p1 in p4k1:
        k = p1 ** 3
        if k > Ndd: break
        for p2 in p4k1:
            if p2 == p1: continue
            k2 = k * p2 ** 2
            if k2 > Nd5: break
            for p3 in p4k1:
                if p3 == p1 or p3 == p2: continue
                k3 = k2 * p3
                if k3 > N: break
                s += k3 * cs[N//k3]
    for p1 in p4k1:
        k = p1 ** 7
        if k > Nd5: break
        for p2 in p4k1:
            if p2 == p1: continue
            k2 = k * p2**3
            if k2 > N: break
            s += k2 * cs[N//k2]
    for p1 in p4k1:
        k = p1 ** 10
        if k > Nd5: break
        for p2 in p4k1:
            if p2 == p1: continue
            k2 = k * p2**2
            if k2 > N: break
            s += k2 * cs[N//k2]
    return s
コード例 #13
0
ファイル: 128.py プロジェクト: hooloong/project-euler-1
#If all of the tiles for which PD(n) = 3 are listed in ascending order to form a sequence, the 10th tile would be 271.

#Find the 2000th tile in this sequence.

#Answer:
	#14516824220

from time import time; t=time()
from mathplus import get_primes_by_sieve

# 3n(n-1)+2, where 6n-1, 6n+1, 12n+5 in primes

N = 2000
M = 1000000

_, sieves = get_primes_by_sieve(M)

s, r = 1, 0
for n in range(2, (M-5)//12):
    if sieves[6*n-7] and sieves[6*n-1] and sieves[12*n-19]:
        s += 1
        if s == N:
            r = 3*n*(n-1)+1
            break
    if sieves[6*n-1] and sieves[6*n+1] and sieves[12*n+5]:
        s += 1
        if s == N:
            r = 3*n*(n-1)+2
            break

print(r)#, time()-t)
コード例 #14
0
ファイル: 146.py プロジェクト: hooloong/project-euler-1
#What is the sum of all such integers n below 150 million?

#Answer:
	#676333270

from time import time; t=time()
from mathplus import get_primes_by_sieve, chain

M = 150000000#1000000
PM = M
dp = (1, 3, 7, 9, 13, 27)
ndp = (19, 21)
test = [10, 80, 130, 200]
circle = 210
P, _ = get_primes_by_sieve(PM)
testset = P[4:50]#(11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 67, 71, 73, 79, 83, 89, 97)
#print time()-t

def is_prime2(n):
    for p in P:
        if p > n: break
        n2 = (n % p)**2
        for d in dp:
            if (n2+d) % p == 0: return False
    n2 = n*n
    for d in ndp:
        nn = n2+d
        flag = False
        for p in chain((3,7), testset, P):
            if p > n: break # <FIXME> nn = 10**2+21
コード例 #15
0
ファイル: problem134.py プロジェクト: paulmcquad/projecteuler
#Consider the consecutive primes p1 = 19 and p2 = 23. It can be verified that 1219 is the smallest number such that the last digits are formed by p1 whilst also being divisible by p2.

#In fact, with the exception of p1 = 3 and p2 = 5, for every pair of consecutive primes, p2 > p1, there exist values of n for which the last digits are formed by p1 and n is divisible by p2. Let S be the smallest of these values of n.

#Find ∑ S for every pair of consecutive primes with 5 ≤ p1 ≤ 1000000.

#Answer:
	#18613426663617118
#https://github.com/skydark/project-euler/blob/master/134.py
from time import time; t=time()
from mathplus import log10, get_primes_by_sieve

M = 10**6+5

primes, _ = get_primes_by_sieve(M)

def f(n, m):
    #assert n > m
    if (n, m) == (1, 0): return 1, 0
    q, r = n//m, n%m
    a, b = f(m, r)
    return b, a-b*q

s = 0
for i in range(2, len(primes)-1):
    p, q = primes[i:i+2]
    n = 10**(int(log10(p))+1)
    a, b = f(q, n%q)
    s += (b*(q-p) % q)*n+p
コード例 #16
0
ファイル: problem132.py プロジェクト: paulmcquad/projecteuler
#For example, R(10) = 1111111111 = 11×41×271×9091, and the sum of these prime factors is 9414.

#Find the sum of the first forty prime factors of R(109).

#Answer:
	#843296
#https://github.com/skydark/project-euler/blob/master/132.py
from time import time; t=time()
from mathplus import pow_mod, get_primes_by_sieve

M = 10**9
C = 40

L = 200000

primes, sieves = get_primes_by_sieve(L)

def f(n, m, p):
    # assert p in primes
    return pow_mod(n, m % (p-1), p)

s = 0
c = C
for p in primes[3:]:
    if f(10, M, p) == 1:
        s += p
        c -= 1
        if c == 0: break
else:
    raise Exception('L is too low')
コード例 #17
0
ファイル: 243.py プロジェクト: karieiriks/project-euler
# Find the smallest denominator d, having a resilience R(d) < 15499/94744 .

# Answer:
# 892371480

from time import time

t = time()
from mathplus import get_primes_by_sieve, takewhile

A = 15499
B = 94744
M = 100

primes, sievs = get_primes_by_sieve(M)


def no_primes_larger_than(n, p):
    for pp in takewhile(lambda x: x <= p, primes):
        while n % pp == 0:
            n //= pp
    return n == 1


a = 1
b = 1
pool = []
for p in primes:
    a *= p - 1
    b *= p