Example #1
0
def main():
    prime_list = [i for i in primes_below(1000)]
    consecutive = 0
    for i in count(1):
        factor_set = dict()
        remain_i = i

        if max(prime_list) == i:
            print max(prime_list)
            prime_list = [j for j in primes_below(i * 10)]

        if i in prime_list:
            consecutive = 0
            continue

        for prime in prime_list:
            while remain_i % prime == 0:
                factor_set[prime] = True
                remain_i = remain_i / prime
            if remain_i == 1:
                break

        if len(factor_set) == DISTINCT_COUNT:
            consecutive += 1
            if consecutive == DISTINCT_COUNT:
                print('we found 4 consecutvie numbers from {} to {}'.format(
                    i - DISTINCT_COUNT + 1, i))
                break
        else:
            consecutive = 0
def main():
    biggest_prime_now = 10000
    while biggest_prime_now > 1000:
        biggest_prime_now = max(primes_below(biggest_prime_now - 1))
        for middle_prime in primes_below(biggest_prime_now - 1):
            if set(list(str(biggest_prime_now))) == set(list(
                    str(middle_prime))):
                smallest_prime = 2 * middle_prime - biggest_prime_now
                if smallest_prime < 1000:
                    continue
                if isprime(smallest_prime):
                    if set(list(str(smallest_prime))) == set(
                            list(str(middle_prime))):
                        print('we found a arithmetic seq {},{},{}'.format(
                            smallest_prime, middle_prime, biggest_prime_now))
Example #3
0
def erdos_orders():
  rtv = []
  for p in pyprimes.primes_below(1000):
    for k in range(1,30):
      rtv.append(erdos_order(p,k))
  rtv.sort()
  return rtv
Example #4
0
def main():
    prime_list = list(primes_below(UPPER_LIMIT))
    prime_list_len = len(prime_list)
    consecutive_len = 0

    # find the longest possible len, which sum is below 1 million
    for consecutive_len in count(1):
        if sum(prime_list[:consecutive_len - 1]) > UPPER_LIMIT:
            break

    while consecutive_len > 0:
        i = 0
        sum_now = sum(prime_list[i:i + consecutive_len])

        while i + consecutive_len < prime_list_len:
            if sum_now > UPPER_LIMIT:
                break
            else:
                if isprime(sum_now):
                    print('found max consecutive primes sum {}'.format( \
                        sum_now \
                        ))
                    return
            sum_now = sum_now - prime_list[i] + prime_list[i + consecutive_len]
            i += 1
        consecutive_len -= 1
Example #5
0
def generate_pub_exp(lam, max_val=EXP_MAX):
    primes = list(prime.primes_below(min(max_val, lam)))

    e = choice(primes)
    while (lam % e == 0) or (e < 10):
        e = choice(primes)

    return e
def main1():
    primes = list(primes_below(18))
    products = 1
    for prime in primes:
        if products < 1000000:
            products *= prime
        else:
            break
    print products
def main():
    limit = 50000000
    p2_limit = sqrt(limit-2**3-2**4)
    p3_limit = 369
    p4_limit = 84
    p2_list = list(primes_below(p2_limit))
    p3_list = list(primes_below(p3_limit))
    p4_list = list(primes_below(p4_limit))
    numbers = set()

    for p2 in p2_list:
        for p3 in p3_list:
            for p4 in p4_list:
                num = sub([p2, p3, p4])
                if num < limit:
                    numbers.add(num)

    print len(numbers)
def getAnswer():        
    primesList = list(pyprimes.primes_below(7654321))
    print "Primes list loaded..."
    primesList = primesList[::-1]
    print "Primes list reversed..."
    for prime in primesList:
        print prime
        if checkPandigital(prime) == True:
            return prime
def main1():
    """Original solution"""
    primes = list(primes_below(1000))
    tmp = [0, 0] # tmp = [cnt, prime]
    for prime in primes:
        #print "-" * 50
        cnt = method1(prime)
        if tmp[0] < cnt:
            tmp = [cnt, prime]
    print tmp
