Пример #1
0
def prime_factor(x):
    ans = 0
    prime = primes(sqrt(x))
    for num in reversed(prime):
        if x % num == 0 and num > ans:
            ans = num
            print ans
Пример #2
0
def run() -> int:
    # Set the upper limit and generate enough primes generate primes
    limit = 5e7
    pra = primes(limit**.5 + 1)

    # We will be saving suitable numbers here
    below_limit = set()

    # Check all combinations of primes
    for i in pra:
        for j in pra:
            for k in pra:

                # Multiply and add the primes in the needed way
                sum_value = i**2 + j**3 + k**4

                # If the sum exceeds the limit, skip to next j, else save the sum
                if sum_value >= limit:
                    break
                else:
                    below_limit.add(sum_value)

            # Break loops sooner if the numbers are getting too big
            if i**2 + j**3 + 2**4 >= limit:
                break
        if i**2 + 2**3 + 2**4 >= limit:
            break

    return len(below_limit)
Пример #3
0
def run() -> int:
    # Generate squares of primes. Primes up to ten will suffice - we only use factorials up to 51, which means
    # numbers in this problem can't be divisible by primes higher than that
    pra = [a**2 for a in primes(10)]

    # Initialize the set for storing all numbers that appear in the Pascal triangle and then fill the said set
    pascal = set()

    for i in range(51):
        for j in range(i // 2 + 1):
            pascal.add(useful.binomial(i, j))

    # Check all numbers stored in 'pascal' and store the valid ones into 'valid'
    valid = set()
    for j in sorted(pascal):
        if useful.is_prime(j):
            # If the number itself is prime, it cannot be divided by any square of a prime. Add it to the valid ones.
            valid.add(j)
        else:
            # If any of the prime squares divide the number we're currently checking, add that number to 'valid'
            if not any(j % k == 0 for k in pra):
                valid.add(j)

    # Finally, return the sum of the squarefree numbers
    return sum(valid)
Пример #4
0
def foo():
	num = 600851475143
	is_sq, sqrt = is_square(num)
	primes_list = primes(sqrt + 1)
	for x in reversed(primes_list):
		if num % x == 0:
			print x
			return
	print num
Пример #5
0
def compute():
    isprime = primes(999999)

    def is_circular_prime(n):
        s = str(n)
        return all(is_prime(int(s[i:] + s[:i])) for i in range(len(s)))

    ans = sum(1 for i in isprime if is_circular_prime(i))
    return str(ans)
Пример #6
0
def run() -> int:
    pra = primes(5e7)
    valid = set()
    for i in range(len(pra)):
        for j in range(i, len(pra)):
            product = pra[i] * pra[j]
            if product < 1e8:
                valid.add(product)
            else:
                break

    return len(valid)
Пример #7
0
def main():
    primes_below_thousand = primes(1000)
    primes_below_thousand_copy = primes_below_thousand[:]
    n = 0
    product_of_coefficients = -1

    # Looping through quadratic equation
    for b in primes_below_thousand:
        for a in primes_below_thousand:
            i = 0
            # Positive a and b
            while True:
                quadratic_expression = i**2 + (a * i) + b
                if quadratic_expression not in primes_below_thousand_copy:
                    if is_prime(quadratic_expression):
                        primes_below_thousand_copy.append(quadratic_expression)
                    else:
                        if i - 1 > n:
                            n = i - 1
                            product_of_coefficients = a * b
                        break
                i += 1
            i = 0
            # Negative a and positive b
            while True:
                quadratic_expression = i**2 - (a * i) + b
                if quadratic_expression not in primes_below_thousand_copy:
                    if is_prime(
                            quadratic_expression) and quadratic_expression > 0:
                        primes_below_thousand_copy.append(quadratic_expression)
                    else:
                        if i - 1 > n:
                            n = i - 1
                            product_of_coefficients = -1 * a * b
                        break
                i += 1
    print(product_of_coefficients)
    print(primes_below_thousand_copy)
    print(time.time() -
          start)  # Printing total time of execution in milliseconds
Пример #8
0
def sieve(n):
    # n+1 used so zero is included in the array
    m = n+1
    numbers = [True] * m
    numbers[0] = numbers[1] = False
    for i in range(2, int(n**0.5)+1):
        if numbers[i]:
            for j in range(i*i, n+1, i):
                numbers[j] = False
    return numbers


def compute(n):
    total = 0
    primes = sieve(n)
    for i, val in enumerate(primes):
        if val:
            total += i
    return str(total)


print(compute(2000000))

# A much quicker way
import eulerlib
primes = eulerlib.primes(2000000)
total = 0
for prime in primes:
    total += prime
print(total)
def summation_of_primes(x):
    list_primes = eulerlib.primes(x)
    sum_of_list = sum(list_primes)
    return sum_of_list
Пример #10
0
def sumPrimes():
    total = sum(eulerlib.primes(1999999))
    return str(total)
Пример #11
0
    Calculate number of primes for n**2 + an + b
    Where a, b are integers and n starts from 0
    '''
    n = 0  # Num of primes
    while True:
        if not eulerlib.isprime(n**2 + (a * n) + b):
            break
        n += 1
    return n


ma, mb, mprimes = 0, 0, 0
factor = 100

# b must be prime becuase otherwise test fails when n == 0
brange = eulerlib.primes(1000)
nbrange = []
for prime in brange:
    nbrange.append(prime * -1)
brange += nbrange  # Add neg version of the primes
###

# Search the problem space...
for a in range(-9 * factor, 10 * factor):
    for b in brange:
        if eulerlib.isprime(a + b + 1):  # Cull at n == 1 (probably not needed)
            nprimes = nP4Quad(a, b)
            if nprimes > mprimes:
                ma, mb, mprimes = a, b, nprimes

print("Solution: a = %d, b = %d, nprimes = %d, a*b = %d" %
Пример #12
0
import eulerlib
primes=eulerlib.primes(1000)
def d(n,p):
    count=0
    if n<p:
        if n==0:
            return 1
        else:
            return 0
    elif n==p:
        return 1
    else:
        for i in range(primes.index(p)+1):
            count+=d(n-p,primes[i])
        return count

def p(n):
    count=0
    index=0
    while n>primes[index]:
        count+=d(n,primes[index])
        index+=1
    return count


i=5
while p(i)<5000:
    i+=1
print(i)
#problema 87
import eulerlib, math

primos = eulerlib.primes(math.sqrt(50000000)) #Lista de numeros primos menores que la raiz cuadrada de 50000000
numeros = {0}

for i in range(2,5): #Recorre los posibles exponentes para los primos [2,3,4]
	numerosaux = set()
	for p in primos: 
		q = p ** i #Prueba las posibles combinaciones entre exponentes y primos
		if q > 50000000:
			break
		for x in numeros:
			if x + q <= 50000000:
				numerosaux.add(x + q)
	numeros = numerosaux

m = len(numeros)
print m
Пример #14
0
from eulerlib import primes
print(sum(primes(2000000)))
#procedual code with out using built-in function will be uploaded soon 
Пример #15
0
def compute2():
	ans = sum(primes(1999999))
	return str(ans)
Пример #16
0
from eulerlib import is_square, primes

(is_sq, sqroot) = is_square(600851475143)

p = primes(sqroot + 1)

for i in range(0, len(p) + 1):
    j = 0 - i

    if 600851475143 % p[j] == 0:
        print(p[j])
        break
Пример #17
0
def main():
    answer = sum(primes(1999999))
    print(answer)
Пример #18
0
from eulerlib import primes

print sum(primes(2000000))

#142913828922
Пример #19
0
def calc(n):
    prime_list = primes(n)
    result = 0
    for num in prime_list:
        result += num
    return result