コード例 #1
0
def run():
    sieve = sieve_of_eratosthenes(1000000, lower_limit=10)
    # builds masks
    bins = {i: [] for i in range(1, 7)}
    for i in range(1, 7):
        bins[i].extend([
            j for j in product(('0', '1'), repeat=i)
            if sum(int(k) for k in j) == 3
        ])
    for prime in sieve:
        binaries = bins[len(str(prime)) - 1]
        for b in binaries:
            res = number_primes(prime, b)
            if res[0] == 8:
                return res[1]
コード例 #2
0
from Utils import sieve_of_eratosthenes


def rotate(s):
    return s[len(s) - 1] + s[:len(s) - 1]


sieve = sieve_of_eratosthenes(999999)
table = {k: False for k in range(1000000)}
for i in sieve:
    table[i] = True

circular_primes = []

for i in sieve:
    if i not in circular_primes:
        s = str(i)
        rotations = []
        for k in s:
            s = rotate(s)
            rotations.append(int(s))
            if not table[int(s)]:
                rotations = []
                break
        circular_primes += rotations

print(len(set(circular_primes)))
コード例 #3
0
# same as 76 only nums is different
from Utils import sieve_of_eratosthenes
limit = 1000
table = {i: 0 for i in range(-limit + 1, limit + 1)}
table[0] = 1
nums = sieve_of_eratosthenes(limit)
for num in nums:
    for amount in range(1, limit + 1):
        table[amount] += table[amount - num]
for k, v in table.items():
    if v > 5000:
        print(k)
        break
コード例 #4
0
# the less primefactors a number has the closer is the euler-totient to it
# checking every possible number with two factors yields the correct result
from sympy import factorint
from math import sqrt
from Utils import sieve_of_eratosthenes


# returns euler totient given the only two factors
def euler_totient(n, two_factors):
    return int(n * (1 - (1 / two_factors[0])) * (1 - (1 / two_factors[1])))


# True if a and b have the same digits
def samedigits(a, b):
    return sorted([int(i) for i in str(a)]) == sorted([int(i) for i in str(b)])


limit = 10**7
sieve = sieve_of_eratosthenes(4000)
min = 1000
for i, prime1 in enumerate(sieve):
    for j in range(i, len(sieve)):
        prime2 = sieve[j]
        product = prime1 * prime2
        if product > limit:
            break
        eul = euler_totient(product, (prime1, prime2))
        if product / eul < min and samedigits(product, eul):
            min = (product) / eul
            min_prod = product
print(min_prod)
コード例 #5
0
from Utils import sieve_of_eratosthenes

# no need for euler totient function, because only small numbers
def recur_len(d):
    if d % 2 != 0 and d % 5 != 0 and d != 0 and d != 1:
        n = 1
        # every fraction 1/d can be written as x/(10**n - 1) where n is the length 
        # of the recurring cycle
        while (10**n - 1) % d != 0:
            n += 1
        return n

res = 0
res_index = 0
sieve = sieve_of_eratosthenes(1000,lower_limit=6)

for i in reversed(sieve):
    length = recur_len(i)
    if length > res:
        res = length
        res_index = i
    if i < res:
        break

print(res_index)
コード例 #6
0
from Utils import sieve_of_eratosthenes

print(sum(sieve_of_eratosthenes(2000000)))
コード例 #7
0
from Utils import sieve_of_eratosthenes

def samedigits(a,b):
    a = sorted(str(a))
    b = sorted(str(b))
    return a == b

sieve = sieve_of_eratosthenes(9999,lower_limit=1000)
for i in range(len(sieve)-1):
    prime1 = sieve[i]
    for j in range(i+1,len(sieve)):
        prime2 = sieve[j]
        # test if same digits
        if samedigits(prime1,prime2):
            prime3 = 2*prime2 - prime1
            # test if prime3 is prime
            if prime3 in sieve and samedigits(prime1,prime3):
                concat = str(prime1)+str(prime2)+str(prime3)
                if concat != '148748178147':
                    print(concat)