Example #1
0
def prime_factors(n):
    l = []
    for p in primes(n+1):
        while n % p == 0:
            n //= p
            l.append(p)
    return l
Example #2
0
def gen_distinct_primefacs(n):
    """Generates a list of a list of prime factors for all numbers <= n"""
    prime_facs = [[] for i in xrange(n+1)]
    for prime in primes(n+1):
        for i in xrange(prime, n+1, prime):
            prime_facs[i].append(prime)
    return prime_facs
Example #3
0
def rad(n):
    """Creates list of radicals up to n"""
    l = [1]*(n+1)
    for prime in primes(n+1):
        for i in xrange(prime, n+1, prime):
            l[i] *= prime
    return l
Example #4
0
def main(n):
    factorisations = prime_factors_gen(n)
    result = 0
    current_rval = 1
    prime_factorisation = {p: 0 for p in primes(n+1)}
    for k in xrange(1, n//2+1):
        print k
        numerator_change = 1
        denominator_change = 1
        change_dictionary = {}
        for p in factorisations[n-k+1]:
            change_dictionary[p] = factorisations[n-k+1][p]
        for p in factorisations[k]:
            if p in change_dictionary:
                change_dictionary[p] -= factorisations[k][p]
            else:
                change_dictionary[p] = -factorisations[k][p]
        for p in change_dictionary:
            if prime_factorisation[p] + change_dictionary[p] != 0:
                numerator_change *= (1 + pow(p, prime_factorisation[p] + change_dictionary[p], modulus))
            if prime_factorisation[p] != 0:
                denominator_change *= (1 + pow(p, prime_factorisation[p], modulus))
            prime_factorisation[p] += change_dictionary[p]
            numerator_change %= modulus
            denominator_change %= modulus
        current_rval *= numerator_change
        current_rval *= modinv(denominator_change, modulus)
        current_rval %= modulus
        if k < n//2:
            result += 2*current_rval
        else:
            result += current_rval
        result %= modulus
    return (result - pow(2, n, modulus) + 2) % modulus
Example #5
0
def main(n):
    prime_list = primes(n+2)
    l = [(1, k**3+1) for k in xrange(n+1)]
    for p in prime_list:
        remainders = set([])
        if p == 2:
            remainders.add(1)
        elif p == 3:
            remainders.add(2)
        else:
            if is_square(p-3, p):
                a, b = find_square_roots(p-3, p)
                remainders.add((1+a)*((p+1)//2) % p)
                remainders.add((1+b)*((p+1)//2) % p)
            remainders.add(p-1)
        for rem in remainders:
            for i in xrange(rem, n+1, p):
                a, b = l[i]
                while b % p == 0:
                    b //= p
                l[i] = (p, b)
    result = 0
    for i in l[1:]:
        result += max(i) - 1
    return result
Example #6
0
def f(perimeter):
    climit = perimeter // 2
    count = 0
    cfactorisations = [{} for _ in xrange(climit+1)]
    #Fill in cfactorisations
    for sol in xrange(1, climit+1, 2):
        cfactorisations[sol][2] = 1
    for p in primes(climit+1)[1:]:
        if is_square(p-1, p):
            a, b = find_square_roots(p-1, p)
            for sol in range(a, climit+1, p) + range(b, climit+1, p):
                k = 0
                N = sol**2 + 1
                while N % p == 0:
                    N //= p
                    k += 1
                cfactorisations[sol][p] = k
    for c in xrange(1, climit+1):
        if (c**2+1) != prod(cfactorisations[c]):
            cfactorisations[c][(c**2+1)//prod(cfactorisations[c])] = 1
    print "Completed factorisations"
    for c in xrange(1, climit+1):
        print c
        new_count = 0
        curr_prod = (1, 0)
        prime_facs = cfactorisations[c]
        curr_primes = prime_facs.keys()
        if 2 in curr_primes:
            for i in xrange(prime_facs[2]):
                curr_prod = compl_mult(curr_prod, (1, 1))
            curr_primes.remove(2)
        mod3_primes = [p for p in curr_primes if p % 4 == 3]
        if all([prime_facs[p] % 2 == 0 for p in mod3_primes]):
            k = 1
            for p in mod3_primes:
                k *= p**(prime_facs[p]//2)
            mod1_primes = sorted([p for p in curr_primes if p % 4 == 1])
            length = len(mod1_primes)
            l = [range(prime_facs[p]+1) for p in mod1_primes]
            for picker in product(*l):
                new_prod = curr_prod
                for i in xrange(length):
                    p = mod1_primes[i]
                    div = prime_divs(p)
                    conj_div = (div[0], -div[1])
                    pick = picker[i]
                    for j in xrange(pick):
                        new_prod = compl_mult(new_prod, div)
                    for j in xrange(prime_facs[p] - pick):
                        new_prod = compl_mult(new_prod, conj_div)
                for z in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
                    a, b = compl_mult(new_prod, z)
                    if a >= 0 and b >= 0:
                        if a <= b:
                            if c <= k*(a + b):
                                if k*a + k*b + c <= perimeter:
                                    new_count += 1
        count += new_count
    return count
Example #7
0
def f(n):
    s = 0
    for p in primes(n):
        if p > n // 2:
            s += p
        else:
            s += p*legendre(p, n)
    return s
Example #8
0
def S(n):
    new_prime_list = primes(isqrt(n)+1)
    Pindex = len(new_prime_list)
    result = (tri(n) - 1)
    for i in xrange(Pindex):
        c, s = CSeq(n, i)
        result += (prime_list[i]*c - s)
    return result
Example #9
0
def S(n):
    prime_list = primes(n+1)
    l = [0, 0] + [1]*(n-1)
    for prime in prime_list:
        for k in xrange(1, int(log(n)/log(prime))+1):
            res = s(prime, k)
            for index in xrange(prime**k, n+1, prime**k):
                l[index] = max(l[index], res)
    return sum(l)
Example #10
0
def gen_facs(n):
    l = [[] for _ in xrange(n+1)]
    for p in primes(n+1):
        k = 1
        while p**k <= n:
            for i in xrange(p**k, n+1, p**k):
                l[i].append(p)
            k += 1
    return l
Example #11
0
def f(n, max_allowed):
    if (n, max_allowed) in mem:
        return mem[(n, max_allowed)]
    if max_allowed == 2 or n < 3:
        tmp = n%2 == 0
    elif n == max_allowed:
        tmp = isprime(n) + f(n, max_allowed - 1)
    else:
        tmp = sum(f(n-i, min(i, n-i)) for i in primes(max_allowed+1))
    mem[(n, max_allowed)] = tmp
    return tmp
Example #12
0
def prime_factors_gen(n):
    factorisations = [{} for _ in xrange(n+1)]
    for p in primes(n+1):
        for i in xrange(p, n+1, p):
            factorisations[i][p] = 1
        k = 2
        while p**k <= n:
            for i in xrange(p**k, n+1, p**k):
                factorisations[i][p] += 1
            k += 1
    return factorisations
Example #13
0
def F(N):
    neg_factorisations = [{} for _ in xrange(N+1)]
    pos_factorisations = [{} for _ in xrange(N+1)]
    for i in xrange(2, N+1, 2):
        neg_factorisations[i][2] = 1
        pos_factorisations[i][2] = 1
    for p in primes(N+3)[1:]:
        if p % 4 == 1:
            #Then -1 is a quadratic residue
            m1, m2 = find_square_roots(p-1, p)
            pos_solutions = [m1 - 1, m2 - 1]
            neg_solutions = [m1 + 1, m2 + 1]
            k = 1
            while p**(k-1) <= N:
                for sol in pos_solutions:
                    for i in xrange(sol, N+1, p**k):
                        if p in pos_factorisations[i]:
                            pos_factorisations[i][p] += 1
                        else:
                            pos_factorisations[i][p] = 1
                for sol in neg_solutions:
                    for i in xrange(sol, N+1, p**k):
                        if p in neg_factorisations[i]:
                            neg_factorisations[i][p] += 1
                        else:
                            neg_factorisations[i][p] = 1
                pos_solutions = [(x - (pos_f(x)*modinv(2*x+2, p))) % p**(k+1) for x in pos_solutions]
                neg_solutions = [(x - (neg_f(x)*modinv(2*x-2, p))) % p**(k+1) for x in neg_solutions]
                k += 1
    result = 0
    for i in xrange(1, N+1):
        print i
        pos_factor_result = prod([p**pos_factorisations[i][p] for p in pos_factorisations[i]])
        neg_factor_result = prod([p**neg_factorisations[i][p] for p in neg_factorisations[i]])
        if pos_factor_result != pos_f(i):
            if pos_f(i)//pos_factor_result in pos_factorisations[i]:
                pos_factorisations[i][pos_f(i)//pos_factor_result] += 1
            else:
                pos_factorisations[i][pos_f(i)//pos_factor_result] = 1
        if neg_factor_result != neg_f(i):
            if neg_f(i)//neg_factor_result in neg_factorisations[i]:
                neg_factorisations[i][neg_f(i)//neg_factor_result] += 1
            else:
                neg_factorisations[i][neg_f(i)//neg_factor_result] = 1
        factorisation = {}
        for p in pos_factorisations[i]:
            factorisation[p] = pos_factorisations[i][p]
        for p in neg_factorisations[i]:
            if p in factorisation:
                factorisation[p] += neg_factorisations[i][p]
            else:
                factorisation[p] = neg_factorisations[i][p]
        result += R(factorisation)
    return result
Example #14
0
def f(n):
    prime_list = primes(n // 2)
    semiprimes = set([])
    for factor1 in prime_list:
        for factor2 in prime_list:
            x = factor1 * factor2
            if x < n:
                semiprimes.add(x)
            else:
                break
    return len(semiprimes)
Example #15
0
def pi(x):
    prime_list = [1] + primes(int(x**0.5)+1)
    def phi(x, a):
        if (x, a) in phi_mem:
            return phi_mem[(x, a)]
        elif a == 1:
            return (x + 1) // 2
        else:
            t = phi(x, a-1) - phi(x // prime_list[a], a-1)
            phi_mem[(x, a)] = t
            return t
    return phi(x, len(prime_list) - 1) + len(prime_list) - 2
Example #16
0
def modified_prime_sieve(start, stop):
    """Returns a list of all primes p such that start <= p < stop"""
    l = [True for _ in xrange(start, stop)]
    prime_list = primes(isqrt(stop)+1)
    for p in prime_list:
        if start % p == 0:
            first = start
        else:
            first = (start//p)*p + p
        for i in xrange(first-start, stop-start, p):
            l[i] = False
    return [start+i for i in xrange(stop-start) if l[i]]
Example #17
0
def main(n):
    l = [True for _ in xrange(n+1)]
    limit = isqrt(2*n**2)
    for p in primes(limit+1)[1:]:
        if is_square((p+1)//2, p):
            a, b = find_square_roots((p+1)//2, p)
            for i in xrange(a, n+1, p):
                if 2*i**2 - 1 > p:
                    l[i] = False
            for i in xrange(b, n+1, p):
                if 2*i**2 - 1 > p:
                    l[i] = False
    return sum(l) - 2
Example #18
0
def main(n):
	candidates = [p for p in primes(n)[1:] if is_square(5, p)]
	count = 1
	successes = 5 #Since 3 is a prim fib root mod 5, we add this.
	for cand in candidates:
		a, b = find_square_roots(5, cand)
		if is_prim_root((1+a)*((cand+1)//2) % cand, cand):
			count += 1
			successes += cand
		else:
			if is_prim_root((1+b)*((cand+1)//2) % cand, cand):
				count += 1
				successes += cand
	return count, successes
Example #19
0
def main(n):
    prime_list = primes(n+2)
    #divisors = dynamic_modified_divisors(n)
    result = 1 #1 is a special case, 2 does not divide 1, but all other numbers
    cand1 = [p-1 for p in prime_list]
    cand2 = [2*(p-2) for p in prime_list]
    cands = list(set(cand1).intersection(set(cand2)))

    #Removing non-squarefree numbers
    for p in primes(int(n**0.5)+1):
        cands = [cand for cand in cands if cand % (p**2) != 0]

    for cand in cands:
        flag = True
        for i in xrange(3, int(cand**0.5)+1):
            if cand % i == 0:
                if not isprime(i + cand // i):
                    flag = False
                    break
        if flag:
            result += cand

    return result
Example #20
0
def S(n):
    l = [True] * (5*n)
    low = ((n-2)*(n-3))//2 + 1
    high = ((n+2)*(n+3))//2
    for p in primes(isqrt(high)+1):
        if low % p == 0:
            for i in xrange(low, high+1, p):
                l[i-low] = False
        else:
            for i in xrange( ((low //p)+1)*p, high+1, p):
                l[i-low] = False
    prevrowlow = ((n-2)*(n-1))//2 + 1
    prevrowhigh = ((n-1)*n)//2
    nthrowlow = ((n-1)*n)//2 + 1
    nthrowhigh = (n*(n+1))//2
    nextrowlow = (n*(n+1))//2 + 1
    nextrowhigh = ((n+1)*(n+2))//2
    return [i+low for i in xrange(5*n) if l[i]]
Example #21
0
def factors_of_even_gen(n):
    factorisations = [{} for _ in xrange(0, n+1, 2)]
    for i in xrange(2, n+1, 2):
        factorisations[i//2][2] = 1
    k = 2
    while 2**k <= n:
        for i in xrange(2**k, n+1, 2**k):
            factorisations[i//2][2] += 1
        k += 1
    for p in primes(n+1)[1:]:
        print p
        for i in xrange(2*p, n+1, 2*p):
            factorisations[i//2][p] = 1
        k = 2
        while p**k <= n:
            for i in xrange(2*p**k, n+1, 2*p**k):
                factorisations[i//2][p] += 1
            k += 1
    return factorisations
Example #22
0
def S(n):
    res = 0
    prime_list = primes(n)
    for b in prime_list[1:-1]:
        print b
        #Case a = 2
        if (b + 1) % 3 == 0:
            c = (((b+1)**2) // 3) - 1
            if search(prime_list, c):
                res += (2+b+c)
        divisors = get_divs_square(b+1)
        a_vals = [d-1 for d in divisors if d <= b and d % 2 == 0]
        for a in a_vals:
            c = ((b+1)**2 // (a+1)) - 1
            if c < n:
                if search(prime_list, a):
                    if search(prime_list, c):
                        res += (a+b+c)
    return res
Example #23
0
def f(n):
    l = []
    i = 2
    while i < n:
        l.append(i)
        i *= 2
    prime_list = primes(1000)
    i = 1
    while prod(prime_list[:i]) < n:
        i += 1
    result = l
    for p in prime_list[1:i-1]:
        new = []
        for elem in l:
            if (n // elem) >= p:
                for i in xrange(1, int(log(n//elem, p))+1):
                    new.append(elem*p**i)
        result += new
        l = new
    return sum(set(pseudoFortunate(i) for i in result))
Example #24
0
def main(L):
    P = [(0, 4*i**2+1) for i in xrange(L+1)]
    for p in primes(2*L+1)[2:]:
        print p
        r = (p-1)*modinv(4, p) % p
        if is_square(r, p):
            a, b = find_square_roots((p-1)*modinv(4, p) % p, p)
            for i in xrange(a, L+1, p):
                n, m = P[i]
                while m % p == 0:
                    m //= p
                P[i] = (p, m)
            for i in xrange(b, L+1, p):
                n, m = P[i]
                while m % p == 0:
                    m //= p
                P[i] = (p, m)
    result = 0
    for i in xrange(1, L+1):
        result += max(P[i][0], P[i][1])
    return result % 10**18
Example #25
0
def main(n):
    prime_list = primes(n)
    A = [1]
    prod = 2
    res = 1
    for i in xrange(1, len(prime_list)):
        new_prime = prime_list[i]
        print new_prime
        res = crt(res, i+1, prod, new_prime)
        A.append(res)
        prod *= new_prime
    result = 0
    for i in xrange(1, len(prime_list)):
        curr_prime = prime_list[i]
        print curr_prime
        for j in xrange(1, i):
            if A[j] % curr_prime == 0:
                result += curr_prime
                A[j] //= curr_prime
                break
    return result
Example #26
0
def f(n):
    """
    #11914460
    >>> sum(f(15 * (10 ** 7)))
    676333270
    >>> sum(f(2000000))
    1242490
    >>> sum(f(100))
    10
    """
    ps = list(primes(5000))
    steps = list(get_steps(ps, n))
    for i in steps:
        if i >= n:
            break
        sq_i = i * i
        if (
            i < n
            and all(map(lambda x: is_prime(sq_i + x), ADD_List))
            and all(map(lambda x: not is_prime(sq_i + x), [11, 17, 19, 21, 23]))
        ):
            yield i
Example #27
0
def main(n):
    current_primes = [p for p in primes(n) if p % 4 == 1]
    prime_divisors = []
    for p in current_primes:
        signal = False
        for i in xrange(1, n):
            if signal:
                break
            for j in xrange(1, i):
                if p == i**2 + j**2:
                    prime_divisors.append((i, j))
                    signal = True
                    break
    num_primes = len(current_primes)
    final_result = 0
    for picker in product([0, 1], repeat=num_primes):
        div_list = []
        for i in xrange(num_primes):
            if picker[i]:
                div_list.append(prime_divisors[i])
        final_result += S(div_list)
    return final_result
Example #28
0
def G(n):
    count = 0
    prime_list = primes(isqrt(4*n+1)+1)
    N = 8*n+3
    l = [1 for _ in xrange((isqrt(8*n+3)-1)//2+1)]
    products = [2 for _ in xrange((isqrt(8*n+3)-1)//2+1)]
    for p in prime_list[1:]:
        L = N % p
        if L == 0:
            sol = ((-1)*(p+1)//2) % p
            for i in xrange(sol, len(l), p):
                exp = power(p, N-(2*i+1)**2)
                products[i] *= p**exp
                if p % 4 == 1:
                    l[i]*= (exp+1)
                else:
                    if exp % 2 != 0:
                        l[i] = 0
        if is_square(L, p):
            l1, l2 = find_square_roots(L, p)
            sol1 = ((-1 + l1)*(p+1)//2) % p
            sol2 = ((-1 + l2)*(p+1)//2) % p
            for i in range(sol1, len(l), p) + range(sol2, len(l), p):
                exp = power(p, N-(2*i+1)**2)
                products[i] *= p**exp
                if p % 4 == 1:
                    l[i]*= (exp+1)
                else:
                    if exp % 2 != 0:
                        l[i] = 0
    for k in xrange((isqrt(8*n+3)-1)//2+1):
        if N - (2*k+1)**2 != products[k]:
            p = (N - (2*k+1)**2)//products[k]
            if p % 4 == 3:
                l[k] = 0
            else:
                l[k] *= 2
    return sum(l)
Example #29
0
def f2():
    N=5*10**7
    l=list(primes(7100))
    step=500000
    re=0
    re+=len(l)*2
    re+=len([i for i in l if i%4==3])
    begin=7100
    while begin+step<N:
        print(begin)
        b=list(big_primes(begin,begin+step,l))
        if (begin+step)*16<N:
            re+=len(b)
        elif begin*16<N:
            re+=len([i for i in b if i*16<N])
        if (begin+step)*4<N:
            re+=len(b)
        elif begin*4<N:
            re+=len([i for i in b if i*4<N])
        re+=len([i for i in b if i%4==3])
        begin+=step
    b=list(big_primes(begin,N,l))
    re+=len([i for i in b if i%4==3])
    return re
Example #30
0
from eulertools import primes

prime_list = primes(100000)
prime_set = set(prime_list)
prime_sum = 0
counter = 0


def is_truncable(prime):
    prime = str(prime)
    for x in xrange(1, len(str(prime))):
        if not int(prime[-x:]) in prime_set:
            return False
        if not int(prime[:x]) in prime_set:
            return False
    return True


for p in prime_list[4:]:
    if is_truncable(p):
        print p
        prime_sum += p
        counter += 1
    if counter == 11:
        break
print prime_sum
Example #31
0
def main(n):
    prime_list = primes(n)[2:]
    return sum(f(prime_list[i], prime_list[i+1]) for i in xrange(len(prime_list)-1))
Example #32
0
from eulertools import primes

circular_primes = set()
prime_set  = set(primes(100000))

def is_circular(prime):
    for x in xrange(1,len(str(prime))):
        if not int(rotate(str(prime), x)) in prime_set:
            return False
    return True
    
def rotate(strg,n):
''' Rotates a string to the left by n characters '''
    return strg[n:] + strg[:n]

for p in filter( lambda x: x < 10**6 , prime_set):
    if is_circular(p):
        circular_primes.add(p)
        
print len(circular_primes)