def sub():
    primes = list(primes_below(17))

    print check(1406357289)

    below = 10 ** 9
    above = 10 ** 10
    for i in xrange(below, above):
        if check(i):
            print i
def getAnswer():
    truncPrimes = []
    primes = list(pyprimes.primes_below(1000000))
    primes = primes[::-1]
    
    for prime in primes:
        if len(truncPrimes) == 11:
            return sum(truncPrimes)
        if checkLeft(prime) and checkRight(prime):
            truncPrimes.append(prime)
Example #12
0
def main():
    primes = [prime for prime in pyprimes.primes_below(10000) if prime >= 1000]
    for prime in primes:
        permutations = [int("".join(x)) for x in itertools.permutations(str(prime)) if int("".join(x)) in primes]
        if len(permutations) >= 3:
            for permutation in itertools.permutations(permutations, 3):
                x, y, z = sorted(permutation)
                if x != 1487 and y - x == z - y and x != y and y != z and x != z:
                    print "%i %i %i" % (x, y, z)
                    exit()
Example #13
0
def get_prime_factors(value) -> List:
    prime_factors = []
    primes_below = list(pp.primes_below(value))
    number_body = value
    for prime in primes_below:
        if number_body == 1:
            break
        while number_body % prime == 0:
            prime_factors.append(prime)
            number_body = number_body / prime
    return prime_factors
Example #14
0
def nprimes(n):
	import pyprimes
	p_gen = pyprimes.primes_below(n)
	p_arr = []
	try:
		while True:
			p_arr.append(next(p_gen))
	except StopIteration:
		pass
	finally:
		del p_gen
	return p_arr
def main1():
    end = 10 ** 5 # 10 ** 8
    primes = list(primes_below(end/2))
    semiprimes = set()
    cnt = 0
    for prime1 in primes:
        for prime2 in primes:
            product = prime1 * prime2
            if product < end and product not in semiprimes:
                semiprimes.add(product)
                cnt += 1
    print cnt
def main():
    primes = list(primes_below(80))
    target = 11  # how many ?
    while True:
        ways = [1] + [0] * target
        for i in primes:
            for j in range(i, target + 1):
                ways[j] += ways[j - i]
        if ways[target] > 5000:
            break
        target += 1
    print target
def create_semiprimes():
    end = 10 ** 7
    lowerbound = 2000
    upperbound = 5000
    prime_nums = [x for x in primes_below(upperbound) if x > lowerbound]
    semiprimes = set()
    for prime1 in prime_nums:
        for prime2 in prime_nums:
            semiprime = prime1 * prime2
            if semiprime < end:
                semiprimes.add(semiprime)
    return semiprimes
def main2():
    lim = 10 ** 8 # 10 ** 8
    sqlim = int(sqrt(lim))
    primes = list(primes_below(lim/2 + 1))
    #print primes
    #print len(primes)
    pi = countprimes(primes, lim/2 + 1)
    #print pi
    #print len(pi)
    total = 0
    for k in xrange(1, pi[sqlim]+1):
        total += pi[int(lim/primes[k-1])] - k + 1
    print total
Example #19
0
def getPrimeFactOfNcR(n, r):
    primesPowerDict = {}
    primes = [prime for prime in pyprimes.primes_below(n)]
    for prime in primes:
        highestPowerOfPrime = getHighestPowerOfPInNFact(n, prime)
        primesPowerDict[prime] = highestPowerOfPrime
    primesBelowRIndex = bisect.bisect(primes, r)
    primesBelowNMinusRIndex = bisect.bisect(primes, (n - r))
    for i in xrange(primesBelowRIndex):
        hp = getHighestPowerOfPInNFact(r, primes[i])
        primesPowerDict[primes[i]] -= hp
    for i in xrange(primesBelowNMinusRIndex):
        hp = getHighestPowerOfPInNFact(n - r, primes[i])
        primesPowerDict[primes[i]] -= hp
    return primesPowerDict
