Exemplo n.º 1
0
import math
from sieve_prime import get_all_primes

all_primes = get_all_primes(10000000)

all_primes[-1]

len(all_primes)


def is_relative_prime(num1, num2):
    return math.gcd(num1, num2) == 1

def phi(n):
    return sum(is_relative_prime(i, n) for i in range(1,n))

def is_perm(num1, num2):
    num1_str = str(num1)
    num2_str = str(num2)
    for c in '0123456789':
        if num1_str.count(c) != num2_str.count(c):
            return False
    return True


#max(i/phi(i) for i in range(2,upper_bound) if i % 2 == 0)
print(17*13)
phi(17*13)

def phi2(m,n):
    return m * n - m - n + 1
Exemplo n.º 2
0
from sieve_prime import get_all_primes
from bisect import bisect_right

all_primes = get_all_primes(50000)


def find_max_prime(n):
    return all_primes[bisect_right(all_primes, n) - 1]


def ways(n, p):
    if n == 0:
        return 1
    if n == 1:
        return 0
    if p > n:
        return ways(n, find_max_prime(n))
    else:
        if p == 2:
            if n % 2 == 0:
                return 1
            else:
                return 0
        else:
            total = 0
            temp_prime = p
            while temp_prime > 2:
                total += sub_ways(n, temp_prime)
                temp_prime = find_max_prime(temp_prime - 1)
            if n % 2 == 0:
                total += 1
Exemplo n.º 3
0
    return sum(math.gcd(i, n) == 1 for i in range(1, n))


def prime_factors(num):
    f = set()
    if get_index(all_primes, num) > -1:
        f.add(num)
        return f
    else:
        remaining = num
        for p in all_primes:
            while remaining % p == 0:
                f.add(p)
                remaining //= p
            if remaining == 1:
                return f


def phi2(n):
    result = n
    factors = prime_factors(n)
    for f in factors:
        result *= f - 1
        result //= f
    return result


limit = 1000000
all_primes = get_all_primes(limit)
sum(phi2(i) for i in range(2, limit + 1))
Exemplo n.º 4
0
def prime_factors(num):
    f = set()
    if get_index(all_primes, num) > -1:
        f.add(num)
        return f
    else:
        remaining = num
        for p in all_primes:
            while remaining % p == 0:
                f.add(p)
                remaining //= p
            if remaining == 1:
                return f


def phi2(n):
    result = n
    factors = prime_factors(n)
    for f in factors:
        result *= f - 1
        result //= f
    return result


limit = 1000000
all_primes = get_all_primes(limit)
sum(phi2(i) for i in range(2, limit+1))


Exemplo n.º 5
0
from sieve_prime import get_all_primes
from bisect import bisect_right

all_primes = get_all_primes(50000)

def find_max_prime(n):
    return all_primes[bisect_right(all_primes, n) - 1]

def ways(n, p):
    if n == 0:
        return 1
    if n == 1:
        return 0
    if p > n:
        return ways(n, find_max_prime(n))
    else:
        if p == 2:
            if n % 2 == 0:
                return 1
            else:
                return 0
        else:
            total = 0
            temp_prime = p
            while temp_prime > 2:
                total += sub_ways(n, temp_prime)
                temp_prime = find_max_prime(temp_prime - 1)
            if n % 2 == 0:
                total += 1
            return total
Exemplo n.º 6
0
import math
from bisect import bisect_right
from sieve_prime import get_all_primes

upper_bound = 50000000

p4_bound = int(upper_bound ** (1/4))

p3_bound = int(upper_bound ** (1/3))

p2_bound = int(upper_bound ** (1/2))

p2_primes = get_all_primes(p2_bound)
p3_index = bisect_right(p2_primes, p3_bound)
p4_index = bisect_right(p2_primes, p4_bound)


p3_primes = p2_primes[:p3_index]
p4_primes = p2_primes[:p4_index]


print(len(p2_primes), len(p3_primes), len(p4_primes))

sum_set = set()
for p2 in p2_primes:
    for p3 in p3_primes:
        for p4 in p4_primes:
            total = p2 ** 2 + p3 ** 3 + p4 ** 4
            if total <= upper_bound:
                sum_set.add(total)
            else:
Exemplo n.º 7
0
import math
from bisect import bisect_right
from sieve_prime import get_all_primes

upper_bound = 50000000

p4_bound = int(upper_bound**(1 / 4))

p3_bound = int(upper_bound**(1 / 3))

p2_bound = int(upper_bound**(1 / 2))

p2_primes = get_all_primes(p2_bound)
p3_index = bisect_right(p2_primes, p3_bound)
p4_index = bisect_right(p2_primes, p4_bound)

p3_primes = p2_primes[:p3_index]
p4_primes = p2_primes[:p4_index]

print(len(p2_primes), len(p3_primes), len(p4_primes))

sum_set = set()
for p2 in p2_primes:
    for p3 in p3_primes:
        for p4 in p4_primes:
            total = p2**2 + p3**3 + p4**4
            if total <= upper_bound:
                sum_set.add(total)
            else:
                break
Exemplo n.º 8
0
import math
import numpy as np
from sieve_prime import get_all_primes

from bisect import bisect_left

def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    return -1

upper_bound = 900000000

all_primes = get_all_primes(upper_bound)

len(all_primes)

def get_corner(i, j, corners):
    if i == 0:
        return 1 + 8 * i + j * 2 + 2
    else:
        return corners[i-1][j] + 8 * i + j * 2 + 2


def get_ratio(n, corners):

    for i in range(n):
        for j in range(4):
            corners[i][j] = get_corner(i, j, corners)
Exemplo n.º 9
0
from sieve_prime import get_all_primes

from bisect import bisect_left


def index(a, x):
    'Locate the leftmost value exactly equal to x'
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    return -1


upper_bound = 900000000

all_primes = get_all_primes(upper_bound)

len(all_primes)


def get_corner(i, j, corners):
    if i == 0:
        return 1 + 8 * i + j * 2 + 2
    else:
        return corners[i - 1][j] + 8 * i + j * 2 + 2


def get_ratio(n, corners):

    for i in range(n):
        for j in range(4):