def main():
    primesieve = PrimeSieve(1000000)
    primes = primesieve.primes
    sieve = primesieve.sieve

    def tryreplace(num):
        onum = num
        digits = []
        while num:
            digits.append(num % 10)
            num //= 10
        c = Counter(digits)
        for digit, ct in c.items():
            if ct > 1:
                familyct = 1
                for i in range(digit + 1, 10):
                    trynum = int(str(onum).replace(str(digit), str(i)))
                    if sieve[trynum]:
                        familyct += 1
                if familyct == 8:
                    return onum

    for prime in primes:
        c = Counter([int(num) for num in list(str(prime))])
        if any(ct > 1 for ct in c.values()):
            res = tryreplace(prime)
            if res:
                return res
def main():
    primesieve = PrimeSieve(1000000)
    primes = primesieve.primes

    def isPrime(num):
        for p in primes:
            if p**2 > num:
                break
            if num % p == 0:
                return False
        return True

    allcount = 13
    primecount = 8
    currside = 7
    while primecount / allcount >= 0.1:
        currside += 2
        a = currside**2
        newcorners = [
            a, a - currside + 1, a - 2 * currside + 2, a - 3 * currside + 3
        ]
        for corner in newcorners:
            if isPrime(corner):
                primecount += 1
        allcount += 4
    print(currside)
def main():
    primesieve = PrimeSieve(1000000)
    sieve = primesieve.sieve
    primes = primesieve.primes
    for i in count(35, 2):
        if sieve[i]:  #if this odd number is prime
            continue
        for j in count(1):
            if i - 2 * j * j < 0: return i
            if sieve[i - 2 * j * j]: break

    return "not found"
def main():
    '''
    for each 4 digit prime
    find permutations of the 4 digits
    keep the permutations that are prime also; discard perms starting with 0
    sort the remaining 4 digit prime permutations
    for each permutation, find differences between it and larger permutations
        store the differences
        for each difference, see if there's one that's twice as big as it
    
    '''
    primesieve = PrimeSieve(10000)
    primes = primesieve.primes
    sieve = primesieve.sieve

    def permute(num):
        digits = []
        while num:
            digits.append(num % 10)
            num //= 10
        pers = list(permutations(digits))
        primepers = set()
        for per in pers:
            if per[0] == 0: continue
            pervalue = 0
            for digit in per:
                pervalue *= 10
                pervalue += digit
            if sieve[pervalue]:
                primepers.add(pervalue)
        primepers = list(primepers)
        primepers.sort()
        return primepers

    #ans = []
    ans = set()
    for prime in primes:
        if prime < 1000: continue
        primepers = permute(prime)
        n = len(primepers)
        for i in range(n):
            diffs = []
            for j in range(i + 1, n):
                diffs.append(primepers[j] - primepers[i])
            diffset = set(diffs)
            for diff in diffs:
                if diff > 0 and 2 * diff in diffset:
                    ans.add((primepers[i], primepers[i] + diff,
                             primepers[i] + 2 * diff))

    return ans
示例#5
0
def other(limit=1000000):
    primesieve = PrimeSieve(limit)
    sieve = primesieve.sieve
    primes = primesieve.primes

    # Find largest window
    total = window = 0
    while total + primes[window] < limit:
        total += primes[window]
        window += 1

    while window > 0:
        # roll window
        total = sum(primes[i] for i in range(window))
        i = window
        while total < limit and i < len(primes):
            if sieve[total]: return total
            total += primes[i]
            total -= primes[i - window]
            i += 1
        window -= 1
from collections import *
from itertools import *
from random import *
from time import *
from functools import *
from fractions import *
from math import *
from pe_lib import PrimeSieve, FactorSieve
'''

'''
factor_sieve = FactorSieve(10**7)
prime_sieve = PrimeSieve(10**7)
sieve = prime_sieve.sieve
primes = prime_sieve.primes


def isPerm(num1, num2):
    return sorted(str(num1)) == sorted(str(num2))


def main():
    lowest = float('inf')
    lowestn = 0
    for i in range(2, 10**7):
        tot = factor_sieve.factor(i).totient()
        if isPerm(i, tot):
            if i / tot < lowest:
                lowest = i / tot
                lowestn = i
    return lowestn
示例#7
0
def main():
    prime_sieve = PrimeSieve(1000000)
    primes = prime_sieve.primes
    isprime = prime_sieve.isprime

    def check(a, b):
        s = str(a)
        t = str(b)
        return isprime(int(s + t)) and isprime(int(t + s))

    '''
    We're thinking we need around 4200 primes
    based on a 2.5% independent success on 10 pairs.

    Approach 1: set a hard limit
    - try all n choose 5 combinations
    - can prune if first 2, 3, 4, don't work
    - if no answer, then increase limit
    - if run too long, then lower limit

    Approach 2: increase limit slowly
    - for each prime, in in increasing order
    - first determine which smaller primes are compatible
    - then see if any cliques can be extended
    - we can't throw away the existing cliques

    How to show this is minimal sum? (optional)
    Take lowest 1-clique and add 4*highest prime considered
    Take lowest 2-clique and add 3*highest prime considered
    Take lowest 3-clique and add 2*highest prime considered
    Take lowest 4-clique and add 1*highest prime considered
    If the minimum of these is larger than the clique we found,
    then the clique we found has minimal sum.
    Otherwise increase the highest prime considered.
    '''
    '''
    Existing cliques: {}, {1}, {1, 3}, {1, 3, 4}, {1, 4}, {2}, {3}, {4}
    Add: {}, {1, 5}, {1, 4, 5}, {5}
    root (prefix tree)
        1 ->
            3 ->
                4 -> {}
            4 ->
                5 -> {}
            5 -> {}
        2 -> {}
        3 -> {}
        4 -> {}
        5 -> {}
    for each new prime p ascending:
        find all primes q<p compatible with p
        do a dfs over tree only going down paths where compatible and extend
        if depth hits 5, then we print and stop
    '''

    class Node:
        def __init__(self):
            self.children = {}

    cliques = Node()
    stack = []
    best = [float('inf')]
    for p in primes:
        if p >= best[0]: break

        cache = {}

        def is_compatible(q):
            if q not in cache:
                cache[q] = check(p, q)
            return cache[q]

        def dfs(node, curr):
            if len(stack) >= 4:
                best[0] = min(best[0], curr)
                print(stack + [p], curr)
            to_remove = []
            for q, childnode in node.children.items():
                if curr + q >= best[0]:
                    to_remove.append(q)
                    continue
                if is_compatible(q):
                    stack.append(q)
                    dfs(childnode, curr + q)
                    stack.pop()
            for q in to_remove:
                del node.children[q]
            node.children[p] = Node()

        dfs(cliques, p)
from collections import *
from itertools import *
from random import *
from time import *
from functools import *
from fractions import *
from math import *
from pe_lib import PrimeSieve, sqrt
'''

'''
n = 5 * 10**7
a = PrimeSieve(sqrt(n)).primes


def main():
    ans = set()
    for i in a:
        ii = i * i
        for j in a:
            jjj = j * j * j
            if ii + jjj >= n: break
            for k in a:
                s = ii + jjj + k * k * k * k
                if s < n:
                    ans.add(s)
                else:
                    break
    print(len(ans))