コード例 #1
0
def prime_factors(n):
    primes = euler_tools.sieve(n)
    factors = collections.defaultdict(list)
    for p in primes:
        j = 1
        while True:
            factors[p * j].append(p)
            j += 1
            if p * j > n:
                break

    return factors
コード例 #2
0
import euler_tools
import copy

primes = euler_tools.sieve(100)


def n(factorization):
    _n = 1
    for k, v in factorization.items():
        _n *= (k**v)
    return _n


def d(factorization):
    _d = 1
    for k, v in factorization.items():
        _d *= (2 * v + 1)
    return _d


def replace(factorization, factor, limit):
    _factorization = copy.deepcopy(factorization)
    for i in range(2, factor):
        factorization[factor] = 0

        pds = euler_tools.prime_divisors(i)
        for pd in pds:
            factorization[pd] += 1

        print(factor, i, pds, factorization, d(factorization),
              d(_factorization))
コード例 #3
0
import euler_tools

N = 1e6

primes = euler_tools.sieve(int(N))
print(primes)
res = []

for p in range(1, int(N) + 1):
    k = p + 1
    #print(p, k)
    _p = k**2 + k * p + p**2
    if _p in primes:
        res.append(_p)
        print(_p, p**3, k * p**2, p, k)

##    k = p-1
##    _p = k**2+k*p+p**2
##    if _p in primes:
##        res.append(_p)
##        print(_p, p**3, k*p**2, p, k)

    if _p > N:
        break

with euler_tools.Watch():
    print(len(set(res)))
コード例 #4
0
ファイル: problem_266.py プロジェクト: pah8p/Project-Euler
import euler_tools

PRIMES = euler_tools.sieve(190)

FACT = {}


def fact(n):
    try:
        return FACT[n]
    except KeyError:
        if n == 0 or n == 1:
            f = 1
        else:
            f = n * fact(n - 1)
        FACT[n] = f
        return f


def combin(n, k):
    return int(fact(n) / (fact(k) * fact(n - k)))


def product(x):
    p = 1
    for _x in x:
        p *= _x
    return p


def num_divisors(num_primes):
コード例 #5
0
ファイル: problem_124.py プロジェクト: pah8p/Project-Euler
import euler_tools

N = int(1e5)

primes = euler_tools.sieve(N)


def rads(n):

    _rads = [1] * n

    for p in primes:

        k = 1
        while True:

            if p * k <= n:
                _rads[p * k - 1] *= p
                k += 1

            else:
                break

#    print(max(_rads))

    _rads = [(n + 1, rad) for n, rad in enumerate(_rads)]

    #print(_rads)
    return sorted(_rads, key=lambda x: x[1])

コード例 #6
0
ファイル: problem_268.py プロジェクト: pah8p/Project-Euler
from euler_tools import sieve

PRIMES = sieve(100)

print(PRIMES)

def divisible_by_four_primes(n):
    nums = []
    for i in range(len(PRIMES)):
        for j in range(i+1, len(PRIMES)):
            for k in range(j+1, len(PRIMES)):
                for m in range(k+1, len(PRIMES)):

                    q = 1
                    while True:
                        num = q*PRIMES[i]*PRIMES[j]*PRIMES[k]*PRIMES[m]

                        print(n/num)

                        if num < n:
                            nums.append(num)
                            q += 1
                        else:
                            break
                        
    return len(nums), nums


print(divisible_by_four_primes(10000000000000000))
                        
コード例 #7
0
ファイル: problem_641.py プロジェクト: pah8p/Project-Euler
import euler_tools
'''for n < N, n = PROD(p**np) pick n such that PROD(np+1) == 1 MOD 6'''

primes = euler_tools.sieve(int(1e6))


def sieve(n):
    a = [1] * n

    for m in range(2, n + 1):
        if m % 1e6 == 0:
            print(m)
        k = 1
        while True:
            if m * k <= len(a):
                if a[m * k - 1] == 6:
                    a[m * k - 1] = 1
                else:
                    a[m * k - 1] += 1
                k += 1
            else:
                break

    return a


def pairs():
    i = 1
    while True:
        if 2**i > 1e36:
            break
コード例 #8
0
ファイル: problem_123.py プロジェクト: pah8p/Project-Euler
import euler_tools

PRIMES = euler_tools.sieve(int(3e5))
print(PRIMES[2])


def n_min(r_max):

    for n, p in enumerate(PRIMES):

        r = 2 * p * (1 + n)

        print(r, p, n + 1, r < p**2)

        if r >= r_max:

            return n + 1, p, r, n, PRIMES[n - 1], 2 * PRIMES[n - 1] * n

    return None


print(n_min(1e10))