def f(n):
    """Returns a list of prime numbers to be the longest
    when it is represented by the sum of the prime
    consecutive prime numbers less than or equal to n."""
    p = list(primes_below(n))
    i, s, t = 0, 0, 0
    while s < n:
        i, s = i+1, s+p[i]
    for j in xrange(i):
        if i-j < t:
            break
        for k in xrange(i-1, j, -1):
            if k-j < t:
                break
            l = p[j:k+1]
            if sum(l) in p:
                m, t = l, len(l)
    return m
Example #21
0
def search(max_p=1000, max_k=100, max_n=10, max_m=2):
    qs = []
    for p in pyprimes.primes_below(max_p + 1):
        for k in range(1, max_k + 1):
            qs.append(p**k)
    qs.sort()
    print("constructed qs", len(qs))

    for q in qs:
        print("q", q)
        for n in range(1, max_n + 1):
            for m in range(2, max_m + 1):
                print(n, m)
                tmp = pair(q, n, m)
                if pp.is_ddp_new(*tmp):
                    print(tmp)
                    print(p, k, n, m)
                    print("")
def method1(prime):
    primes = list(primes_below(prime))
    sum = 0
    max_cnt = 0
    for start_index in range(len(primes)):
        index = start_index
        cnt = 0
        while True:
            sum += primes[index]
            cnt += 1
            if sum == prime and max_cnt < cnt:
                max_cnt = cnt
                break
            elif sum >= prime:
                break
            index += 1
        sum = 0
    #print "prime", prime, "max_cnt", max_cnt
    return max_cnt
def main1():
    #answer = 0
    for rep_digits in xrange(3, 100, 3):
        # rep_digits is the number of digits of the replacement characters
        for n in xrange(rep_digits+1, 100):
            # list of prime numbers of n digits
            primes = [s for s in list(primes_below(10**n)) if s>10**(n-1)]
            for prime in primes:
                for k, L in g(prime, rep_digits):
                    if k > 2:
                        break
                    if L[-1] >= n-1:
                        break
                    cnt_non_prime = 0 # counter of non-prime
                    for replaced_num in replace(prime, L):
                        if replaced_num not in primes:
                            cnt_non_prime += 1
                        if cnt_non_prime > 2:
                            break
                    if cnt_non_prime == 2:
                        return prime
def main():
    listb = list(primes_below(1000))
    listb = listb[1:]

    max_cnt = 0
    max_a = 0
    max_b = 0
    for b in listb:
        # print b
        for a in [x for x in xrange(-b / 40 - 40, 1000) if x % 2 != 0]:
            cnt = 0
            # print "(a, b)", (a, b)
            while isprime(cnt ** 2 + a * cnt + b):
                cnt += 1
                # print cnt
            if max_cnt < cnt:
                max_cnt = cnt
                max_a = a
                max_b = b
    # print listb
    # print "max_cnt", max_cnt, "max_a", max_a, "max_b", max_b
    print max_a * max_b
def main():
    limit = 10000
    primes = list(primes_below(10000))
    composites = [] # odd composites
    for odd in xrange(1, limit, 2):
        # make composites up to limit
        if is_odd_composite(odd):
            composites.append(odd)
        else:
            continue
    for c in composites:
        if c > 33:
        # We already know up through 33
            x2 = compute_conject(primes, c)
            if x2 == False:
                """print the first odd composite that cannot
                be written as the sum of a prime and twice the square"""
                print c
                break
            else:
                continue
        else:
            continue
Example #26
0
import math
import pyprimes

start = time.time()

limit = 40000000
chain = 25
Sum = 0

## sieve to generate the value of phi
phi = list(range(limit + 1))

