Exemplo n.º 1
0
def gen_primes(M=M, odd_only=False):
    from mathplus import sieve
    sieves = sieve(M)
    if odd_only:
        primes = [str(i) for i, f in enumerate(sieves) if f and i != 2]
    else:
        primes = [str(i) for i, f in enumerate(sieves) if f]
    
    f = open(_get_prime_file(M, odd_only), 'w')
    f.write(','.join(primes))
    f.close()
Exemplo n.º 2
0
def gen_primes(M=M, odd_only=False):
    from mathplus import sieve
    sieves = sieve(M)
    if odd_only:
        primes = [str(i) for i, f in enumerate(sieves) if f and i != 2]
    else:
        primes = [str(i) for i, f in enumerate(sieves) if f]

    f = open(_get_prime_file(M, odd_only), 'w')
    f.write(','.join(primes))
    f.close()
Exemplo n.º 3
0
#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?

#Answer:
#296962999629

from time import time

t = time()
from mathplus import sieve, permutations

ret = {}
primes = sieve(10000)
p = [i for i, f in enumerate(primes) if f and i >= 1000]
for n in p:
    c = []
    for i in permutations(
        (n // 1000, (n % 1000) // 100, (n % 100) // 10, n % 10)):
        if 0 in i: break
        m = i[0] * 1000 + i[1] * 100 + i[2] * 10 + i[3]
        if primes[m] and m not in c: c.append(m)
    if len(c) >= 3:
        c = sorted(c)
        if c[0] in ret: continue
        for i in range(1, len(c) - 1):
            for j in range(i):
                if c[i] * 2 - c[j] in c:
                    ret[c[0]] = (c[j], c[i], 2 * c[i] - c[j])
Exemplo n.º 4
0
#n² + an + b, where |a| < 1000 and |b| < 1000

#where |n| is the modulus/absolute value of n
#e.g. |11| = 11 and |−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.

#Answer:
#-59231

from time import time
t = time()
from mathplus import sieve, product

M = 15000
primes = sieve(M)
# one of (1+a+b, b*b+a*b+b) is not a prime, so n*n+a*n+b has less than range(b) primes
ab = [[a, b, b] for a, b in product(range(
    -1000 + 1, 1000, 2), [i for i in range(83, 1000, 2) if primes[i]])]

n = 0
lastone = None
while len(ab) > 1:
    lastone = ab[0]
    x = 2 * n + 1
    for v in ab:
        v[2] += x + v[0]
    ab = [[a, b, x] for a, b, x in ab if x > 0 and (x >= M or primes[x])]
    n += 1

lastone = ab[0]
Exemplo n.º 5
0
#!/usr/bin/python
# -*- coding: utf-8 -*-

#The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

#There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

#How many circular primes are there below one million?

#Answer:
	#55  

from time import time; t = time()
from mathplus import sieve

M = 1000000

S = sieve(M)
count = 2
for k in range(3, M, 2):
    if not S[k]: continue
    s = str(k)
    if any((i in s) for i in '024568'): continue
    for i in range(len(s)):
        if not S[int(s)]: break
        s = s[1:]+s[0]
    else:
        count += 1

print(count)#, time()-t
Exemplo n.º 6
0
#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×12
#15 = 7 + 2×22
#21 = 3 + 2×32
#25 = 7 + 2×32
#27 = 19 + 2×22
#33 = 31 + 2×12

#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?

#Answer:
#5777

from time import time
t = time()
from mathplus import sieve, isqrt

M = 10000
p = sieve(M)

for n in range(35, M, 2):
    if p[n]: continue
    for i in range(1, isqrt((n - 3) / 2) + 1):
        if p[n - 2 * i * i]: break
    else:
        print(n)  #, time()-t
        break
Exemplo n.º 7
0
#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×12
#15 = 7 + 2×22
#21 = 3 + 2×32
#25 = 7 + 2×32
#27 = 19 + 2×22
#33 = 31 + 2×12

#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?

#Answer:
	#5777

from time import time; t=time()
from mathplus import sieve, isqrt

M = 10000
p = sieve(M)

for n in range(35, M, 2):
    if p[n]: continue
    for i in range(1, isqrt((n-3)/2)+1):
        if p[n-2*i*i]: break
    else:
        print(n)#, time()-t
        break
Exemplo n.º 8
0
# -*- coding: utf-8 -*-

#The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

#There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

#How many circular primes are there below one million?

#Answer:
#55

from time import time
t = time()
from mathplus import sieve

M = 1000000

S = sieve(M)
count = 2
for k in range(3, M, 2):
    if not S[k]: continue
    s = str(k)
    if any((i in s) for i in '024568'): continue
    for i in range(len(s)):
        if not S[int(s)]: break
        s = s[1:] + s[0]
    else:
        count += 1

print(count)  #, time()-t
Exemplo n.º 9
0
    #n² + an + b, where |a| < 1000 and |b| < 1000

    #where |n| is the modulus/absolute value of n
    #e.g. |11| = 11 and |−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.

#Answer:
	#-59231

from time import time; t=time()
from mathplus import sieve, product

M = 15000
primes = sieve(M)
# one of (1+a+b, b*b+a*b+b) is not a prime, so n*n+a*n+b has less than range(b) primes
ab = [[a, b, b] for a, b in product(
    range(-1000+1, 1000, 2),
    [i for i in range(83, 1000, 2) if primes[i]]
    )]

n = 0
lastone = None
while len(ab) > 1:
    lastone = ab[0]
    x = 2*n+1
    for v in ab:
        v[2] += x+v[0]
    ab = [[a, b, x] for a, b, x in ab if x > 0 and (x >= M or primes[x])]
    n += 1
Exemplo n.º 10
0
# -*- 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?

#Answer:
	#296962999629

from time import time; t=time()
from mathplus import sieve, permutations

ret = {}
primes = sieve(10000)
p = [i for i, f in enumerate(primes) if f and i >= 1000]
for n in p:
    c = []
    for i in permutations((n//1000, (n % 1000)//100, (n % 100)//10, n%10)):
        if 0 in i: break
        m = i[0]*1000+i[1]*100+i[2]*10+i[3]
        if primes[m] and m not in c: c.append(m)
    if len(c) >= 3:
        c = sorted(c)
        if c[0] in ret: continue
        for i in range(1, len(c)-1):
            for j in range(i):
                if c[i]*2-c[j] in c:
                    ret[c[0]] = (c[j], c[i], 2*c[i]-c[j])
assert len(ret) == 2