Exemplo n.º 1
0
def f1(n):
  arr = primeBoolList(n)

  pdt = pow(2, trunc(log(n, 2)));

  for i in range(3, n+1, 2):
    if(arr[i]):
      k = trunc(log(n, i))
      pdt *= pow(i, k)
            
  return pdt
Exemplo n.º 2
0
def f(count, beg, end, sum):
  L = primeBoolList(end)
  i = beg
  while i <= end:
    k = trunc(log10(i))
    k = pow(10, k)
    f = i / k
    
    # check the most left digit
    if f not in (2,3,5,7):
      i += k
      continue
    
    # left to right
    j = i
    while L[j] and k > 0:
      j = j % k
      k /= 10
    if k == 0:
      # right to left
      j = i / 10
      while L[j] and j != 0:
        j /= 10
      if j == 0:
        sum += i
        count -= 1
        if count == 0:
          return sum
          
    # check the most right digit
    if i % 10 == 7:
      i += 6
    else:
      i += 2
  
  if N > 0:
    return f(count, end+1, end*10, sum)
Exemplo n.º 3
0
# Answer: 296962999629

from prime import primeBoolList


def arePermutation(m, n):
    c = [0] * 10
    for i in range(4):
        c[m % 10] += 1
        c[n % 10] -= 1
        m /= 10
        n /= 10
    return c == [0] * 10


l = primeBoolList(9999)

i = 1001

while i <= 9999:
    if i != 1487 and l[i]:
        j = i + 2
        k = i + 4
        while k <= 9999:
            if l[j] and l[k]:
                if arePermutation(i, j) and arePermutation(i, k):
                    print "%d%d%d" % (i, j, k)

            j += 2
            k += 4
Exemplo n.º 4
0
#   n^2 + 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 prime import primeBoolList

MAX = 1000

pb = primeBoolList(2 * MAX * MAX + MAX)

mn = 0
mm = 0
for a in range(1 - MAX, MAX):
    for b in range(1 - MAX, MAX):
        n = 0
        while True:
            c = n * (n + a) + b
            if c < 0 or not pb[c]:
                break
            n += 1
        if n > mn:
            mn = n
            mm = a * b
Exemplo n.º 5
0
#
# 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 math import trunc
from math import log10

from prime import primeBoolList
from util import pow

N = 1000000
L = primeBoolList(N)


def rotate(n):
    k = trunc(log10(n))
    if k == 0:
        return n
    return n / 10 + n % 10 * pow(10, k)


s = 0
for i in range(2, N + 1):
    if L[i]:
        c = 1
        k = rotate(i)
        while k != i and L[k]: