Пример #1
0
def is_truncatable_prime(n):
    if n in ["2", "3", "5", "7"]: return False
    l = len(n)

    for i in range(0, l):
        m = int(n[i:])
        if not lib.is_prime(m): return False

        m = int(n[0:l-i])
        if not lib.is_prime(m): return False

    return True
Пример #2
0
def ok(a, b):
    x = lib.concat(a, b)
    if x < N:
        if not sieve[x]:
            return False
    elif not lib.is_prime(x):
        return False

    y = lib.concat(b, a)
    if y < N:
        if not sieve[y]:
            return False
    elif not lib.is_prime(y):
        return False
    return True
Пример #3
0
def count_quandratic_primes(a, b):
    count = 0
    n = 0
    while n == count:
        count += int(is_prime(n ** 2 + a * n + b))
        n += 1
    return count
Пример #4
0
def get_prime_n(n):
    count = 0
    num = 1
    while count != n:
        num += 1
        if (is_prime(num)):
            count += 1
    return num
Пример #5
0
def euler_27(max_a, max_b):
    a = range(-max_a, max_a + 1)
    #n = 0 means that the equation simplifies to b, so b must be prime.
    b = filter(lambda x: lib.is_prime(abs(x)), range(-max_b, max_b + 1))

    vars = filter(a_filter, itertools.product(a, b))
    solution = max((consec_primes_for_eq(*var), var) for var in vars)[1]
    return operator.mul(*solution)
Пример #6
0
def euler_46():
    r = itertools.count(3, 2)
    composite_odd = filter(lambda x: not lib.is_prime(x), r)

    for n in composite_odd:
        squares = itertools.takewhile(lambda x: x < n,
                (2 * (i ** 2) for i in itertools.count()))
        if not any(map(lib.is_prime, (n - s for s in squares))):
            return n
Пример #7
0
def prime_factorization(n):
    primes = []
    candidates = xrange(2, n + 1)
    candidate = 2

    while not primes and candidate in candidates:
        if n % candidate == 0 and lib.is_prime(candidate):
            primes = primes + [candidate] + prime_factorization(n / candidate)
        candidate += 1

    return primes
Пример #8
0
def prime_factorization(n):
    primes = []
    candidates = xrange(2, n+1)
    candidate = 2

    while not primes and candidate in candidates:
        if n%candidate == 0 and lib.is_prime(candidate):
            primes = primes + [candidate] + prime_factorization(n/candidate)
        candidate += 1

    return primes
Пример #9
0
def main():
    pandigital_length = 9
    while pandigital_length > 1:
        pandigitals = generate_pandigitals(pandigital_length)
        pandigitals.sort(reverse=True)
        for pandigital in pandigitals:
            if lib.is_prime(pandigital):
                print(pandigital)
                pandigital_length = 0
                break
        pandigital_length -= 1
    return 0
Пример #10
0
def is_prime_cached(n, primes):
    """
    Checks if a number is prime. Caches primes found in primes
    :param n: the number to check
    :param primes: list of known primes
    :return: True if prime, False otherwise
    """
    if n in primes:
        return True
    if lib.is_prime(n):
        primes.add(n)
        return True
    return False
