Exemple #1
0
    def test_primes(self):
        prime_list = self._answers["primes"]
        computed = []
        for i in range(0, prime_list[-1] + 1):
            if libeuler.is_prime(i):
                computed.append(i)
        self.assertListEqual(computed, prime_list)

        for n in self._answers["primes_random"]:
            self.assertEqual(libeuler.is_prime(n), True)
Exemple #2
0
def is_circ_prime (n):
	if not is_prime(n):
		return False

	digits = digital_split(n)
	count = len(digits) - 1
	f = lambda x, y: ((x * 10) + y)

	for i in xrange(count):
		digits.append(digits.pop(0))
		m = reduce(f, digits)
		if not is_prime(m):
			return False

	return True
Exemple #3
0
def is_goldbach(n, primes, squares):
    if is_prime(n):
        return True
    for p in primes:
        for s in squares:
            if n == p + (2 * s):
                return True
    return False
def main():
    c = 0
    i = 0
    while c < PRIME_N:
        i += 1
        if libeuler.is_prime(i):
            c += 1

    print(i)
Exemple #5
0
#!/usr/bin/env python

from libeuler import is_prime

N = 10001

num = 1
count = 0
while count < N:
	num += 1
	if is_prime(num):
		count += 1

print num

Exemple #6
0
#!/usr/bin/env python

from libeuler import is_prime

max_n = 0
max_a = 0
max_b = 0
for a in xrange(-1000, 1000):
	for b in xrange(-1000, 1000):
		f = lambda n : n**2 + a*n + b

		n = 0
		while True:
			if is_prime(f(n)):
				n += 1
			else:
				break

		if n > max_n:
			max_n = n
			max_a = a
			max_b = b

print max_a * max_b

Exemple #7
0
#!/usr/bin/env python

from libeuler import is_prime
from itertools import permutations

f = lambda x, y: ((x * 10) + y)

x = [9, 8, 7, 6, 5, 4, 3, 2, 1]
y = permutations(x)
print x

while True:
	try:
		n = reduce(f, y.next())
		if is_prime(n):
			break
	except StopIteration:
		x.pop(0)
		y = permutations(x)
		print x

		if len(x) == 0:
			print "Search failed!"
			n = 0
			break

print
print n

Exemple #8
0
#!/usr/bin/env python

from libeuler import is_prime

x = 600851475143.0

end = int(x ** 0.5) + 1
for i in xrange(end, 1, -1):
    if x % i == 0:
        print "Factor: %d" % i
        if is_prime(i):
            print i
            break
Exemple #9
0
	b = digital_split(y)
	c = digital_split(z)
	a.sort()
	b.sort()
	c.sort()
	A = reduce(f, a)
	B = reduce(f, b)
	C = reduce(f, c)
	return (A == B == C)

L = xrange(M, N)
primes = filter(is_prime, L)

seq = None
step = 3330
for p1 in primes:
	p2 = p1 + step
	p3 = p2 + step
	if (p3 > N):
		break
	if are_digital_permutations(p1, p2, p3) \
		and is_prime(p2) \
		and is_prime(p3) \
		and (p1 != 1487):
			seq = (p1, p2, p3)
			break

f = lambda x, y: ((x * 10000) + y)
print reduce(f, seq)

Exemple #10
0
        return True
    for p in primes:
        for s in squares:
            if n == p + (2 * s):
                return True
    return False


primes = [2, 3]
squares = [1, 4]
sq_cnt = 2
next_sq = 9

n = 5
while True:
    if is_prime(n - 1):
        primes += [n - 1]
    elif is_prime(n):
        primes += [n]

    if n - 1 == next_sq:
        squares += [n - 1]
        sq_cnt += 1
        next_sq = (sq_cnt + 1) ** 2
    elif n == next_sq:
        squares += [n]
        sq_cnt += 1
        next_sq = (sq_cnt + 1) ** 2

    flag = is_goldbach(n, primes, squares)
    if not flag: