Project Euler #27: Quadratic primes

Sort by

recency

|

38 Discussions

|

  • + 0 comments
    from sys import stdin
    
    read = stdin.readline
    
    N = int(2e3)
    
    sieve = [True] * (N + 1)
    
    sieve[0] = sieve[1] = False
    
    for p in range(2, int(N ** .5) + 1):
        if sieve[p]:
            for n in range(p * p, N + 1, p):
                sieve[n] = False
                
    n = int(read())
                
    res, res_a, res_b = 0, 0, 0
                
    def find(a, b):
        count = 0
        for n0 in range(n):
            maybe = n0**2 + n0*a + b
            if maybe > N or maybe < 2 or not sieve[maybe]:
                break
            count += 1
        return count
    
    for b in range(2, n + 1):
        if not sieve[b]:
            continue
        for a in range(-n, n + 1):
            x = find(a, b)
            if x > res:
                res = x
                res_a, res_b = a, b
            
    print(res_a, res_b)
    
  • + 0 comments
    import itertools
    
    def is_prime(x):
        if x % 2 == 0:
            return False
        for i in range(3, int(x**0.5) + 1, 2):
            if x % i == 0:
                return False
        return True
    
    def find_quadratic_primes(n):
        max_consecutive = 0
        best_solution = None
    
        for a, b in itertools.product(range(-n, n + 1), range(n + 1)):
            consecutive_primes = 0
            x = 0
    
            while True:
                abc = x**2 + a * x + b
    
                if abc < 0:  # As our required number must be at least positive
                    break
    
                if not is_prime(abc):
                    break
    
                x += 1
                consecutive_primes += 1
    
            if consecutive_primes > max_consecutive:
                best_solution = (a, b)
                max_consecutive = consecutive_primes
    
        return best_solution
    
    if __name__ == "__main__":
        n = int(input())
        solution = find_quadratic_primes(n)
        print(*solution)
    
  • + 0 comments

    a little messy but solves all the testcases
    100/-points python3

    import itertools
    def isprime(x):
        if x%2==0:
            return False
        for i in range(3,int(x**0.5)+1,2):
            if x%i==0:
                return False
        return True
    
    n=int(input())
    max_consec=0
    solutions=[]
    for i in itertools.product(list(range(-n,n+1,1)),range(n+1)): #I didn't set the range of b through the negatives because for x=0 the exspression will become b which must be positive so only +values 
        primes=0
        x=0
        while True:
            abc=x**2+i[0]*x+i[1]
            if abc<0: #as our required number must be atleast positive
                break
            if not isprime(abc):
                break
            x+=1
            primes+=1
        if primes>max_consec:
            solutions.append(i)
            max_consec=primes
    print(*solutions[-1])
    
  • + 0 comments

    Brute forcing in PHP. Parameter a cannot be even because if it is, then n(n+a) will cycle through odd and even numbers so we won't get more than one consequitive prime. If a is odd, then n(n+a) will always be even which means b cannot be even either. So these two facts can speed up the search somewhat.

    <?php
    
    $MAX_N = 2000;
    $sieve = array_fill(0, $MAX_N, 1);
    
    function build_primes() {
        global $sieve, $MAX_N;
        
        // Use the Eratosphene's sieve
        for ($i = 2; $i < $MAX_N; $i++)
            if ($sieve[$i] != 0)
                for ($j = $i*$i; $j < $MAX_N; $j = $j+$i)
                    $sieve[$j] = 0;
    }
    
    function max_primes(int $a, int $b) {
        global $sieve, $MAX_N;
        
        $count = $n = 0;
        while (true) {
            $r = $n*$n + $a*$n + $b;        
            if ($r < 0 || $r > $MAX_N || $sieve[$r] == 0) break;
            $count++;
            $n++;
        }
        
        return $count;
    }
    
    function find_ab(int $n) {
        $maxc = $maxa = $maxb = -1;
        for ($a = -$n; $a <= $n; $a++)
            for ($b = -$n; $b <= $n; $b++)
                if ($a % 2 != 0 && $b % 2 != 0) {
                    $c = max_primes($a, $b);
                    if ($c > 0 && $maxc < $c) {
                        $maxc = $c;
                        $maxa = $a;
                        $maxb = $b;
                    }
                }
        return sprintf("%d %d", $maxa, $maxb);
    }
    
    $f = fopen("php://stdin", "r");
    $line = fgets($f);
    build_primes();
    echo find_ab((int)$line);
    
    ?>
    
  • + 0 comments

    As for "n=0" the value of n^2+an+b must be prime, b will be prime, so it is enough to iterate over every prime b between [-n,n], obtained by the sieve of Eratosthenes.