Beispiel #1
0
 def solve(self):
     n = 1000
     for n in xrange(1000, 10000 - (2 * 3330)):
         if not is_prime(n):
             continue
         m = n + 3330
         if not self.is_permutation(n, m) or not is_prime(m):
             continue
         o = m + 3330
         if not self.is_permutation(n, o) or not is_prime(o):
             continue
         if n == 1487:
             continue
         return 100000000 * n + 10000 * m + o
Beispiel #2
0
def len_consecutive_squares(a, b):
    i = 0
    value = i**2 + i*a + b
    while value > 1 and is_prime(value):
        i += 1
        value = i**2 + i*a + b
    return i
Beispiel #3
0
def is_circular_prime(number):
    numberstring = str(number)
    for i in range(1, len(numberstring)):
        candidate = int(f'{numberstring[i:]}{numberstring[:i]}')
        if not is_prime(candidate):
            return False
    return True
Beispiel #4
0
    def execute(self):
        answer = None

        max_prime_count = 0

        for b in smaller_primes(1001):
            for a in range(-b+1, 1000):
                n = 0

                while is_prime(n**2 + a*n + b):
                    n += 1

                if max_prime_count < n:
                    max_prime_count = n
                    answer = a * b

        return answer
Beispiel #5
0
 def solve(self):
     n = 9
     primes = [2]
     goldbach = True
     while goldbach:
         n += 2
         while is_prime(n):
             n += 2
         while primes[-1] < n:
             primes.append(next_prime(primes[-1]))
         goldbach = False
         for p in primes:
             if p > n:
                 break
             r = (n - p) / 2
             sr = int(round(sqrt(r))) 
             if (sr * sr == r):
                 goldbach = True
                 break  
     return n
Beispiel #6
0
 def solve(self):
     pmax = 1000000
     primes = eratosthene(pmax)
     size = 0
     s = 0
     # Computing the max size of the sequence:
     # It is the largest number such that the first 
     # size prime numbers are less than pmax.
     while s < pmax:
         s += primes[size]
         size += 1
     while size > 1:
         for i in xrange(0, len(primes) - size + 1):
             p = sum(primes[i:i+size + 1])
             if p > pmax:
                 break
             if is_prime(p):
                 print "Found: sequence of size {0}".format(size)
                 return p
         size -= 1
Beispiel #7
0
 def solve(self):
     # Strating from 2
     n = 2
     # List of numbers from 0 to 9
     numerals = [str(i) for i in xrange(0, 10)]
     # Number to replace in the primes
     num_to_replace = "1"
     # Family of primes from n by replacing num_to_place by numerals
     family = list()
     
     while True:
         # Getting string from n
         str_n = str(n)
         # Family is empty at the beginning
         family = []
         # If there is no number to replace
         if str_n.count(num_to_replace) == 0:
                 # We compute the next prime and skip the rest of the loop
                 n = next_prime(n)
                 continue
         # else, for any possible numeral
         for numeral in numerals:
             # if we are replacing by 0, we check that we will not replace the first digit
             if (numeral == "0" and str_n[0] == num_to_replace):
                 continue
             # We compute the number obtained by replacing all num_to_replace in n by the current numeral
             m = int(str_n.replace(num_to_replace, numeral))
             # We check if m is prime
             if m >= 2 and is_prime(m):
                 # If it is, we add it to the family
                 family.append(m)
         # At the end, if the family contains at least 8 numbers
         if len(family) >= 8:
             # We return the first one
             return n
         # Else, we compute the next prime and perform another iteration
         n = next_prime(n)
Beispiel #8
0
#!/usr/bin/env python

"""
Problem #3, http://projecteuler.net/problem=3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143?

"""
import math
import toolbox

largest = 0
number = 600851475143
size = int(math.ceil(math.sqrt(number)))

for n in range(2, size):
    if toolbox.is_prime(n):
        if number % n == 0:
            largest = n

print("Largest prime factor of {number} is {largest}".format(**locals()))
Beispiel #9
0
 def nb_primes_formula(self, a,b):
     n = 0
     while (n**2+a*n+b > 1 and is_prime(n**2 + a*n + b)):
         n += 1
     return n
Beispiel #10
0
from toolbox import is_prime

def contains_sequence(numbers):
    numberset = set(numbers)
    for idx, first in enumerate(numbers):
        for second in numbers[idx+1:]:
            if 2 * second - first in numberset:
                return [first, second, 2 * second - first]
            elif 2 * second - first > numbers[-1]:
                break

seen = set()

for candidate in range(1000, 9999):
    # check if smallest is prime
    if not is_prime(candidate):
        continue
    smallest = ''.join(sorted(str(candidate)))
    if smallest in seen:
        continue
    else:
        found = set()
        for permutation in permutations(str(smallest)):
            number = int(''.join(permutation))
            if not(1000 <= number < 10000):
                continue
            if is_prime(number):
                found.add(number)
        if len(found) < 3:
            continue
        found = sorted(found)
Beispiel #11
0
def is_left_truncatable(number_string):
    for i in range(1, len(number_string)):
        if not is_prime(int(number_string[i:])):
            return False
    return True
Beispiel #12
0
 def commute(self, p, q):
     if not is_prime(int(str(p) + str(q))):
         return False
     if not is_prime(int(str(q) + str(p))):
         return False
     return True