Project Euler Problem 27 (C++和Python)

100 篇文章 3 订阅
87 篇文章 1 订阅

Problem 27 : Quadratic primes

Euler discovered the remarkable quadratic formula:

n2+n+41
It turns out that the formula will produce 40 primes for the consecutive integer values 0≤n≤39. However, when n=40, 402+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 n2−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:

n2+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.

C++ source code

#include <iostream>
#include <cmath>     // sqrtl()
#include <cassert>   // assert()

using namespace std;

class PE0027
{
private:
    int m_a;
    int m_b;
    int m_n;

    bool checkPrime(long long number);

public:
    bool checkConsecutivePrimes(int a, int b, int N);
    void producesMaxConsecutivePrimes();
};

bool PE0027::checkPrime(long long number)
{
    long long i;
    long double squareRoot=sqrtl((long double)number);

    if (number < 2 || number%2 == 0 && number!=2)
    {
        return false;
    }

    for(i=3;i<=(long long)squareRoot;i+=2)  // 3, 5, 7, ...
    {
        if (number % i == 0)
        {
            return false;
        }
    }

    return true;
}

bool PE0027::checkConsecutivePrimes(int a, int b, int N)
{
    long long number;

    for(int n=0; n<N; n++)
    {
        number = n*n + a*n + b;  // n^2 + n + 41

        if (false == checkPrime(number))
        {
            break;
        }
    }
    return true;
}

void PE0027::producesMaxConsecutivePrimes()
{
    long long number;
    int a, b, n;
    
    m_a = m_b = m_n = 0;

    for(a=-999; a<1000; a++)   // |a|<1000| 
    {
        for(b=-1000; b<=1000; b++) // |b|<=1000
        {
            for(n=0; ;n++) // n is numOfPrimes
            {
                number = n*n + a*n + b;  // e.g. n^2 + n + 41

                if (false == checkPrime(number))
                {
                    break;
                }
            }

            if (n > m_n) 
            {
                m_a = a; 
                m_b = b; 
                m_n = n;
            }
        }
    }

    if (m_a < 0)
    {
        cout << "The quadratic formula n^2 - " << -m_a;
    }
    else
    {
        cout << "The quadratic formula n^2 + " << m_a;
    }

    if (m_b < 0)
    {
        cout << "n - " << -m_b;
    }
    else
    {
        cout << "n + " << m_b;
    }

    cout << ", produces " << m_n << " consecutive primes." << endl;

    cout << "The product of the coefficients, a and b, is " << m_a*m_b << endl;
}

int main()
{
    PE0027 pe0027;

    assert(true == pe0027.checkConsecutivePrimes(1, 41, 40));

    pe0027.producesMaxConsecutivePrimes();

    return 0;
}

Python source code

import math
    
def checkPrime(x):
    if x < 2 or x%2 == 0 and x!=2:
        return False

    for i in range(3, int(math.sqrt(x))+1, 2):  # 3, 5, 7, ...
        if x % i == 0:
            return False

    return True

def checkConsecutivePrimes(a, b, N):
    for n in range(0, N):
        number = n*n + a*n + b  # n^2 + n + 41
        if False == checkPrime(number):
            break
    return True

def producesMaxConsecutivePrimes():
    m_a = m_b = m_n = 0

    for a in range(-999, 1000):       # |a|<1000| 
        for b in range(-1000, 1001):  # |b|<=1000
            n = 0  # n is numOfPrimes
            while True:
                number = n*n + a*n + b  # e.g. n^2 + n + 41
                if False == checkPrime(number):
                    break
                n += 1
            if n > m_n: 
                m_a = a; 
                m_b = b; 
                m_n = n;

    if m_a < 0:
        print("The quadratic formula n^2 -",-m_a,end=' ')
    else:
        print("The quadratic formula n^2 +",m_a,end=' ')

    if m_b < 0:
        print("n-",-m_b,end=' ')
    else:
        print("n+",m_b,end=' ')

    print(", produces",m_n,"consecutive primes.")
    print("The product of the coefficients, a and b, is",m_a*m_b)

def main():
    assert True == checkConsecutivePrimes(1, 41, 40)
    producesMaxConsecutivePrimes()

if  __name__ == '__main__':
    main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值