def goldbach(n):
    kontrol = allprimes(n)
    for p in kontrol:
        if(int(sqrt((n-p)/2))==sqrt((n-p)/2)):
            return(1)
            break
    else:
        return(0)
Beispiel #2
0
def goldbach(n):
    kontrol = allprimes(n)
    for p in kontrol:
        if (int(sqrt((n - p) / 2)) == sqrt((n - p) / 2)):
            return (1)
            break
    else:
        return (0)
#    It turns out that the formula will produce 40 primes for the consecutive integer values 0≤n≤390≤n≤39. However, when n=40,40^2+40+41=40(40+1)+41n=40,40^2+40+41=40(40+1)+41 is divisible by 41, and certainly when n=41,41^2+41+41n=41,41^2+41+41 is clearly divisible by 41.
#     The incredible formula n^2−79n+1601 was discovered, which produces 80 primes for the consecutive values 0≤n≤790≤n≤79. The product of the coefficients, −79 and 1601, is −126479.
#     Considering quadratics of the form:
#         n^2+an+b, where |a|<1000|a|<1000 and |b|≤1000|b|≤1000
#         where |n||n| is the modulus/absolute value of nn
#        e.g. |11|=11|11|=11 and |−4|=4|−4|=4
#        Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.

from functions import allprimes
from functions import isprime

def quadratic(a,b):
    primecount = 0
    for n in range(100):
        if(isprime(n**2+a*n+b)==1):
            primecount += 1
        else:
            break
    return(primecount)

blist = allprimes(1000)
maxab = 1
sonuc = None

for a in range(-999,1000):
    for b in blist:
        if(quadratic(a,b)>maxab):
            maxab = quadratic(a,b)
            sonuc = a*b
print(sonuc)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# https://projecteuler.net/problem=266

from functions import allprimes
from math import sqrt
from bisect import bisect_left

primes = allprimes(190)

p = 1
for n in primes:
    p = p * n

kok = sqrt(p)


def euler(n):
    nrs = {1}
    for m in primes[n::2]:
        nrs |= {nr * m for nr in nrs}
    return (sorted(nrs))


nrs1 = euler(0)
nrs2 = euler(1)

maxpsqr = 0

for m in nrs1:
    psqr = nrs2[bisect_left(nrs2, kok / m) - 1] * m
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
# There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.
# What 12-digit number do you form by concatenating the three terms in this sequence?

from functions import allprimes
primes = allprimes(10000)[168:]
sonuc = 0
for n in primes:
    for m in range(2, 5000 - int(n / 2), 2):
        if (primes.count(n + m) == 1 and primes.count(n + 2 * m) == 1):
            for i in str(n):
                if (str(n + m).count(i) == 0 or str(n + 2 * m).count(i) == 0):
                    break
            else:
                for j in str(n + m):
                    if (str(n + 2 * m).count(j) == 0 or str(n).count(j) == 0):
                        break
                else:
                    for k in str(n + 2 * m):
                        if (str(n).count(k) == 0 or str(n + m).count(k) == 0):
                            break
                    else:
                        print(n, n + m, n + 2 * m)
                        sonuc += 1
    if (sonuc == 2):
        break
Beispiel #6
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
# Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
# NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

from functions import allprimes

primes = allprimes(750000)

rightprimes = []
for n in primes[4:]:
    temp = n
    n = list(str(n))
    for i in range(1, len(n)):
        n.pop()
        n = ''.join(str(i) for i in n)
        if (primes.count(int(n)) == 0):
            break
        n = list(str(n))
    else:
        rightprimes.append(temp)

leftprimes = []
for n in rightprimes:
    temp = n
    n = list(str(n))
    for i in range(1, len(n)):
        n.remove(n[0])
        n = ''.join(str(i) for i in n)
        if (primes.count(int(n)) == 0):
