日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【ProjectEuler】ProjectEuler_046

發(fā)布時(shí)間:2023/12/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【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); };


#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) {// 0和1不是素?cái)?shù)if(num <= 1){return false;}UINT32 sqrtOfNum = (UINT32)sqrt((double)num); // num的2次方// 從2到sqrt(num),如果任何數(shù)都不能被num整除,num是素?cái)?shù),否則不是for(UINT32 i = 2; 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; }
// 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。