Exemple #1
0
"""Project Euler problem 27"""
from euler import get_plist

class Answer:
    def __init__(self):
        self.a = 0
        self.b = 0
    def setval(self, a,b):
        self.a = a
        self.b = b
    def get(self):
        return self.a*self.b

def quad(n, a, b):
    return n*n + a*n + b

longest = 0
ans = Answer()
primes = set(get_plist(100000))
for a in range(-1000, 1001):
    if a%2==0: continue
    for b in range(-1000, 1001):
        if not abs(b) in primes: continue
        n = 0
        while quad(n, a, b) in primes:
            n += 1
        if n > longest:
            longest = n
            ans.setval(a,b)
print("Answer: " + str(ans.get()))
Exemple #2
0
# -*- coding:utf-8 -*-
"""Project Euler problem 46"""

from euler import get_plist, is_prime

mx = 10**6
primes = set(get_plist(mx))
ans = 0
for odd in range(9, mx, 2):
    if is_prime(odd, primes):
        continue
    flg = True

    for j in [i * i * 2 for i in range(int((mx**0.5) / 2) + 1)]:
        if j >= odd: break

        if is_prime(odd - j, primes):
            flg = False
            break
    if flg:
        ans = odd
        break
print("Answer: " + str(ans))
Exemple #3
0
# -*- coding:utf-8 -*-
"""Project Euler problem 10"""
from euler import get_plist

sm = 0
primes = get_plist(2000000)

for i in primes:
    sm += i

print("Answer: " + str(sm))
Exemple #4
0
# -*- coding:utf-8 -*-
"""Project Euler problem 7"""

from euler import get_plist

target = 10000
l = 10**6
while True:
    primes = get_plist(l)
    if len(primes)>=target: break
print("Answer: " + str(primes[target]))
Exemple #5
0
# -*- coding:utf-8 -*-
"""Project Euler problem 51"""

from euler import get_plist

primes = get_plist(10**6)
primesset = set(primes)
avail = []
for prime in primes:
    s = [int(d) for d in str(prime)]
    for i in range(10):
        if s.count(i) == 3:
            avail.append((prime, i))
for p in avail:
    c = 0
    for i in range(10):
        t = str(p[0]).replace(str(p[1]), str(i))
        if str(int(t))!=t:
            continue
        if int(t) in primesset:
            c+=1
            if c==8:
                print("Answer: " + str(p[0]))
                quit()
Exemple #6
0
# -*- coding:utf-8 -*-
"""Project Euler problem 37"""

from euler import get_plist

primes = get_plist(1000000)
primesset = set(primes)
sm = 0
for p in primes:
    if p<10: continue
    for i in range(len(str(p))):
        if not int(str(p)[i:]) in primesset:
            break
    else:
        for i in range(len(str(p))-1):
            if not int(str(p)[:-i-1]) in primesset:
                       break
        else:
            sm += p
print("Answer: " + str(sm))
Exemple #7
0
from euler import get_plist
def getPrimeFactor(x, primes):
    pf = []
    mx = x/2+1
    for i in primes:
        if mx<i:
            break
        if x!=1:
            if x%i==0:
                x/=i
                pf.append(i)
    return pf

mx = 10**6
primes = get_plist(mx)
chk = 4
fst = 0
cnt = 0
for i in range(mx):
    pf = set(getPrimeFactor(i, primes))
    if len(pf)==chk:
        if cnt==0:
            fst = i
        cnt += 1
        if cnt==chk:
            break
    else:
        cnt = 0
print("Answer: " + str(fst))
Exemple #8
0
# -*- coding:utf-8 -*-
"""Project Euler problem 49"""

from itertools import permutations as p
from euler import get_plist

primes = set(get_plist(10000))
for i in range(1000, 10000):
    if i == 1487: continue
    fst = i
    snd = fst + 3330
    thd = snd + 3330
    if not fst in primes or not snd in primes or not thd in primes:
        continue
    pat = set(["".join(ls) for ls in p(str(fst))])
    if str(snd) in pat and str(thd) in pat:
        break
print("Answer: " + str(fst) + str(snd) + str(thd))
Exemple #9
0
# -*- coding: utf-8 -*-
"""Project Euler problem 243"""

from fractions import Fraction as frac
from itertools import count
from euler import get_plist


def R(d):
    return phi(d) / (d - 1)


primes = get_plist(100)
target = frac(15499, 94744)
d = 1
phi = 1
for idx, p in enumerate(primes):
    d *= p
    phi *= p - 1
    if frac(phi, d) < target: break
for i in count(1):
    if frac(phi * i, d * i - 1) < target:
        break
print("Answer: " + str(d * i))
Exemple #10
0
# -*- coding:utf-8 -*-
"""Project Euler problem 50"""
from euler import get_plist

target = 10**6
primes = get_plist(target)
primesset = set(primes)

mx, ans =  0, 0
for l in range(len(primes)):
    for r in range(l+1,len(primes)):
        sm = sum(primes[l:r+1])
        if sm>target: break
        length = (r+1)-l
        if sm in primesset and mx<length:
            ans = sm
            mx = length

print("Answer: " + str(ans))
Exemple #11
0
# -*- coding:utf-8 -*-
"""Project Euler problem 41"""
from euler import get_plist
from itertools import permutations

primes = set(get_plist(7654321))

for pat in reversed(
        sorted([
            int("".join(l)) for l in list(permutations([x for x in "1234567"]))
        ])):
    if pat in primes:
        print("Answer: " + str(pat))
        break
else:
    for pat in reversed(
            sorted([
                int("".join(l))
                for l in list(permutations([x for x in "1234"]))
            ])):
        if pat in primes:
            print("Answer: " + str(pat))
            break
Exemple #12
0
# -*- coding:utf-8 -*-
"""Project Euler problem 51"""

from euler import get_plist

primes = get_plist(10**6)
primesset = set(primes)
avail = []
for prime in primes:
    s = [int(d) for d in str(prime)]
    for i in range(10):
        if s.count(i) == 3:
            avail.append((prime, i))
for p in avail:
    c = 0
    for i in range(10):
        t = str(p[0]).replace(str(p[1]), str(i))
        if str(int(t)) != t:
            continue
        if int(t) in primesset:
            c += 1
            if c == 8:
                print("Answer: " + str(p[0]))
                quit()
Exemple #13
0
# -*- coding:utf-8 -*-
"""Project Euler problem 7"""

from euler import get_plist

target = 10000
l = 10**6
while True:
    primes = get_plist(l)
    if len(primes) >= target: break
print("Answer: " + str(primes[target]))
Exemple #14
0
# -*- coding:utf-8 -*-
"""Project Euler problem 46"""

from euler import get_plist, is_prime

mx = 10**6
primes = set(get_plist(mx))
ans = 0
for odd in range(9, mx, 2):
    if is_prime(odd, primes):
        continue
    flg = True

    for j in [i*i*2 for i in range(int((mx**0.5)/2)+1)]:
        if j>=odd: break

        if is_prime(odd-j,  primes):
            flg = False
            break
    if flg:
        ans = odd
        break
print("Answer: " + str(ans))
Exemple #15
0
def getPrimeFactor(x, primes):
    pf = []
    mx = x / 2 + 1
    for i in primes:
        if mx < i:
            break
        if x != 1:
            if x % i == 0:
                x /= i
                pf.append(i)
    return pf


mx = 10**6
primes = get_plist(mx)
chk = 4
fst = 0
cnt = 0
for i in range(mx):
    pf = set(getPrimeFactor(i, primes))
    if len(pf) == chk:
        if cnt == 0:
            fst = i
        cnt += 1
        if cnt == chk:
            break
    else:
        cnt = 0
print("Answer: " + str(fst))
Exemple #16
0
# -*- coding:utf-8 -*-
"""Project Euler problem 10"""
from euler import get_plist

sm = 0
primes = get_plist(2000000)

for i in primes:
	sm += i

print("Answer: " + str(sm))