#pragma once#include <windows.h>
#include <vector>
#include <set>using namespace std;class MoonMath
{
public:MoonMath(void);~MoonMath(void);//************************************// Method:    IsInt// Access:    public// Describe:  判断double值在epsilon的范围内是否很接近整数//            如1.00005在epsilon为0.00005以上就很接近整数// Parameter: double doubleValue    要判断的double值// Parameter: double epsilon        判断的精度,0 < epsilon < 0.5// Parameter: INT32 & intValue      如果接近,返回最接近的整数值// Returns:   bool                  接近返回true,否则返回false//************************************static bool IsInt(double doubleValue, double epsilon, INT32 &intValue);//************************************// Method:    Sign// Access:    public// Describe:  获取value的符号// Parameter: T value   要获取符号的值// Returns:   INT32     正数、0和负数分别返回1、0和-1//************************************template <typename T>static INT32 Sign(T value);const static UINT32 MIN_PRIMER = 2;     // 最小的素数//************************************// Method:    IsPrimer// Access:    public// Describe:  判断一个数是否是素数// Parameter: UINT32 num    要判断的数// Returns:   bool          是素数返回true,否则返回false//************************************static bool IsPrimer(UINT32 num);//************************************// Method:    IsIntegerSquare// Access:    public static// Describe:  判断给定的数开平方后是否为整数// Parameter: UINT32 num// Returns:   bool//************************************static bool IsIntegerSquare(UINT32 num);//************************************// Method:    GetDiffPrimerFactorNum// Access:    public static// Describe:  获取num所有的不同质因数// Parameter: UINT32 num// Returns:   set<UINT32>//************************************static set<UINT32> MoonMath::GetDiffPrimerFactorNum(UINT32 num);
};
#include "MoonMath.h"
#include <cmath>MoonMath::MoonMath(void)
{
}MoonMath::~MoonMath(void)
{
}template <typename T>
INT32 MoonMath::Sign(T value)
{if(value > 0){return 1;}else if(value == 0){return 0;}else{return -1;}
}bool MoonMath::IsInt(double doubleValue, double epsilon, INT32 &intValue)
{if(epsilon > 0.5 || epsilon < 0){return false;}if(INT32(doubleValue + epsilon) == INT32(doubleValue - epsilon)){return false;}INT32 value = INT32(doubleValue);intValue = (fabs(doubleValue - value) > 0.5) ? (value + MoonMath::Sign(doubleValue)) : (value) ;return true;
}bool MoonMath::IsPrimer(UINT32 num)
{if(num < MIN_PRIMER){return false;}UINT32 sqrtOfNum = (UINT32)sqrt((double)num); // num的2次方// 从MIN_PRIMER到sqrt(num),如果任何数都不能被num整除,num是素数,否则不是for(UINT32 i = MIN_PRIMER; i <= sqrtOfNum; ++i){if(num % i == 0){return false;}}return true;
}bool MoonMath::IsIntegerSquare(UINT32 num)
{UINT32 qurtNum = (UINT32)sqrt((double)num);return (qurtNum * qurtNum) == num;
}set<UINT32> MoonMath::GetDiffPrimerFactorNum(UINT32 num)
{UINT32 halfNum = num / 2;set<UINT32> factors;for(UINT32 i = 2; i <= halfNum; ++i){if(!MoonMath::IsPrimer(i)){continue;}if(num % i == 0){factors.insert(i);while(num % i == 0){num /= i;}}}return factors;
}
// Consecutive prime sum
//     Problem 50
//     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?#include <iostream>
#include <windows.h>
#include <ctime>
#include <assert.h>#include <vector>
#include <MoonMath.h>using namespace std;// 打印时间等相关信息
class DetailPrinter
{
public:void Start();void End();DetailPrinter();private:LARGE_INTEGER timeStart;LARGE_INTEGER timeEnd;LARGE_INTEGER freq;
};DetailPrinter::DetailPrinter()
{QueryPerformanceFrequency(&freq);
}//************************************
// Method:    Start
// Access:    public
// Describe:  执行每个方法前调用
// Returns:   void
//************************************
void DetailPrinter::Start()
{QueryPerformanceCounter(&timeStart);
}//************************************
// Method:    End
// Access:    public
// Describe:  执行每个方法后调用
// Returns:   void
//************************************
void DetailPrinter::End()
{QueryPerformanceCounter(&timeEnd);cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;const char BEEP_CHAR = '\007';cout << endl << "By GodMoon" << endl << __TIMESTAMP__ << BEEP_CHAR << endl;system("pause");
}/*************************解题开始*********************************///************************************
// Method:    MakePrimerVector
// Access:    public
// Describe:  获取[0,maxNum]的所有素数,存入primers
// Parameter: UINT32 maxNum             要找的最大值
// Parameter: vector<UINT32> & primers  输出素数数组,素数由小到大排列
// Returns:   void
//************************************
void MakePrimerVector(UINT32 maxNum, vector<UINT32> &primers)
{for(UINT32 num = MoonMath::MIN_PRIMER; num <= maxNum; ++num){if(MoonMath::IsPrimer(num)){primers.push_back(num);}}
}//************************************
// Method:    GetSum
// Access:    public
// Describe:  计算迭代器[itBegin,itEnd)的和
// Parameter: const vector<UINT32>::const_iterator & itBegin
// Parameter: const vector<UINT32>::const_iterator & itEnd
// Returns:   UINT32
//************************************
UINT64 GetSum(const vector<UINT32>::const_iterator &itBegin,const vector<UINT32>::const_iterator &itEnd)
{UINT64 sum = 0;for(vector<UINT32>::const_iterator it = itBegin; it != itEnd; ++it){sum += *it;}return sum;
}void TestFun1()
{cout << "Test OK!" << endl;
}void F1()
{cout << "void F1()" << endl;// TestFun1();DetailPrinter detailPrinter;detailPrinter.Start();/*********************************算法开始*******************************/const UINT32 MAX_NUM = 1000000;vector<UINT32> primers;MakePrimerVector(MAX_NUM - 1, primers);UINT32 count = primers.size();UINT64 currSum = 0;bool notFound = true;vector<UINT32>::const_iterator itBegin; // 数列的第一个数vector<UINT32>::const_iterator itEnd;   // 数列的最后一个数的下一个元素!itBegin = primers.begin();// numCount为数列中的数字个数for(UINT32 numCount = count; numCount > 0 && notFound; --numCount){itEnd = itBegin + numCount;// 获取第一组数列,索引范围[0,numCount-1]currSum = GetSum(itBegin, itEnd);if(currSum < MAX_NUM && MoonMath::IsPrimer(currSum)){break;}UINT32 remainSetCount = count - numCount;   // 剩余的数列个数for(UINT32 setIndex = 0; setIndex < remainSetCount; ++setIndex){// 计算下一组数列和,减去前一个数,加上后一个数currSum = currSum - primers[setIndex] + primers[setIndex + numCount];if(currSum < MAX_NUM && MoonMath::IsPrimer(currSum)){itBegin += setIndex + 1;itEnd += setIndex + 1;notFound = false;break;}}}cout << currSum << " is sum of " << *itBegin << " to " << *(itEnd - 1) << endl;cout<<"sum is "<<GetSum(itBegin,itEnd)<<endl;/*********************************算法结束*******************************/detailPrinter.End();
}//主函数
int main()
{F1();return 0;
}/*
void F1()
997651 is sum of 7 to 3931
sum is 997651
Total Milliseconds is 1.60107e+006
*/