for i in range(2, limit + 1):
    if phi[i] == i: # i is prime
        for j in range(i, limit + 1, i):
            phi[j] = int(phi[j]*(i - 1)/i)

# loop over the prime to determine which ones have an iterative chain of length 25
for p in pyprimes.primes_below(limit):
    cour = p - 1
    for i in range(2, chain):
        cour = int(phi[cour])
        if cour == 1 and i < chain - 1:
            break
        if cour == 1 and i == chain - 1:
            Sum += p

print(Sum)
end = time.time() - start
print('in %f sec' % end)
def main():
    primes = list(primes_below(2*10**6))
    print sum(primes)
Example #28
0
File: search.py Project: yawara/gbc
import math

from gbc_without_sage import pair_int
import pyprimes

MAX_P = 1000000
MAX_K = int(math.log(MAX_P, 2))
pks = []
for p in pyprimes.primes_below(MAX_P):
    for k in range(1, MAX_K):
        pks.append(p ** k)
pks.sort()

MAX_VALID_DEGREE = MAX_P + 1
print(MAX_VALID_DEGREE)


def is_new_record(order, degree):
    if degree > MAX_VALID_DEGREE:
        raise Exception
    for pk in pks:
        if pk + 1 > degree:
            break
        else:
            q = pk

    h = degree - (q + 1)

    return order > q ** 2 + q + 1 + h

Example #29
0
import sys
import pyprimes

arg = int(sys.argv[1])

p = pyprimes.primes_below(arg)

total = 0
for i in p:
    total += i
print("Answer: ", total)
Example #30
0
def getPrimes(n):
	global primes
	primes = [prime for prime in  pyprimes.primes_below(n)] 
Example #31
0
def compute_P(n):
    prime_list = list(py.primes_below(n))
    return compute_Pnk(n, len(prime_list), prime_list)
Example #32
0
def lrgstNumDiv(n):
    t = 1
    for x in list(primes_below(n)):
        t *= x**numPowers(x,n)
    return t
def listOfPrimes(number):
    x = list(pyprimes.primes_below(number))
    return x
################################################################################
#                           Project Euler Problem 50 Solution
#                                   by Mike Kane
#
#   The prime 41, can be written as the sum of six consecutive primes:
#
#   41 = 2 + 3 + 5 + 7 + 11 + 13
#   This is the longest sum of consecutive primes that adds to a prime below one-hundred.
#
#   The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
#
#   Which prime, below one-million, can be written as the sum of the most consecutive primes?
################################################################################

import pyprimes
pyprimes.primes_below(1000000)
primesList = []

for x in pyprimes.primes_below(1000000):
    primesList.append(x)

def getAnswer():
    ''' program needs to check to see if any combination of prime numbers from list adds up to equal primeToBeChecked '''
    flippedPrimesList = reversed(primesList)
    numberToCheck = 0
    
    for number in flippedPrimesList:
        while numberToCheck < number:
            pass

Example #35
0
            continue
        prime_1a, prime_1b = pair1
        prime_2a, prime_2b = pair2
        four_primes = frozenset((prime_1a, prime_1b, prime_2a, prime_2b))
        if four_primes in seen_pairs_of_pairs:
            continue  # avoid repeats
        if (
                is_good_pair(prime_1a, prime_2a)
                and is_good_pair(prime_1a, prime_2b)
                and is_good_pair(prime_1b, prime_2a)
                and is_good_pair(prime_1b, prime_2b)
        ):  # "cross check" - don't recompute is_good_pair(pair1) and ...(pair2)
            print(four_primes)
            seen_pairs_of_pairs.add(four_primes)
            yield four_primes


