Beispiel #1
0
def divisors_arithmetic_sequence_bf(limit_n, solutions):
    total = 0
    primes_index = prime_factors(3 * limit_n, False)
    print('Primes calculated!')
    primes = prime_factors(10**4)
    t0 = time()
    p = 1
    for n in range(1, limit_n + 1):
        if n > p * 10**6:
            print('Now in: ', p)
            p += 1
        subtotal = 0
        k = 3 * n
        if primes_index[k] == 0:
            if n % 4 in (0, 3):
                sqrt_k = math.sqrt(k)
                factors = list(decompose_primes(n, primes))
                print(k, n % 4, 'Candidate', factors)
                factors = decompose_primes(n, primes)
                for d in range(1, math.ceil(sqrt_k)):
                    if k % d == 0:
                        if (d % 2) + ((k // d) % 2) != 1:
                            if check_compatibility(d, k // d, k):
                                subtotal += 1
                                if subtotal > solutions:
                                    break
        if subtotal == solutions:
            print(k, list(factors))
            total += 1
    t1 = time()
    print('Total time to get solution: ', t1 - t0)
    return total
Beispiel #2
0
def product_pairs(n):
    '''
    2. This function solves the problem finding every possible pair of factors given its prime decomposition
    '''
    primes = prime_factors(1000)
    prime_factors = decompose_primes(n, primes, as_dict=False)
    prime_factors = [1] + prime_factors
    pairs = {}
    for r1 in range(1, len(prime_factors) // 2 + 1):
        for comb1 in combinations([i for i in range(len(prime_factors))], r1):
            a = 1
            for idx in comb1:
                a *= prime_factors[idx]
            set_a = set(decompose_primes(a, primes))
            complement = [
                i for i in range(len(prime_factors)) if i not in comb1
            ]
            for r2 in range(r1, len(complement) + 1):
                for comb2 in combinations(complement, r2):
                    b = 1
                    for idx in comb2:
                        b *= prime_factors[idx]
                    if set_a.isdisjoint(set(decompose_primes(b, primes))):
                        if (a, b) not in pairs and (b, a) not in pairs:
                            pairs[(a, b)] = 1
    return len(pairs) + 1
Beispiel #3
0
def pythagorean_triplets(limit_n):
    primes = prime_factors(1000)
    total = 0
    t0 = time()
    for m in range(2, limit_n + 1):
        if 2 * m**2 + 2 * m >= limit_n:
            break
        factors = decompose_primes(m, primes, True)
        inc = 1
        if m % 2 == 0:
            inc = 2
        complete = False
        for n in range(1, m, inc):
            if inc == 1 and n % 2 == 1:
                continue
            are_coprime = True
            for p in factors:
                if n % p == 0:
                    are_coprime = False
                    break
            if are_coprime:
                a = m**2 - n**2
                b = 2 * m * n
                c = m**2 + n**2
                if (a + b + c) >= limit_n:
                    break
                if c % (b - a) == 0:
                    print(a, b, c)
                    total += limit_n // (a + b + c)
        if complete:
            break
    t1 = time()
    print('Time to reach solution: {0} sec'.format(t1 - t0))
    return total
Beispiel #4
0
def gcd(p, primes):
    factors_n = {2: 9, 5: 9}
    factors_p = decompose_primes(p, primes, True)
    val = 1
    for i in [2, 5]:
        if i in factors_p:
            for n in range(min(factors_p[i], factors_n[i])):
                val *= i
    return val
Beispiel #5
0
def radicals(limit_n):
    '''
    finds radicals for every prime number below limit_n
    '''
    radicals = {}
    primes = prime_factors(limit_n)
    for i in range(1, limit_n):
        radicals[i] = list(decompose_primes(i, primes, True).keys())
    square_free = [0] * (limit_n + 1)
    for p in primes:
        for i in range(p * p, limit_n, p * p):
            square_free[i] = 1
    return radicals, square_free
Beispiel #6
0
def solution_given_primes():
    '''
    3. This function solves the problem using the prime decomposition to calculate the number of valid solutions
    '''
    n = 180100
    primes = prime_factors(200000)
    while True:
        dec_primes = decompose_primes(n**2, primes, as_dict=True)
        divs = 1
        for p in dec_primes:
            divs *= (dec_primes[p] + 1)
        if divs > 1999:
            return n
            break
        n += 1