【ProjectEuler】ProjectEuler_050(找到100万以内最多连续素数的和,它也同时是个素数)相关推荐

  1. C++的速度比Java快2.1%:来自计算100万以内质数的实验数据对比

    为了验证C++到底比Java快多少分别用两种语言计算100万以内的质数,并记录时间 C++的程序是 clock_t start,ends; start=clock(); int i, j; for(i ...

  2. easyexcel导出excel,大数据量100万以内分页查询zip格式导出

    easyexcel导出excel,大数据量100万以内分页查询zip格式导出 准备工作 整体思路 controller层 service层 mapper层 VO 表结构 测试 备注 easyExcel ...

  3. C语言错误:expected declaration or statement at end of input、编写函数求100万以内的素数

    C语言错误:expected declaration or statement at end of input (中文:输入结尾应为声明或语句) 可能错误: 某一个函数或者变量没有在使用之前声明. 某 ...

  4. 求100以内的素数c语言_100万以内的四胞胎素数166组

    四胞胎素数定义为[p p+2 p+6 p+8],p为第一个素数. 100万以内的四胞胎素数有166组. 我们选两个典型案例 109841||109843||109847||109849 canlic ...

  5. 在10万以内判断,一个整数加上100后是一个完全平方数,再加上168又是一个完全平方数,求该数...

    题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后 的结果 ...

  6. 在10万以内的一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少?

    在10万以内的一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少? 完全平方数:如果一个数的平方根的平方等于该数,这说明此数是完全平方数 例如:√4 == ...

  7. 互联网日报 | 5月20日 星期四 | 京东一季度营收2032亿元;比亚迪第100万辆新能源车下线;唯品会连续34个季度盈利...

    ‍ ‍今日看点 ✦ 京东2021年Q1营收2032亿元,活跃购买用户数近5亿 ✦ 唯品会2021年Q1营收284亿元,实现连续34个季度盈利 ✦ 跻身百万俱乐部,比亚迪第100万辆新能源车正式下线 ✦ ...

  8. 有一个100万的数组,里边有两个是重复的,如何设计算法找到

    输出:两个重复的元素的索引 首先,直接两重循环查找不是不行,估计是最蠢的做法了. 其次,先用快速排序拍一遍,然后遍历,时间复杂度最好的情况是最坏的情况是nlogn+n 有人说是用hash,有人说用位图 ...

  9. 苹果悬赏100万美元找漏洞 辞职的理由找到了!

    12月21日,据外媒报道,苹果已经向所有安全研究人员开放其漏洞("bug")悬赏计划,为发现它的操作系统中的主要缺陷提供100万美元或更多奖励. 据悉,苹果该计划自2016年推出以 ...

最新文章

  1. 深入掌握JMS--转
  2. QToolButton设置背景无效的思考
  3. python哪个方向工资高_【看完这五大Python就业方向,你选择哪个?】- 环球网校
  4. JBOSS内存溢出处理
  5. 业内指路共享软件:更多机会在移动和海外市场(三)
  6. 信息学奥赛一本通(1110:查找特定的值)
  7. java 财付通支付_工商变更:马化腾卸任财付通支付科技有限公司法定代表人
  8. Flex(flash)检测摄像头的3种状态(是否被占用,没安装摄像头,正常)
  9. Win11如何添加过时硬件?Win11添加过时硬件的方法
  10. 测者的测试技术手册:Junit单元测试遇见的一个枚举类型的坑(枚举类型详解)...
  11. 腾讯2014实习北京笔试
  12. 考研数据结构程序题常见代码【C语言实现】
  13. 【Delphi】如何在三轴加速器的频谱分析中使用FFT(快速傅里叶变换)算法
  14. JavaScript的toast
  15. 另一个伊甸专武,国际服用(手机随便做的,有点粗糙,见谅
  16. Linux修改fstab引起系统无法启动问题的解决方法
  17. 单月涨粉超600w,直播销售额破5亿,2月的黑马都是谁?
  18. 三子棋 Tic-Tac-Toe
  19. linux 软件包kbd 位置,linux系统安装包的管理
  20. NOJ 1116 哈罗哈的大披萨 【淡蓝】 [状压dp+各种优化]

热门文章

  1. Web前端三大主流框架是什么?Web前端前景与就业形势分析
  2. trick:CSS 3+checkbox实现JQuery的6个基本动画效果
  3. 备案域名绑定服务器后 提示需要备案_最近有人问我:买了个域名,怎么用不了?域名究竟该怎么用?...
  4. “零信任”防御云计算信任危机
  5. 谷歌最新AI产品——AutoML Vision,可以自动设计机器学习模型
  6. [资源]C++书籍之 Essential C++(中文版)
  7. 对于游戏论坛的需求分析
  8. shell uefi 保存_UEFI Shell
  9. 机器学习实战 - 基于概率论的分类方法:朴素贝叶斯
  10. html 获取radio状态,html关于点击radio触发事件