# Pragma once # include <windows. h> # include <vector> # include <set> using namespace STD; 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 ); const static uint32 min_primer = 2; // minimum prime number //********************************** ** // 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 ); //************************************// method: getdiffprimerfactornum // access: public static // describe: gets all the different quality factors of num // parameter: uint32 num // returns: set <uint32> //********************************** ** static set <uint32> moonmath:: getdiffprimerfactornum (uint32 num ); //************************************// method: getdigitmap // access: Public // describe: Obtain the number map contained in num // parameter: uint32 num // parameter: uint16 & the number map of the num returned by digitmap, binary format // returns: If bool contains duplicate numbers, false is returned, otherwise, return true //*********************************** * Static bool getdigitmap (uint32 num, uint16 & digitmap ); //************************************// method: issamedigitnum // access: Public // describe: determines whether N numbers are composed of the same number. Each number must contain different numbers. // parameter: const uint32 Nums [] an array of N numbers // parameter: uint32 numcount number // returns: bool // ************************************ static bool isnumshavesamedigit (const uint32 Nums [], uint32 numcount ); //************************************// method: factorial // access: Public // describe: Get the factorial of N // parameter: uint32 N // returns: uint32 // ************************************ static uint32 factorial (uint32 N ); //************************************// method: combination // access: Public // describe: Sum of (n, R) // parameter: uint32 N // parameter: uint32 R // returns: uint32 // ************************************ static uint32 combination (uint32 N, uint32 R );};
# Include "moonmath. H" # include <cmath> # include <iostream> using namespace STD; 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;} If (num = min_primer) {return true;} // you can determine whether it can be divisible by 2 if (Num & 1) = 0) {return false;} uint32 sqrtofnum = (uint32) SQRT (double) num); // The 2nd power of num // from min_primer to SQRT (Num ), if no number can be divisible by num, num is a prime number, otherwise it is not for (uint32 I = min_primer + 1; I <= sqrtofnum; I + = 2) {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;} bool moonmath: getdigitmap (uint32 num, uint16 & digitmap) {uint32 digit = 0; digitmap = 0; while (num! = 0) {digit = num % 10; num/= 10; // if the number already exists, return false if (digitmap & (1 <digit) {return false ;} digitmap | = (1 <digit);} return true;} bool moonmath: isnumshavesamedigit (const uint32 Nums [], uint32 numcount) {uint16 lastdigitmap = 0; uint16 currdigitmap = 0; If (numcount <2) {return false;} If (! Moonmath: getdigitmap (Nums [0], lastdigitmap) {return false;} For (uint32 I = 1; I <numcount; ++ I) {If (! Moonmath: getdigitmap (Nums [I], currdigitmap) {return false;} If (currdigitmap! = Lastdigitmap) {return false;} return true;} uint32 moonmath: factorial (uint32 N) {If (n <= 1) {return 1;} uint32 result = 1; for (uint32 I = 2; I <= N; ++ I) {result * = I;} If (result = 0) {cout <n <Endl ;} return result;} uint32 moonmath: combination (uint32 N, uint32 R) {// C (n, R) = n! /R! /(N-R )! = (R + 1) * (R + 2 )*... * n/1/2 /... /(n-R) uint32 result = 1; for (uint32 I = R + 1; I <= N; ++ I) {result * = I ;} uint32 d = N-R; For (uint32 I = 2; I <= D; ++ I) {result/= I;} return result ;}
// Permuted multiples // problem 52 // it can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. /// find the smallest positive integer, X, such that 2x, 3x, 4x, 5x, and 6X, contain the same digits. /// Question 52: Find the smallest positive integer x, so that 2x, 3x, 4x, 5x, and 6X both contain the same number. // 125874 is twice the value of 251748. It contains the same number, but the order is different. /// Find the smallest positive integer x, so that 2x, 3x, 4x, 5x, and 6X both contain the same number. # 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; const char beep_char = '\ 007 '; cout <Endl <"by godmoon" <Endl <_ timestamp _ <beep_char <Endl; System ("pause ");} ************ *********************///**************** * ******************* // method: getmulnum // access: Public // describe: returns the multiple of num // parameter: uint32 num // parameter: Ui The storage location of nt32 mulnum [] is a multiple of mulnumcount + 1 num. // parameter: uint32 mulnumcount maximum multiple // returns: void // *********************************** void getmulnum (uint32 num, uint32 mulnum [], uint32 mulnumcount) {for (uint32 I = 1; I <= mulnumcount; ++ I) {mulnum [I-1] = num * I ;}} void testfun1 () {cout <"testfun1 OK! "<Endl;} void F1 () {cout <" Void F1 () "<Endl; // testfun1 (); detailprinter. start (); /********************************** algorithm start **** * *************************/const uint32 num_count = 6; uint32 mulnums [num_count]; uint32 num; For (num = 1; ++ num) {getmulnum (Num, mulnums, arraysize (mulnums); If (moonmath :: isnumshavesamedigit (mulnums, arraysize (mulnums) {break ;}} cout <"the smallest num is" <num <Endl; /*********************************** end of the algorithm **** * **************************/detailprinter. end () ;}// main function int main () {F1 (); Return 0 ;}/ * void F1 () the smallest num is 142857 total milliseconds is 32.823by godmoonthu Mar 28 22:46:09 2013 */