Beispiel #1
0
    def sieve_less(self):
        for composite in count(35, 2):
            # ignore odd primes
            if simple_is_prime(composite):
                continue

            for i in count():
                delta = composite - 2 * i**2

                if delta < 2:
                    return composite

                if simple_is_prime(delta):
                    break
 def brute_force(self):
     for i in (7, 4):  # all other lengths of n are divisible by 3
         digits = reversed("123456789"[:i])
         for j in permutations(digits):
             j = int(''.join(j))
             if simple_is_prime(j):
                 return j
Beispiel #3
0
    def brute_force(self):
        upper_bound = 1_000_000
        primes = list(sieve_of_eratosthenes(4000))
        max_length = 0
        max_prime = 0

        for start in range(len(primes)):
            current_length = 0
            for end in range(start, len(primes)):
                s = sum(primes[start:end])
                current_length += 1

                if s > upper_bound:
                    break
                elif simple_is_prime(s) and current_length > max_length:
                    max_length = current_length
                    max_prime = s

        return max_prime
    def brute_force(self):
        """
        Loop through all a and b values such that abs(a|b) < 1000

        then count up from 0 (n) and check if the output of the quadratic
        n^2 + an + b for each n until the output is no longer a prime
        check if we beat the max and then rinse and repeat
        """
        best_a, best_b, best_n = 0, 0, 0

        for a in range(-1000, 1000):
            for b in range(-1000, 1000):
                for n in count():
                    output = (n ** 2) + (a * n) + b

                    if not simple_is_prime(output):
                        if best_n < n - 1:
                            best_a = a
                            best_b = b
                            best_n = n - 1

                        break
        return best_a * best_b