コード例 #1
0
ファイル: problem070.py プロジェクト: 1tux/project_euler
def main():
    global primes
    primes = sieve(10**4)
    best = 5
    best_n = 1
    for p in primes:
        for q in primes:

            n = p * q
            if n > 10**7:
                break

            phi = (p - 1) * (q - 1)
            res = float(n) / phi
            if res < best:
                if sorted(str(phi)) == sorted(str(n)):
                    best_n = n
                    best = res

    print best_n, best
コード例 #2
0
ファイル: problem070.py プロジェクト: 1tux/project_euler
def main():
	global primes
	primes = sieve(10 ** 4)
	best = 5
	best_n = 1
	for p in primes:
		for q in primes:

			n = p * q
			if n > 10 ** 7:
				break

			phi = (p-1) * (q-1)
			res = float(n) / phi
			if res < best:
				if sorted(str(phi)) == sorted(str(n)):
					best_n = n
					best = res

	print best_n, best
コード例 #3
0
ファイル: problem077.py プロジェクト: 1tux/project_euler
cache = {}
from pe_utils import sieve

primes = sieve(10**4)


def solve(n, k=1):

    if n == 0:
        return 1
    if n < 0 or n < k:
        return 0
    if (n, k) not in cache:
        x = 0
        for i in primes:  #xrange(k, n+1):
            if i < k:
                continue
            if i > n + 1:
                break
            x += solve(n - i, i)
        cache[(n, k)] = x

    return cache[(n, k)]


res = 0
i = 0
while res <= 5000:
    i += 1
    res = solve(i)
コード例 #4
0
ファイル: problem060.py プロジェクト: 1tux/project_euler
from pe_utils import sieve, is_prime
import itertools

primes = sieve(8400)
primes_set = set(sieve(10**6))
set_size = 5
print "got primes"


def merge(a, b):
    return int(str(a) + str(b))


def all_primes(chain, p):
    for x in chain:
        if not is_prime(merge(x, p), primes_set) or not is_prime(
                merge(p, x), primes_set):
            return False
    return True


def make_chain(chain):
    if len(chain) == set_size:
        return chain
    for p in primes:
        if p > chain[-1] and all_prime(chain + [p]):
            new_chain = make_chain(chain + [p])
            if new_chain:
                return new_chain
    return False
コード例 #5
0
ファイル: problem047.py プロジェクト: 1tux/project_euler
from pe_utils import get_prime_factors, sieve2 as sieve

N = 10 ** 6
k = 3
primes = sieve(N)
a = get_prime_factors(1, primes)
b = get_prime_factors(2, primes)
c = get_prime_factors(3, primes)

for i in xrange(4, N):
	d = get_prime_factors(i, primes)

	#print i, a, b, c
	if len(set(a)) == len(set(b)) == len(set(c)) == len(set(d)) == 4:
		print i-3 # starting from 4 ...
		break
	a,b,c = b,c,d

#print get_prime_factors(8, primes)
コード例 #6
0
ファイル: problem026.py プロジェクト: 1tux/project_euler
from fractions import Fraction as f
from pe_utils import sieve
max_cycle = 1
max_d = 0

primes = sieve(1000)

for d in primes:
	for c in xrange(1, 1000):
		x = f(1, d)
		y = x * (10 ** c) - x
		z = x * (10 ** c)
		if z.denominator == 1: # no cycle
			break
		if y.denominator == 1: # got cycle
			if c > max_cycle:
				max_cycle = c
				max_d = d
			break

print max_d, max_cycle
コード例 #7
0
ファイル: problem066.py プロジェクト: 1tux/project_euler
		l2 = l1[1:-1]
		add = F(l1[0])
		if len(l2) == 1:
			return add+1/F(l2[0])
	return add+F(1)/(F(l2[0])+create_fraction_from_list(l2[1:],0))


def solve_eq(D, m = 1):
	l = sqrt_to_list(D)
	l = [l[0]] + l[1:] + l[i:m]
	f = create_fraction_from_list(l)
	x, y = f.numerator, f.denominator
	return x,y

powers = set(i**2 for i in xrange(40))
primes = set(sieve(10000))

x = (0, 0)
for i in xrange(10000+1):
	if i in powers or i not in primes:
		continue

	s = solve_eq(i)
	# x = max((s[0], i), x)
	m = 1
	xk = s[0]
	yk = s[1]
	n = i
	if xk**2-n*yk**2 != 1: # fix for the case the result is -1
		xk, yk = s[0]*xk+n*s[1]*yk, s[1]*xk+s[0]*yk
	assert xk**2-n*yk**2 == 1
コード例 #8
0
"""
Distinct primes factors

Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
"""
from pe_utils import sieve

p = sieve(1000000)


def num_pfactors(n):
    cnt = 0
    if p[n]:
        return 0
    x = n
    i = 2
    while x != 1:
        if not p[i]:
            i += 1
            continue
        if x % i == 0:
            cnt += 1
        while x % i == 0:
            x //= i
        i += 1
    return cnt


nf = dict()
for i in range(647, 1000000):
    if num_pfactors(i) != 4:
コード例 #9
0
ファイル: problem087.py プロジェクト: 1tux/project_euler
from math import log
from pe_utils import sieve

N = 50 * 10**6
primes = sieve(int(N**0.5) + 1)
primes2 = sieve(int(N**0.34) + 1)
primes3 = sieve(int(N**0.25) + 1)

print log(len(primes), 2)
print log(N, 2)

