[Projecte1_] projecteuler_046

Source: Internet
Author: User
# Pragma once # include <windows. h> class moonmath {public: moonmath (void );~ Moonmath (void ); //************************************// method: isint // access: Public // describe: determines whether the double value is very close to an integer in the Epsilon range. // if the value of 1.00005 is greater than 0.00005, It is very close to an integer. // parameter: double doublevalue: the double value to be determined. // parameter: Double Epsilon: The accuracy of the determination. If 0 <Epsilon <0.5 // parameter: int32 & intvalue is close, returns the nearest integer // returns: bool returns true, otherwise, false is returned //*********************************** * Static bool isint (double doublevalue, double Epsilon, int32 & intvalue ); //************************************// method: sign // access: Public // describe: Get the value symbol // parameter: T value to get the value of the symbol // returns: positive, 0, and negative numbers of int32 return 1, 0, and-1, respectively //************************ * *********** template <typename T> static int32 sign (T value ); //************************************// method: isprimer // access: Public // describe: determines whether a number is a prime number. // parameter: uint32 num indicates the number to be judged. // returns: bool indicates a prime number and returns true, otherwise, false is returned //*********************************** * Static bool isprimer (uint32 num ); //************************************// method: isintegersquare // access: public static // describe: determines whether the given number is an integer after the square is opened. // 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 and 1 are not prime if (Num <= 1) {return false;} uint32 sqrtofnum = (uint32) SQRT (double) num ); // The 2nd power of num // from 2 to SQRT (Num). If no number can be divisible by num, num is the prime number; otherwise, it is not 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; // class detailprinter {public: void start (); void end (); detailprinter (); Private: large_integer timestart; large_integer timeend; large_integer freq ;}; detailprinter: detailprinter () {queryperformancefrequency (& freq );} //************************************// method: start // ACC ESS: Public // describe: called before each method is executed // returns: void // *********************************** void detailprinter:: Start () {queryperformancecounter (& timestart );} //************************************// method: end // access: Public // describe: Call each method after execution // returns: void // *********************************** void detailprinter:: end () {queryperformancecounter (& timeend); cout <"Total milliseconds is" <(Dou BLE) (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, extends tTime); cout <Endl <"by godmoon" <Endl <timestr <beep_char; System ("pause ");} ************ *********************///*************** * ****************** // Method: isfitconjecture/Access: Public // describe: determine whether num meets the conjecture // parameter: uint32 num // returns: bool // *********************************** bool isfitconjecture (uint32 num) {// if it is a prime number, it matches the conjecture that if (moonmath: isprimer (Num) {return true ;}for (uint32 I = 0; I <num; I ++) {// exclude non-prime if (! Moonmath: isprimer (I) {continue;} // determine whether (Num-I)/2 is an integer square if (moonmath: isintegersquare (Num-I) /2) {return true;} return false;} void testfun1 () {cout <"test OK! "<Endl;} void F1 () {cout <" Void F1 () "<Endl; // testfun1 (); detailprinter. start (); /********************************** algorithm 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; /*********************************** end of the algorithm **** * **************************/detailprinter. end () ;}// main function int main () {F1 (); Return 0 ;}/ * void F1 () the smallest odd composite that cannot be written as the sum of a prime and twice a square is: 5777 total milliseconds is 62.9729by godmoonsun Mar 10 22:26:17 2013 */

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.