if __name__ == "__main__":
    prime_pool = list(primes_below(10000))  # arbitrary limit
    prime_pool.remove(2)  # ends with 2 --> not prime
    prime_pool.remove(5)  # ends with 5 --> not prime
    good_pairs = get_good_pairs(prime_pool)  # constant time: about ~10 secs
    print("done getting pairs")

    for good_pop in gen_good_pairs_of_pairs(good_pairs):
        for prime in prime_pool:  # the fifth prime
            if all(is_good_pair(prime, pop_prime) for pop_prime in good_pop):
                five_primes = list(good_pop) + [prime]
                print(f"winner: {five_primes}, sum={sum(five_primes)}")
                break  # prime_pool is ascending, so done with this foursome
Example #36
0
##
##2**4*3**2*5*7*11*13*17*19

from primeList import *
from pyprimes import *

# Function to find the highest power necessary for an integer.
def numPowers(x,n):
    y = 1
    while x**(y+1) <= n:
        y += 1
    return y

# Multiplies primes raised to correct power.
def lrgstNumDiv(n):
    t = 1
    for x in list(primes_below(n)):
        t *= x**numPowers(x,n)
    return t

print(lrgstNumDiv(20))

# There's a nice pythonic one-liner way to do this using logarithms and reduce:
from math import floor, log
from operator import mul
from functools import reduce
from pyprimes import primes_below

print(reduce(mul,[p**floor(log(20,p)) for p in primes_below(20)]))

Example #37
0
def sum_of_primes():
    return sum(pyprimes.primes_below(2000000))
Example #38
0
#!/usr/bin/env python2.7
# vim: fileencoding=utf-8
from __future__ import unicode_literals
import pyprimes

primes = set()
for i in pyprimes.primes_below(1000000):
    primes.add(i)

count = 0
for i in primes:
    s = str(i)
    b = True
    for _ in range(len(s)):
        if int(s) not in primes:
            b = False
            break
        s = s[1:] + s[0]
    if b:
        count += 1
print count
Example #39
0
# Problem 10
# Find the sum of all the primes below two million.
import pyprimes

print("Sum of primes; {0}").format(sum(pyprimes.primes_below(2000000)))  #Lol one-liner
Example #40
0
import sys
import pyprimes

arg = int(sys.argv[1])

p = pyprimes.primes_below(arg)

total = 0
for i in p:
    total += i
print("Answer: ",total)
Example #41
0
	def findPrimes(n):
		self.primes = [prime for prime in pyprimes.primes_below(n)]
Example #42
0
def pair(n):
    return order(n), degree(n)


def bc_order(p, k):
    q = p**k
    return q**2 + q + 1


def bc_degree(p, k):
    q = p**k
    return q + 1


def bc_pair(p, k):
    return bc_order(p, k), bc_degree(p, k)


if __name__ == '__main__':
    rtv = []
    for p in pyprimes.primes_below(100):
        for k in range(2, 10):
            rtv.append((bc_pair(p, k), (p, k)))

    for n in range(2, 1000):
        rtv.append((pair(n), n))

    rtv.sort()
    for r in rtv:
        print(r)
Example #43
0
# https://code.google.com/codejam/contest/6254486/dashboard#s=p2
import sys
import pyprimes

factor_primes = list(pyprimes.primes_below(pyprimes.nth_prime(1e5)))


def log(*messages):
    return  # silent
    for message in messages:
        print message,
    print


class CoinCandidate(object):
    def __init__(self, value):
        self.value = value
        self.bases = range(2, 10 + 1)
        self.base_values = dict((i, int(self.value, i)) for i in self.bases)
        self.base_divisors = dict()
        self.prime_bases = set()

    def is_invalid(self):
        return len(self.prime_bases) > 0

    def is_valid(self):
        # 2 - 10
        return len(self.base_divisors) == 9

    def is_done(self):
        return self.is_valid() or self.is_invalid()
