示例#1
0
def main():
    ''' Driver function '''
    for n in count(1, 1):
        p = primes_nth(n)
        p_sqr = p**2
        rem = (pow(p - 1, n, p_sqr) + pow(p + 1, n, p_sqr)) % p_sqr
        if rem > 10**10:
            print(n)
            break
示例#2
0
    def get_mapped_prime(self, token: str):

        prime = self.opcode_to_prime.get(token)
        if prime:
            pass
        else:
            helper_hash = hashlib.blake2b(digest_size=3)
            helper_hash.update(token)
            prime = pyprimesieve.primes_nth(int(helper_hash.hexdigest(), 16))
            self.opcode_to_prime[token] = prime

        return prime
示例#3
0
def sum_S_to(n):
    ''' Returns the sum of values of S (as described in the problem) for all
    consecutive primes p1 and p2, p2 > p1 and 5 <= p1 <= n '''
    total = 0
    primes = primes_to(n)
    # Get p2 for last p1
    primes += [primes_nth(len(primes) + 1)]
    # The lowest positive value of n that satisfies both n = 0 (mod p2) and
    # n = p1 (mod 10**k) (k being the number of digits in p1) is S for that
    # pair of primes. The function (easily derived from modular relations)
    #    n(x) = pow_10*(x*p2 - p1*pow_10_mult_inv) + p1
    # (pow_10 is 10**k and pow_10_mult_inv is the multiplicative inverse of
    # 10**k mod p2) produces all valid values of n for a given p1,p2 pair
    for p1, p2 in zip(primes[2:-1], primes[3:]):
        p1_digits = int(log(p1, 10)) + 1
        pow_10 = 10**p1_digits
        pow_10_mult_inv = pow(pow_10, -1, p2)
        # Find the lowest value of x for which n(x) is positive
        x = int((-p1 / pow_10 + p1 * pow_10_mult_inv) / p2) + 1
        total += pow_10 * (x * p2 - p1 * pow_10_mult_inv) + p1
    return total
示例#4
0
 def test_smallnums_1(self):
     self.assertEqual(pyprimesieve.primes_nth(1), 2)
示例#5
0
 def test_bignums_3(self):
     self.assertEqual(pyprimesieve.primes_nth(81749371), 1648727417)
示例#6
0
 def test_bignums_2(self):
     self.assertEqual(pyprimesieve.primes_nth(94949), 1228567)
示例#7
0
 def test_bignums_1(self):
     self.assertEqual(pyprimesieve.primes_nth(10001), 104743)
示例#8
0
 def test_smallnums_2(self):
     self.assertEqual(pyprimesieve.primes_nth(4), 7)
示例#9
0
文件: e007.py 项目: horse-eye/euler
def ps_nth_super(nth):
    return primes_nth(nth)
示例#10
0
def _hash_char(val: str) -> int:
    if len(val) > 1:
        raise ValueError("char length must be 1")

    idx = ord(val)
    return primes_nth(idx)
示例#11
0
def _get_hash(val) -> int:
    if isinstance(val, int):
        return primes_nth(val)
    elif isinstance(val, str):
        return reduce(lambda x, y: x * _hash_char(y), val, 1)
示例#12
0
import string
import math
from random import randrange
import pyprimesieve as pp
# Possible digits from the lowest to the highest
DIGITS = '%s%s' % (string.digits, string.ascii_lowercase)
PRIMES = [pp.primes_nth(idx) for idx in range(1, 501)]

small_primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]  # etc.


def probably_prime(n, k):
    """Return True if n passes k rounds of the Miller-Rabin primality
    test (and is probably prime). Return False if n is proved to be
    composite.

    """
    if n < 2:
        return False
    for p in small_primes:
        if n < p * p:
            return True
        if n % p == 0:
            return False
    r, s = 0, n - 1
    while s % 2 == 0:
        r += 1
        s //= 2
    for _ in range(k):
        a = randrange(2, n - 1)
        x = pow(a, s, n)
示例#13
0
 def test_exception_2(self):
     with self.assertRaises(ValueError):
         pyprimesieve.primes_nth(-1111)
示例#14
0
from pyprimesieve import primes_nth
print primes_nth(10001)
示例#15
0
def getSolution(limit):
    return pyprimesieve.primes_nth(limit)
示例#16
0
 def test_exception_2(self):
     with self.assertRaises(ValueError):
         pyprimesieve.primes_nth(-1111)