zoukankan      html  css  js  c++  java
  • [Project Euler] Problem 50

    Problem Description

    The prime 41, can be written as the sum of six consecutive primes:

    41 = 2 + 3 + 5 + 7 + 11 + 13

    This is the longest sum of consecutive primes that adds to a prime below one-hundred.

    The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

    Which prime, below one-million, can be written as the sum of the most consecutive primes?

    C++

    Here is the way:

    Firstly, we fill a integer array with primes which ranges from 1 to one million, we use the prime array to calculate later;

    Secondly, We set two pointers, one points to the first element of the array above, the other points to the position behind the last element, we assume that the longest consecutive primes are these from the start pointer to the end pointer. If not, we move the end pointer to its previous position, then check again.

    Thirdly, If we find a valid list, we record it, and add the start pointer by one and reset the end pointer which means move the end pointer to the (last element + 1) place. Repeat step 2.

    Codes:

    int* g_primeArray = NULL;
    int g_primeCount = 0;
    
    const int MAX_NUM = 1000000;
    
    void Initialize()
    {
    	g_primeCount = MAX_NUM / 5;
    	g_primeArray = new int[g_primeCount];
    	MakePrimes(g_primeArray, g_primeCount, MAX_NUM);
    
    }
    
    int CalculateSum(int* start, int* end, int& length, bool& isMax)
    {
    	isMax = false;
    	int* low = start;
    	int* high = end;
    	int sum = 0;
    	while(low < high)
    	{
    		sum += *low;
    		if(sum > MAX_NUM)
    		{
    			isMax = true;
    			sum -= *low;
    			break;
    		}
    		else
    		{
    			low++;
    		}
    	}
    	length = low - start; 
    	return sum;
    }
    
    void Problem_50()
    {
    	Initialize();
    	int maxLength = 1;
    	int maxPrime = 0;
    	int* start = g_primeArray;
    	int* end = g_primeArray + g_primeCount;
    
    	while(start < end)
    	{
    		int length = 0;
    		bool isMax = false;
    		int sum = CalculateSum(start, end, length, isMax);
    		if(length < maxLength)
    		{
    			if(isMax)
    				break;
    			start++;
    			end = g_primeArray + g_primeCount;
    			continue;
    		}
    		if(IsPrime(sum))
    		{
    			maxLength = length;
    			maxPrime = sum;
    			// printf("max length = %d, max prime = %d\n", maxLength, maxPrime);
    
    			start++;
    			end = g_primeArray + g_primeCount;
    		}
    		else
    		{
    			end = start + length - 1;
    		}
    	}
    	printf("max length = %d, max prime = %d\n", maxLength, maxPrime);
    }
    
  • 相关阅读:
    [转]vc中socket编程步骤
    [转载]使用命名管道实现进程间通信
    换肤软件摘要
    3D 专业词汇 (转)
    如何从 Microsoft DirectShow 筛选器图形获取数据(转)
    “人大艺术学院”“赵雅芝中文网”等网站被挂马 狼人:
    微软将发布5月安全漏洞补丁 修补PPT 狼人:
    专家提醒:网络挂马借“海运女”传播 狼人:
    黑客借“甲型流感”传毒 挂马疾病预防控制中心网站 狼人:
    黑客称攻破乔布斯亚马逊网站账户 欲售相关信息 狼人:
  • 原文地址:https://www.cnblogs.com/quark/p/2555946.html
Copyright © 2011-2022 走看看