Example #44
0
 #N, J = 16, 50
 #N, J = 6, 3
 count = 0
 #primes = list(pyprimes.primes_below(10 ** (N/2) + 2))
 #print len(primes)
 for i in xrange(1 << (N - 2)):
     s = "1%s1" % bin(i)[2:].zfill(N - 2)
     divisors = []
     for base in xrange(2, 11):
         number = int(s, base)
         if is_probable_prime(number):
             break
         #else:
         #    divisors.append(1)
         sqrt_number = number**0.5
         for prime in pyprimes.primes_below(10**(N / 5) + 2):
             if number % prime == 0:
                 divisors.append(prime)
                 break
             if prime > sqrt_number:
                 break
         #print divisors, base
         if len(divisors) < base - 1:
             #print "%s is prime in base %s" % (s, base)
             break
     else:
         print s, " ".join(map(str, divisors))
         count += 1
         if count == J:
             break
 else:
Example #45
0
def pair(n):
    return order(n), degree(n)


def bc_order(p, k):
    q = p ** k
    return q ** 2 + q + 1


def bc_degree(p, k):
    q = p ** k
    return q + 1


def bc_pair(p, k):
    return bc_order(p, k), bc_degree(p, k)


if __name__ == "__main__":
    rtv = []
    for p in pyprimes.primes_below(100):
        for k in range(2, 10):
            rtv.append((bc_pair(p, k), (p, k)))

    for n in range(2, 1000):
        rtv.append((pair(n), n))

    rtv.sort()
    for r in rtv:
        print(r)
Example #46
0
def main():
    print sum([e for e in pyprimes.primes_below(2000000)])
Example #47
0
    first = 0
    last = len(item_list) - 1
    while (first <= last):
        mid = (first + last) // 2
        if item_list[mid] == item:
            return mid
        elif mid < len(item_list) - 1 and item_list[mid] < item and item_list[
                mid + 1] > item:
            return mid
        else:
            if item < item_list[mid]:
                last = mid - 1
            else:
                first = mid + 1


highest = 10**8
#primesBelow = list(pr.primes_below(math.sqrt(highest)))
primesBelow2 = list(pr.primes_below(highest / 1.99))
primesBelow = primesBelow2[:binary_search(primesBelow2, math.sqrt(highest)) +
                           1]

count = 0
for i in range(len(primesBelow)):
    prime1 = primesBelow[i]
    div = (highest / prime1) // 1
    count += binary_search(primesBelow2, div) - binary_search(
        primesBelow, prime1) + 1

print(count)
Example #48
0
# So the first few Hamming numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15.
# There are 1105 Hamming numbers not exceeding 108.

# We will call a positive number a generalised Hamming number of type n, if it has no prime factor larger than n.
# Hence the Hamming numbers are the generalised Hamming numbers of type 5.

# How many generalised Hamming numbers of type 100 are there which don't exceed 109?


import pyprimes
import math
n = 100
limit = math.pow(10, 9)

# generate liste of prime numbers less than 100
primes = pyprimes.primes_below(n)
primeList = []
for i in primes:
    primeList.append(i)


def hammer(primeList, cour, limit):
    if len(primeList) == 0:
        if cour>limit:
            return(0)
        else:
            return(1)
    else:
        count = 0
        while cour <= limit:
            # recursion
Example #49
0
import pyprimes

primes = pyprimes.primes_below(5000)
#primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 ]

rtv = []

for p in primes:
  for k in range(1, 30):
    q = p ** k
    n = q ** 2 + q + 1
    rtv.append((n,q+1,p,k))
rtv.sort()

rtv_com = []
tmp = 0
for (n, d, p, k) in rtv:
  rtv_com.append((n,d,p,k,d-tmp))
  tmp = d
Example #50
0
def main():
    primes = [prime for prime in pyprimes.primes_below(1000000) if prime == 2 or prime == 5 or ('0' not in str(prime) and '2' not in str(prime) and '4' not in str(prime) and '5' not in str(prime) and '6' not in str(prime) and '8' not in str(prime))]
    print len([prime for prime in primes if len([0 for i in xrange(len(str(prime))) if int(str(prime)[i:] + str(prime)[:i]) not in primes]) == 0])