Beispiel #7
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# For a prime p let S(p) = (∑(p-k)!) mod(p) for 1 ≤ k ≤ 5.
# For example, if p=7,
# (7-1)! + (7-2)! + (7-3)! + (7-4)! + (7-5)! = 6! + 5! + 4! + 3! + 2! = 720+120+24+6+2 = 872.
# As 872 mod(7) = 4, S(7) = 4.
# It can be verified that ∑S(p) = 480 for 5 ≤ p < 100.
# Find ∑S(p) for 5 ≤ p < 108.
# Soruda verilen S fonksiyonu (p-5)!+(p-4)!+(p-3)!+(p-2)!+(p-1)! (mod p) şeklinde yazılabilir.
# Bu ifadeyi (p-1)! parantezine alıp (p-2)(mod p) yerine -2 yazıldığında 
# (elbette diğerleri için de aynı şeyi yaparak) ve Wilson Teoremini kullanarak S(p)=-3/8(mod p) sonucuna ulaşılır.
# Buradan da S(p)=(p-3)/8 (mod p) elde edilir.
# Kolayca S(p)'nin ancak p'nin asal olduğu durumlarda sıfırdan farklı değer verdiği görülebilir.
# p'nin asal olduğu bilinince Fermatın Küçük Teoremi kullanılarak paydadaki 8'in tersi bulunabilir.

from functions import allprimes

def s(p):
    return(((p-3)*pow(8,p-2,p))%p)

primes = allprimes(10**8)[2:]
cozum = 0

for p in primes:
    cozum += s(p)

print(cozum)
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# A number consisting entirely of ones is called a repunit. We shall define R(k) to be a repunit of length k; for example, R(6) = 111111.
# Let us consider repunits of the form R(10^n).
# Although R(10), R(100), or R(1000) are not divisible by 17, R(10000) is divisible by 17. Yet there is no value of n for which R(10^n) will divide by 19. In fact, it is remarkable that 11, 17, 41, and 73 are the only four primes below one-hundred that can be a factor of R(10^n).
# Find the sum of all the primes below one-hundred thousand that will never be a factor of R(10^n).
# https://en.wikipedia.org/wiki/Repunit adresinden de görülebileceği gibi bu sayılar R(n)=10^n-1 şeklinde yazılabiliyor.

from functions import allprimes

primes = allprimes(100000)

bolen = 3
L = 10**20

for n in primes:
    if(pow(10, L, n) != 1):
        bolen += n
print(bolen)

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Consider the divisors of 30: 1,2,3,5,6,10,15,30.
# It can be seen that for every divisor d of 30, d+30/d is prime.
# Find the sum of all positive integers n not exceeding 100 000 000 such that for every divisor d of n, d+n/d is prime.

from functions import allprimes

LIMIT = 10**8
primes = allprimes(LIMIT)


def kontrol(n):
    if n % 4 == 0: return 0
    r = int(n**0.5) + 1
    for i in range(1, r + 1):
        if (n % i == 0):
            if i + n / i not in s:
                return 0
    return 1


s = set(primes)
toplam = sum(p - 1 for p in primes if kontrol(p - 1))

print(toplam)
Beispiel #10
0
from functions import allprimes
from functions import isprime
import random
deneme = 0
koltuklar=allprimes(100)
while(len(koltuklar)>0):
    tercih = random.randint(1,200)%98
    deneme += 1
    if(isprime(tercih)==1 and koltuklar.count(tercih)==1):
        koltuklar.remove(tercih)
print(deneme)

Beispiel #11
0
    i, n = 2, m
    while (i <= m / 2):
        if n % i == 0:
            carpanlar.append(i)
            n = n / i
        else:
            i += 1
    sonuc = []
    while not (len(carpanlar) == 0):
        a = carpanlar.count(carpanlar[0])
        sonuc.append(carpanlar[0]**a)
        carpanlar = carpanlar[a:]
    return (sonuc)


asal = allprimes(200000)
for n in range(130000, 200000):
    if (asal.count(n) == 1):
        continue
    a = primedivisors(n)
    if (asal.count(n + 1) == 1):
        continue
    b = primedivisors(n + 1)
    if (asal.count(n + 2) == 1):
        continue
    c = primedivisors(n + 2)
    if (asal.count(n + 3) == 1):
        continue
    d = primedivisors(n + 3)
    if (len(a) != 4 or len(b) != 4 or len(c) != 4 or len(d) != 4):
        continue
Beispiel #12
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.
# There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.
# What 12-digit number do you form by concatenating the three terms in this sequence?

from functions import allprimes
primes=allprimes(10000)[168:]
sonuc=0
for n in primes:
    for m in range(2,5000-int(n/2),2):
        if(primes.count(n+m)==1 and primes.count(n+2*m)==1):
            for i in str(n):
                if(str(n+m).count(i)==0 or str(n+2*m).count(i)==0):
                    break
            else:
                for j in str(n+m):
                    if(str(n+2*m).count(j)==0 or str(n).count(j)==0):
                        break
                else:
                    for k in str(n+2*m):
                        if(str(n).count(k)==0 or str(n+m).count(k)==0):
                            break
                    else:
                        print(n,n+m,n+2*m)
                        sonuc += 1
    if(sonuc==2):
        break
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
# What is the largest n-digit pandigital prime that exists?

from functions import allprimes
from functions import ispandigital

primes=allprimes(10**7)[::-1]

for n in primes:
    if(ispandigital(n)==1):
        print(n)
        break
# It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
# 9 = 7 + 2×1^2
# 15 = 7 + 2×2^2
# 21 = 3 + 2×3^2
# 25 = 7 + 2×3^2
# 27 = 19 + 2×2^2
# 33 = 31 + 2×1^2
# It turns out that the conjecture was false.
# What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

from functions import allprimes
from math import sqrt

def goldbach(n):
    kontrol = allprimes(n)
    for p in kontrol:
        if(int(sqrt((n-p)/2))==sqrt((n-p)/2)):
            return(1)
            break
    else:
        return(0)

asal = allprimes(10000)

for n in range(9,10000,2):
    if(asal.count(n)==1):
        continue
    if(goldbach(n)==0):
        print(n)
        break
Beispiel #15
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# What is the largest prime factor of the number 600851475143 ?
from functions import allprimes

primes=allprimes(10000)
number,i=600851475143,1
while not (number%primes[-i]==0):
    i +=1
print(primes[-i])
Beispiel #16
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
# Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
# NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

from functions import allprimes

primes=allprimes(750000)

rightprimes = []
for n in primes[4:]:
    temp = n
    n=list(str(n))
    for i in range(1,len(n)):
        n.pop()
        n=''.join(str(i) for i in n)
        if(primes.count(int(n))==0):
            break
        n=list(str(n))
    else:
        rightprimes.append(temp)

leftprimes =[]
for n in rightprimes:
    temp = n
    n=list(str(n))
    for i in range(1,len(n)):
        n.remove(n[0])
        n=''.join(str(i) for i in n)
        if(primes.count(int(n))==0):
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 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?
from functions import isprime
from functions import allprimes

asal = allprimes(4000)
toplam = []

for p in range(5,len(asal)):
    for a in range(0,len(asal)-p):
        if(isprime(sum(asal[a:a+p]))==1 and sum(asal[a:a+p])<1000000):
            toplam.append(sum(asal[a:a+p]))
print(toplam[-1])
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Consider the divisors of 30: 1,2,3,5,6,10,15,30.
# It can be seen that for every divisor d of 30, d+30/d is prime.
# Find the sum of all positive integers n not exceeding 100 000 000 such that for every divisor d of n, d+n/d is prime.

from functions import allprimes

LIMIT = 10**8
primes = allprimes(LIMIT)

def kontrol(n):
    if n%4==0 : return 0
    r=int(n**0.5)+1
    for i in range(1,r+1):
        if(n%i==0):
            if i+n/i not in s:
                return 0
    return 1

s=set(primes)
toplam=sum(p-1 for p in primes if kontrol(p-1))

print(toplam)
    carpanlar = []
    i,n=2,m
    while (i<=m/2):
        if n%i==0:
            carpanlar.append(i)
            n = n/i
        else:
            i += 1
    sonuc = []
    while not(len(carpanlar)==0):
        a=carpanlar.count(carpanlar[0])
        sonuc.append(carpanlar[0]**a)
        carpanlar = carpanlar[a:]
    return(sonuc)

