【ProjectEuler】ProjectEuler_046
生活随笔
收集整理的這篇文章主要介紹了
【ProjectEuler】ProjectEuler_046
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#pragma once#include <windows.h>class MoonMath
{
public:MoonMath(void);~MoonMath(void);//************************************// Method: IsInt// Access: public// Describe: 判斷double值在epsilon的范圍內(nèi)是否很接近整數(shù)// 如1.00005在epsilon為0.00005以上就很接近整數(shù)// Parameter: double doubleValue 要判斷的double值// Parameter: double epsilon 判斷的精度,0 < epsilon < 0.5// Parameter: INT32 & intValue 如果接近,返回最接近的整數(shù)值// Returns: bool 接近返回true,否則返回false//************************************static bool IsInt(double doubleValue, double epsilon, INT32 &intValue);//************************************// Method: Sign// Access: public// Describe: 獲取value的符號(hào)// Parameter: T value 要獲取符號(hào)的值// Returns: INT32 正數(shù)、0和負(fù)數(shù)分別返回1、0和-1//************************************template <typename T>static INT32 Sign(T value);//************************************// Method: IsPrimer// Access: public// Describe: 判斷一個(gè)數(shù)是否是素?cái)?shù)// Parameter: UINT32 num 要判斷的數(shù)// Returns: bool 是素?cái)?shù)返回true,否則返回false//************************************static bool IsPrimer(UINT32 num);//************************************// Method: IsIntegerSquare// Access: public static// Describe: 判斷給定的數(shù)開平方后是否為整數(shù)// Parameter: UINT32 num// Returns: bool//************************************static bool IsIntegerSquare(UINT32 num);
};
// Goldbach's other conjecture // Problem 46 // It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. // // 9 = 7 + 212 // 15 = 7 + 222 // 21 = 3 + 232 // 25 = 7 + 232 // 27 = 19 + 222 // 33 = 31 + 212 // // It turns out that the conjecture was false. // // What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?#include <iostream> #include <windows.h> #include <ctime> #include <assert.h>#include <MoonMath.h>using namespace std;// 打印時(shí)間等相關(guān)信息 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: 執(zhí)行每個(gè)方法前調(diào)用 // Returns: void //************************************ void DetailPrinter::Start() {QueryPerformanceCounter(&timeStart); }//************************************ // Method: End // Access: public // Describe: 執(zhí)行每個(gè)方法后調(diào)用 // Returns: void //************************************ void DetailPrinter::End() {QueryPerformanceCounter(&timeEnd);cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;time_t currentTime = time(NULL);const char BEEP_CHAR = '\007';const int MAX_TIME_CHAR = 30;char timeStr[MAX_TIME_CHAR];ctime_s(timeStr, MAX_TIME_CHAR, ¤tTime);cout << endl << "By GodMoon" << endl << timeStr << BEEP_CHAR;system("pause"); }/*************************解題開始*********************************///************************************ // Method: IsFitConjecture // Access: public // Describe: 判斷num是否符合猜想 // Parameter: UINT32 num // Returns: bool //************************************ bool IsFitConjecture(UINT32 num) {// 如果本身就是素?cái)?shù),符合猜想if(MoonMath::IsPrimer(num)){return true;}for(UINT32 i = 0; i < num; i++){// 排除非素?cái)?shù)if(!MoonMath::IsPrimer(i)){continue;}// 判斷(num-i)/2是否為整數(shù)的平方if(MoonMath::IsIntegerSquare((num - i) / 2)){return true;}}return false; }void TestFun1() {cout << "Test OK!" << endl; }void F1() {cout << "void F1()" << endl;// TestFun1();DetailPrinter detailPrinter;detailPrinter.Start();/*********************************算法開始*******************************/const UINT32 START_ODD_NUM = 3;UINT32 i = START_ODD_NUM;while(IsFitConjecture(i)){i += 2;}cout << "The smallest odd composite that cannot be written as the sum of a prime and twice a square is: " << endl<< i << endl;/*********************************算法結(jié)束*******************************/detailPrinter.End(); }//主函數(shù) int main() {F1();return 0; }/* void F1() The smallest odd composite that cannot be written as the sum of a prime and twic e a square is: 5777 Total Milliseconds is 62.9729By GodMoon Sun Mar 10 22:26:17 2013 */
// Goldbach's other conjecture // Problem 46 // It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. // // 9 = 7 + 212 // 15 = 7 + 222 // 21 = 3 + 232 // 25 = 7 + 232 // 27 = 19 + 222 // 33 = 31 + 212 // // It turns out that the conjecture was false. // // What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?#include <iostream> #include <windows.h> #include <ctime> #include <assert.h>#include <MoonMath.h>using namespace std;// 打印時(shí)間等相關(guān)信息 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: 執(zhí)行每個(gè)方法前調(diào)用 // Returns: void //************************************ void DetailPrinter::Start() {QueryPerformanceCounter(&timeStart); }//************************************ // Method: End // Access: public // Describe: 執(zhí)行每個(gè)方法后調(diào)用 // Returns: void //************************************ void DetailPrinter::End() {QueryPerformanceCounter(&timeEnd);cout << "Total Milliseconds is " << (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / freq.QuadPart << endl;time_t currentTime = time(NULL);const char BEEP_CHAR = '\007';const int MAX_TIME_CHAR = 30;char timeStr[MAX_TIME_CHAR];ctime_s(timeStr, MAX_TIME_CHAR, ¤tTime);cout << endl << "By GodMoon" << endl << timeStr << BEEP_CHAR;system("pause"); }/*************************解題開始*********************************///************************************ // Method: IsFitConjecture // Access: public // Describe: 判斷num是否符合猜想 // Parameter: UINT32 num // Returns: bool //************************************ bool IsFitConjecture(UINT32 num) {// 如果本身就是素?cái)?shù),符合猜想if(MoonMath::IsPrimer(num)){return true;}for(UINT32 i = 0; i < num; i++){// 排除非素?cái)?shù)if(!MoonMath::IsPrimer(i)){continue;}// 判斷(num-i)/2是否為整數(shù)的平方if(MoonMath::IsIntegerSquare((num - i) / 2)){return true;}}return false; }void TestFun1() {cout << "Test OK!" << endl; }void F1() {cout << "void F1()" << endl;// TestFun1();DetailPrinter detailPrinter;detailPrinter.Start();/*********************************算法開始*******************************/const UINT32 START_ODD_NUM = 3;UINT32 i = START_ODD_NUM;while(IsFitConjecture(i)){i += 2;}cout << "The smallest odd composite that cannot be written as the sum of a prime and twice a square is: " << endl<< i << endl;/*********************************算法結(jié)束*******************************/detailPrinter.End(); }//主函數(shù) int main() {F1();return 0; }/* void F1() The smallest odd composite that cannot be written as the sum of a prime and twic e a square is: 5777 Total Milliseconds is 62.9729By GodMoon Sun Mar 10 22:26:17 2013 */
總結(jié)
以上是生活随笔為你收集整理的【ProjectEuler】ProjectEuler_046的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目部署uwsgi +Nginx
- 下一篇: 简述直方图和柱形图的区别_什么是直方图?