Пример #1
0
def p46():
	#getting precalculated primes and squares:
	u = Utils()
	primes = u.sieve(6000)
	squares = [i * i for i in range(1, 101)]
	#start searching at 15:
	o = 15
	while o < 5800:
		#is the test number prime?
		if u.chop(o, primes) != -1:
			#yeap, go to next one:
			o += 2
			continue
		#nope, begin testing:
		ind = 0
		found = False
		while o - primes[ind] > 0:
			s = (o - primes[ind]) / 2
			#found desired decomposition?
			if u.chop(s, squares) != -1:
				#yep, break out:
				found = True
				break
			#nope, try next prime:
			else:
				ind += 1
			#found possible candidate:
		if not found:
			print o
		#keep searching:
		o += 2
Пример #2
0
def p35():
    u = Utils()
    sieve = u.sieve(10 ** 6)
    count = 0
    for prime in sieve:
        s = str(prime)
        l = cyclic_shifts_of(s)
        if all_in(l, sieve, u):
            count += 1
    return count
Пример #3
0
def p37():
    u = Utils()
    primes = u.sieve(10 ** 6)
    total = 0
    for p in primes:
        a = is_right_truncatable(p, u, primes)
        b = is_left_truncatable(p, u, primes)
        if a and b:
            total += p
    #we have to ignore 2, 3, 5, and 7, which sum up to 17:
    print total - 17
Пример #4
0
def p127(max_c, exp):
    u = Utils()
    primes = u.sieve(max_c)
    radicals = rad(int(max_c), primes)
    possible_ys = [i for i in range(1, max_c) if radicals[i] <= int(max_c ** exp)]
    possible_rads = [radicals[i] for i in possible_ys]
    print("len(radicals):", len(radicals))
    print("len(possible_ys):", len(possible_ys))
    print(possible_ys)
    print(possible_rads)
    total = 0
    for a in possible_ys:
        for b in possible_ys:
            c = a + b
            if a < b and c < max_c and hit(a, b, c, radicals):
                print(a,b,c)
                total += c
    print(total)
Пример #5
0
#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.

from utils import Utils
u = Utils()

a = u.sieve(1100000)

"""
    It is assumed that n is a power of 10.
"""
def phi(n):
    return (2 * n) / 5

def f1(p1, p2):
    m = len(str(p1))
    r = 10 ** m
    q = phi(r) - 1
    s = pow(p2, q, r)
    return ((((p1 * p2 * s) / r) % p2) * r) + p1

l = [f1(a[i], a[i + 1]) for i in range(2, len(a) - 1) if a[i] < 10 ** 6]

print sum(l)
Пример #6
0
#coding: UTF-8
#Let p_n be the nth prime: 2, 3, 5, 7, 11, ..., and let r
#be the remainder when (p_n − 1)^n + (p_n + 1)^n is divided
#by p_n^2.
#
#For example, when n = 3, p_3 = 5, and 4^3 + 6^3 = 280
#≡ 5 mod 25.
#
#The least value of n for which the remainder first exceeds
#10^9 is 7037.
#
#Find the least value of n for which the remainder first
#exceeds 10^10.

from utils import Utils

u = Utils()

p = u.sieve(4 * (10 ** 5))

def p123():
    i = 0
    a = 0
    while a < 10 ** 10 and i < len(p):
        i += 1
        a = (2 * p[i] * (i + 1)) % (p[i] ** 2)
    #sum 2 because i is the index of the number just below
    #10 ** 10, and array indices start by 0:
    print i + 2

u.exec_time(p123)
Пример #7
0
#a perfect cube.
#
#For example, when p = 19, 8^3 + 8^2×19 = 12^3.
#
#What is perhaps most surprising is that for each prime with
#this property the value of n is unique, and there are only
#four such primes below one-hundred.
#
#How many primes below one million have this remarkable
#property?

from utils import Utils

cube_list = [x ** 3 for x in range(578)]
u = Utils()
prime_list = u.sieve(10 ** 6)

def p131():
    """
        As taken from the forum:
        Since x^3 = n^2(n + p), and p is a prime, it turns
        out that n must be a cube, as well as n + p, i.e.,
        we must have p = a^3 - b^3 for some a, b.

        But, for a^3 - b^3 = (a - b)(a^2 + ab + b^2) to be
        prime we must have a - b = 1, so p must be a
        difference of consecutive cubes.
    """
    total = 0
    for i in range(len(cube_list) - 1):
        p_ = cube_list[i + 1] - cube_list[i]
Пример #8
0
#is strongly advised that you solve this one first.

from utils import Utils

u = Utils()

MAX = 200000

def get_exp(n, p):
    i = 0
    while n % p == 0:
        i += 1
        n = n / p
    return i

prime_list = u.sieve(MAX)

#calculating the desired number using memoization, stopping
#as soon as we find it:
def p108():
    i = 2
    l = [0, 1]
    while i < MAX:
        for p in prime_list:
            if i % p == 0:
                val = l[i / p] + \
                    2 * l[i / (p ** get_exp(i, p))] - 1
                if val >= 1000:
                    print i
                    return
                else:
Пример #9
0
#coding: UTF-8
#A composite is a number containing at least two prime
#factors. For example, 15 = 3 × 5; 9 = 3 × 3; 12 = 2 × 2
#× 3.
#
#There are ten composites below thirty containing precisely
#two, not necessarily distinct, prime factors: 4, 6, 9, 10,
#14, 15, 21, 22, 25, 26.
#
#How many composite integers, n < 10^8, have precisely two,
#not necessarily distinct, prime factors?

from utils import Utils
from math import sqrt
from bisect import bisect

u = Utils()

#One of the accepted solutions from the forum (by
#logopetria, @Sat, 22 Mar 2008, 08:45):
primes = u.sieve(5 * (10 ** 7))
def p187():
    N=10**8
    total=0
    for x in range(bisect(primes, sqrt(N))):
        p = primes[x]
        total += bisect(primes, N/p) - x
    print total

u.exec_time(p187)