t = 0
x = set()
for a in primes:
    a_q = a**2
    for b in primes2:
        #if b > a:
        #	break
        b_q = b**3
        for c in primes3:
            #if c > b:
            #	break
            y = a_q + b_q + c**4
            if y < N:
                #print a,b,c, a_q,b_q, a_q+b_q+c**4
                t += 1
                x.add(y)
            else:
                break

print len(x)
コード例 #10
0
ファイル: problem037.py プロジェクト: 1tux/project_euler
from pe_utils import sieve


def count_digits(n):
	from math import log
	return int(log(n, 10)) + 1

primes2 = sieve(10 ** 6)
primes = set(primes2)
print "ready"
digits = range(1, 10, 2) + [2]
found = range(1, 10, 2) + [2]

def is_trunc(n):

	bkp_n = n
	while n > 1:
		if n not in primes:
			return False
		n /= 10
	n = bkp_n
	while n > 1:
		if n not in primes:
			return False
		n = n % 10 ** (count_digits(n)-1)

	return bkp_n % 10 != 1 and str(bkp_n)[0] != "1"# / 10 ** (count_digits(bkp_n)-1) != 1
	return True


while len(set(found)) < 11:
コード例 #11
0
ファイル: problem070.py プロジェクト: 1tux/project_euler
from pe_utils import sieve2, get_prime_factors, is_prime, sieve

primes = None
small_primes = sieve(1000)
print "sieve created"

def phi_n(n):

	res = n
	been = []
	for p in set(get_prime_factors(n, primes)):
		res /= p
		res *= p-1
	return res

def old_main():
	global primes
	prime = sieve2(10 ** 7 + 2)
	best = 5
	best_n = 1
	LIM = 10**7
	for n in xrange(2, LIM+1):
		if any(not n % x for x in small_primes): # for ratio to be smallest we need phi(n) higher so n musn't divide with lot's of primes :)
			continue
		phi = phi_n(n)
		res = float(n) / phi
		if res < best:
			if sorted(str(phi)) == sorted(str(n)):
				best_n = n
				best = res
コード例 #12
0
from pe_utils import sieve
from math import sqrt
N = 30
primes = sieve(int(sqrt(N)))0
print primes
コード例 #13
0
ファイル: problem027.py プロジェクト: 1tux/project_euler
from pe_utils import sieve

primes = set(sieve(10 ** 6))


best = 0
best_score = 0
for a in xrange(-1000, 1000):
	for b in xrange(-1000, 1000):

		c = 0
		for n in xrange(1000):
			if n ** 2 + a*n+b in primes:
				c += 1
			else:
				break

		if c > best_score:
			best = (a, b)
			best_score = c


print best[0] * best[1]
コード例 #14
0
from fractions import Fraction as f
from pe_utils import sieve
max_cycle = 1
max_d = 0

primes = sieve(1000)

for d in primes:
    for c in xrange(1, 1000):
        x = f(1, d)
        y = x * (10**c) - x
        z = x * (10**c)
        if z.denominator == 1:  # no cycle
            break
        if y.denominator == 1:  # got cycle
            if c > max_cycle:
                max_cycle = c
                max_d = d
            break

print max_d, max_cycle
コード例 #15
0
ファイル: problem077.py プロジェクト: 1tux/project_euler
cache = {}
from pe_utils import sieve
primes = sieve(10 ** 4)

def solve(n, k = 1):

	if n == 0:
		return 1
	if n < 0 or n < k:
		return 0
	if (n,k) not in cache:
		x = 0
		for i in primes:#xrange(k, n+1):
			if i < k:
				continue
			if i > n+1:
				break
			x += solve(n-i, i)
		cache[(n,k)] = x

	return cache[(n, k)]

res = 0
i = 0
while res <= 5000:
	i += 1
	res = solve(i)

print i, solve(i)
コード例 #16
0
LIM = 10**5
LIM2 = int(LIM**0.5)
odds = set(xrange(3, LIM, 2))
from pe_utils import sieve

primes = sieve(LIM)
my_nums = set()
for p in primes:
    for i in xrange(LIM2):
        my_nums.add(p + 2 * i**2)

print sorted(list(odds - my_nums))[0]
コード例 #17
0
ファイル: problem051.py プロジェクト: 1tux/project_euler
from pe_utils import sieve
from itertools import combinations
N = 10**6
primes = sieve(N)
primes_set = set(primes)


def digit_len(n):
    return len(str(n))


def change_num_dig(num, dig, val):
    num2 = num
    for i in xrange(1, dig):
        num2 /= 10
    cur_val = num2 % 10

    dif = cur_val - val
    num = num - dif * pow(10, dig - 1)
    return num


def apply_mask(n, comb, val, best_c=10):
    p2 = n
    for x in comb:
        p2 = change_num_dig(p2, x + 1, val)
    return p2


def prime_digit_replace(p, comb, best_c=10):
    c = 0
コード例 #18
0
ファイル: problem070.py プロジェクト: 1tux/project_euler
from pe_utils import sieve2, get_prime_factors, is_prime, sieve

primes = None
small_primes = sieve(1000)
print "sieve created"


def phi_n(n):

    res = n
    been = []
    for p in set(get_prime_factors(n, primes)):
        res /= p
        res *= p - 1
    return res


def old_main():
    global primes
    prime = sieve2(10**7 + 2)
    best = 5
    best_n = 1
    LIM = 10**7
    for n in xrange(2, LIM + 1):
        if any(
                not n % x for x in small_primes
        ):  # for ratio to be smallest we need phi(n) higher so n musn't divide with lot's of primes :)
            continue
        phi = phi_n(n)
        res = float(n) / phi
        if res < best: