27 - quadratic primes

Euler discovered the remarkable quadratic formula:

n^2+n+41

It turns out that the formula will produce 40 primes for the consecutive integer values 0≤n≤39. However, when n=40,40^2+40+41=40(40+1)+41 is divisible by 41, and certainly when n=41,412+41+41 is clearly divisible by 41.

The incredible formula n^2−79n+1601 was discovered, which produces 80 primes for the consecutive values 0≤n≤79. The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form:

n^2+an+b, where |a|<1000 and |b|≤1000

where |n| is the modulus/absolute value of n

e.g. |11|=11 and |−4|=4

Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.

link

cevap : -59231

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "stdlib.h"

using namespace std;

bool  prime_check(int *number);
void find_max(unsigned int *length, unsigned int *max_val);
void clear_all(unsigned int *i, bool *prime_yes, unsigned int *n, unsigned int *j, unsigned int result_arr[]);

int main()
{
    unsigned int n = 0, i = 0, j = 0, length = 0, max_val = 0;
    unsigned int limit = 0, *result_arr, n_index = 0;
    int *length_arr;
    result_arr = (unsigned int *)calloc(1000, sizeof(unsigned int));
    length_arr = (int *)calloc(1000, sizeof(int));
    ofstream output;
    output.open("output.txt");
    bool prime_yes = true; 
    int result = 0;

    for (int a = -999; a < 999; a++)
    {
        for (int b = -1000; b < 1000; b++)
        {
            while (prime_yes)
            {
                result = n*n + a*n + b;  
                if (result > 2)
                {
                    prime_yes = prime_check(&result);
                    if (prime_yes)
                    {
                        result_arr[i] = result; 
                        i++;
                    }
                    else
                    {
                        if (n != 0)
                        {
                            length = i;
                            printf("a = %d, b = %d\n", a, b);
                            output << "a= " << a << "," << "b = " << b << endl;
                            printf("length = %d\n\n", length);
                            output << "length = " << length << endl;
                            length_arr[length] = a*b;
                            find_max(&length, &max_val);
                            break;
                        }
                    }
                }        
                n++;
            }
            clear_all(&i, &prime_yes, &n, &j, result_arr);
        }
    }
    cout << "result = " << length_arr[max_val] << endl;
    system("pause");
    return 0;
}

void clear_all(unsigned int *i, bool *prime_yes, unsigned int *n, unsigned int *j, unsigned int result_arr[])
{
    *i = 0;
    *prime_yes = true; // clear flag
    *n = 0;
    *j = 0;
    while (result_arr[*j])
    {
        result_arr[*j] = 0;
        (*j)++;
    }
}

void find_max(unsigned int *length, unsigned int *max_val)
{
        if (*length > *max_val)
        {
            *max_val = *length;
        }
}

bool prime_check(int *number) {

    unsigned int i, counter = 0;

    for (i = 2; i < *number; i++) {
        if (*number % i == 0)
            counter = counter + 1;
    }

    if (counter == 0) {
        return 1;
    }
    else {
        return 0;
    }
}