asal = allprimes(200000)
for n in range(130000,200000):
    if(asal.count(n)==1):
        continue
    a = primedivisors(n)
    if(asal.count(n+1)==1):
        continue
    b = primedivisors(n+1)
    if(asal.count(n+2)==1):
        continue
    c = primedivisors(n+2)
    if(asal.count(n+3)==1):
        continue
    d = primedivisors(n+3)
    if(len(a)!=4 or len(b)!=4 or len(c)!=4 or len(d)!=4):
        continue
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Find the sum of all the primes below two million.
from functions import allprimes

print(sum(allprimes(2000000)))
Beispiel #21
0
from functions import allprimes
from functions import isprime
import random
deneme = 0
koltuklar = allprimes(100)
while (len(koltuklar) > 0):
    tercih = random.randint(1, 200) % 98
    deneme += 1
    if (isprime(tercih) == 1 and koltuklar.count(tercih) == 1):
        koltuklar.remove(tercih)
print(deneme)
Beispiel #22
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
# What is the largest n-digit pandigital prime that exists?

from functions import allprimes
from functions import ispandigital

primes=allprimes(10000000)[::-1]

for n in primes:
    if(ispandigital(n)==1):
        print(n)
        break
Beispiel #23
0
# 15 = 7 + 2×2^2
# 21 = 3 + 2×3^2
# 25 = 7 + 2×3^2
# 27 = 19 + 2×2^2
# 33 = 31 + 2×1^2
# It turns out that the conjecture was false.
# What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?

from functions import allprimes
from math import sqrt


def goldbach(n):
    kontrol = allprimes(n)
    for p in kontrol:
        if (int(sqrt((n - p) / 2)) == sqrt((n - p) / 2)):
            return (1)
            break
    else:
        return (0)


asal = allprimes(10000)

for n in range(9, 10000, 2):
    if (asal.count(n) == 1):
        continue
    if (goldbach(n) == 0):
        print(n)
        break
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# For a prime p let S(p) = (∑(p-k)!) mod(p) for 1 ≤ k ≤ 5.
# For example, if p=7,
# (7-1)! + (7-2)! + (7-3)! + (7-4)! + (7-5)! = 6! + 5! + 4! + 3! + 2! = 720+120+24+6+2 = 872.
# As 872 mod(7) = 4, S(7) = 4.
# It can be verified that ∑S(p) = 480 for 5 ≤ p < 100.
# Find ∑S(p) for 5 ≤ p < 108.
# Soruda verilen S fonksiyonu (p-5)!+(p-4)!+(p-3)!+(p-2)!+(p-1)! (mod p) şeklinde yazılabilir.
# Bu ifadeyi (p-1)! parantezine alıp (p-2)(mod p) yerine -2 yazıldığında
# (elbette diğerleri için de aynı şeyi yaparak) ve Wilson Teoremini kullanarak S(p)=-3/8(mod p) sonucuna ulaşılır.
# Buradan da S(p)=(p-3)/8 (mod p) elde edilir.
# Kolayca S(p)'nin ancak p'nin asal olduğu durumlarda sıfırdan farklı değer verdiği görülebilir.
# p'nin asal olduğu bilinince Fermatın Küçük Teoremi kullanılarak paydadaki 8'in tersi bulunabilir.

from functions import allprimes


def s(p):
    return (((p - 3) * pow(8, p - 2, p)) % p)


primes = allprimes(10**8)[2:]
cozum = 0

for p in primes:
    cozum += s(p)

print(cozum)
Beispiel #25
0
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# A composite is a number containing at least two prime factors. For example, 15 = 3 × 5; 9 = 3 × 3; 12 = 2 × 2 × 3.
# There are ten composites below thirty containing precisely two, not necessarily distinct, prime factors: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.
# How many composite integers, n < 10^8, have precisely two, not necessarily distinct, prime factors?
# 10^8'den yarısından küçük her asal için kendinden küçük eşit asal sayılarla çarpımından oluşturulan sayılar sayıldığında aranan sonuç bulunmuş oluyor.

from sympy import primepi
from functions import allprimes

sonuc = 0

LIMIT = 10**8
primes = allprimes(LIMIT // 2)

for n in primes:
    if (n < LIMIT**(0.5)):
        m = primepi(n)
    else:
        m = primepi(LIMIT // n)
    sonuc += m
print(sonuc)