Пример #11
0
def main():
    circular_primes = [2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97]
    for i in range(101, 1000000, 2):
        n = i
        digit_count = 0
        while n > 0:
            if n % 5 == 0 or n % 2 == 0:
                digit_count = -1
                break
            n //= 10
            digit_count += 1
        if digit_count == -1 or i in circular_primes or not is_prime(i):
            continue
        rotated_primes = [i]
        n = (i % 10 * 10 ** (digit_count - 1)) + (i // 10)
        while n != i:
            if not is_prime(n):
                break
            rotated_primes.append(n)
            n = (n % 10 * 10 ** (digit_count - 1)) + (n // 10)
        else:
            circular_primes += rotated_primes
    print(len(circular_primes))
fig, axs = plt.subplots(2, 2, figsize=(20.0/2.54, 20.0/2.54))
ax1, ax2, ax3, ax4 = axs.flat

print('Mean Error Real FFT (Non-Primes)')

i = 0
markers = ['>', '<', '+', 'x', '^']
styles = ['--', ':', '-.', '--', ':']
for k in sorted(results.keys()):
    v = results[k]
    kwargs = dict(label=k, marker=markers[i], ls=styles[i])
    n = np.array(v['sizes'][:], dtype=np.int)
    # the values which are a power of two
    f = (n != 0) & ((n & (n - 1)) == 0)
    # the primes
    fp = lib.is_prime(n)
    # the non-power of two, non-primes -> regular (or Hamming) numbers
    f2 = ~(f | fp)
    lc1, = ax1.plot(n[f], v['real_l2'][f], **kwargs)
    lc2, = ax2.plot(n[f2], v['real_l2'][f2], **kwargs)
    lc3, = ax3.plot(n[fp], v['real_linf'][fp], **kwargs)
    lc4, = ax4.plot(v['identity_sizes'], v['real_identity'], **kwargs)
    i += 1

    # ignore prime sizes as the error grows strongly with N
    print(k.ljust(20), np.mean(v['real_l2'][~fp]))

ax1.set_title('Power of Two')
ax2.set_title('Regular Numbers')
ax3.set_title('Primes')
ax4.set_title('Identity Error')
Пример #13
0
 def test2(self):
     self.assertTrue(lib.is_prime(3))
Пример #14
0
 def test11(self):
     self.assertFalse(lib.is_prime(6))
Пример #15
0
 def test13(self):
     self.assertFalse(lib.is_prime(9))
Пример #16
0
 def test7(self):
     self.assertTrue(lib.is_prime(17))
Пример #17
0
 def test9(self):
     self.assertTrue(lib.is_prime(23))
Пример #18
0
 def test7(self):
     self.assertTrue(lib.is_prime(17))
Пример #19
0
for consecutive values of n, starting with n = 0
"""

from lib import is_prime

def count_quandratic_primes(a, b):
    count = 0
    n = 0
    while n == count:
        count += int(is_prime(n ** 2 + a * n + b))
        n += 1
    return count


max_n = 0
max_i = 0
max_j = 0
for i in range(-999, 1000):
    if not is_prime(abs(i)):
        continue
    for j in reversed(range(-999, 1000)):
        if not is_prime(abs(j)):
            continue
        n_count = count_quandratic_primes(i, j)
        if max_n < n_count:
            max_n = n_count
            max_i = i
            max_j = j

print(max_i * max_j)
Пример #20
0
 def test9(self):
     self.assertTrue(lib.is_prime(23))
Пример #21
0
 def test8(self):
     self.assertTrue(lib.is_prime(19))
Пример #22
0
 def test15(self):
     self.assertFalse(lib.is_prime(100))
Пример #23
0
 def test13(self):
     self.assertFalse(lib.is_prime(9))
Пример #24
0
 def test12(self):
     self.assertFalse(lib.is_prime(8))
Пример #25
0
import lib

lib._prime_limit = 10000

for x in range(1001, 10000, 2):
    if not lib.is_prime(x):
        continue
    inc_limit = ((10000 - x) / 2)
    for inc in range(10, inc_limit, 2):
        seq = x + inc, x + inc + inc
        if lib.is_prime(seq[0]) and lib.is_prime(seq[1]):
            s1 = sorted(str(x))
            if s1 == sorted(str(seq[0])) and s1 == sorted(str(seq[1])):
                print "%d%d%d" % (x, x+inc, x+inc+inc)
Пример #26
0
"""
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
"""

from lib import is_prime

limit = 2000000
primesum = 0

for i in range(1, limit+1):
	
	if(is_prime(i)):
		primesum += i

print(primesum)
Пример #27
0
import lib

sum = 2
below = 2000000
for i in range(3, below, 2):
    if lib.is_prime(i):
        sum += i
print(sum)
Пример #28
0
 def test6(self):
     self.assertTrue(lib.is_prime(13))
Пример #29
0
 def test11(self):
     self.assertFalse(lib.is_prime(6))
Пример #30
0
 def test5(self):
     self.assertTrue(lib.is_prime(11))
Пример #31
0
 def test8(self):
     self.assertTrue(lib.is_prime(19))
Пример #32
0
 def test4(self):
     self.assertTrue(lib.is_prime(7))
Пример #33
0
 def test10(self):
     self.assertFalse(lib.is_prime(4))
Пример #34
0
import lib
lim = 1000000

primes = list(lib.get_primes(lim))

max_prime = (0, 0, 0)
for idx, start in enumerate(primes):
    total = start

    for num, p in enumerate(primes[idx+1:]):
        if total >= lim:
            break
        if lib.is_prime(total):
            max_prime = max(max_prime, (num + 1, total, start))
        
        total += p

print max_prime
Пример #35
0
 def test12(self):
     self.assertFalse(lib.is_prime(8))
Пример #36
0
Файл: 27.py Проект: Citrie/Euler
def consecutive_prime(a, b):
    n, length = 0, 0
    while (n * n + a * n + b) > 1 and is_prime(n * n + a * n + b):
        n += 1
        length += 1
    return length
Пример #37
0
 def test15(self):
     self.assertFalse(lib.is_prime(100))
Пример #38
0
 def test3(self):
     self.assertTrue(lib.is_prime(5))
Пример #39
0
def num_euler(a, b):
    for n in itertools.count():
        if not lib.is_prime(n*n + a*n + b):
            return n
Пример #40
0
 def test3(self):
     self.assertTrue(lib.is_prime(5))
Пример #41
0
 def test1(self):
     self.assertTrue(lib.is_prime(2))
Пример #42
0
 def test4(self):
     self.assertTrue(lib.is_prime(7))
Пример #43
0
def circular_filter(n):
    return all((lib.is_prime(x) for x in rotate_gen(n)))
Пример #44
0
 def test5(self):
     self.assertTrue(lib.is_prime(11))
Пример #45
0
"""
Largest prime factor
Problem 3
The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?
"""
from math import sqrt, ceil
from lib import is_prime, iterate_odd

number = 600851475143
limit = ceil(sqrt(number))
pf = []
for n in range(3, limit):
    if number % n == 0 and is_prime(n):
        pf.append(n)

print(max(pf))
Пример #46
0
 def test6(self):
     self.assertTrue(lib.is_prime(13))
Пример #47
0
 def test1(self):
     self.assertTrue(lib.is_prime(2))
Пример #48
0
 def test10(self):
     self.assertFalse(lib.is_prime(4))
Пример #49
0
"""
Summation of primes
Problem 10
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
"""
from lib import is_prime

p = [2]
p_sum = 2
max_n = 2000000

for n in range(3, max_n, 2):
    if is_prime(n):
        p.append(n)
        p_sum += n

print(p_sum)
Пример #50
0
 def test2(self):
     self.assertTrue(lib.is_prime(3))