# 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 );};
// Prime digit replacements // problem 51 // by replacing the 1st digit of * 3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime. /// by replacing the 3rd and 4th digits of 56 ** 3 with the same digit, this 5-digit number is the first example having seven Primes among the ten generated numbers, yielding the family: 56003,561 13, 56333,564 43, 56663,567 73, and 56993. consequently 56003, being the first member of this family, is the smallest prime with this property. /// find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family. /// Question 51: Find the minimum number of prime numbers that can be obtained by changing the same part. // Six of the 9 Numbers obtained by replacing the first digit of * 3 are prime numbers:, 53, 73, and 83. /// Replace the third and fourth digits of 56 ** 3 with the same number. The fifth digit is the first digit that can obtain seven prime numbers. The prime number obtained is: 56003,561 13, 56333,564 43, 56663,567 73, and 56993. Therefore, the minimum 56003 is the smallest prime number with this property. /// Find the minimum prime number. You can use the same number to replace a portion of the prime number (not necessarily adjacent) to obtain eight prime numbers. # 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 );} //************************************// me Thod: Start // access: Public // describe: Call before executing each method // 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 millisec Onds 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 ");} ************ * *******************/const uint32 radio = 10; // In hexadecimal notation // The minimum Prime Number of a record, the number that can be increased and the number of times that can be increased. // For example, 56003, the number that can be increased is 100,110, 10, and the number of times that can be increased is 9, 9, respectively, 9 struct fitinfo {uint32 Num; vector <uint32> incnums; vector <uint32> maxinctimes; void clear (); void addincnum (uint32 incnum, uint32 maxinctimes );}; //************************************// method: clear // access: Public // describe: clear information // returns: void // *********************************** void fitinfo:: clear () {num = 0; incnums. clear (); maxinctimes. clear ();} //************************************// method: addincnum // ACC ESS: Public // describe: // parameter: uint32 incnum // parameter: uint32 maxinctimes // returns: void // *********************************** void fitinfo:: addincnum (uint32 incnum, uint32 maxinctimes) {incnums. push_back (incnum); maxinctimes. push_back (maxinctimes );} //************************************// method: getfitinfo // access: Public // describe: Get the information of this number, replace the same continuous number // parameter: uint32 num // paramet Er: fitinfo & fitinfo // returns: void // *********************************** void getadjacentfitinfo (uint32 num, fitinfo & fitinfo, uint32 familyprimercount) {uint32 numbak = num; // back up num for loop uint32 currdigit; // the number currently processed uint32 lastdigit = num % radio; // The number processed in the previous step. It is initialized to the first digit uint32 currweight = 1; // the weight of the current bit, which is 100 ...... Uint32 currincnum = 0; // number of available numbers of the current num uint32 addnum; // used to incrementally calculate the number of continuously available increments uint32 incnum; // used to save the continuously available increments fitinfo. clear (); fitinfo. num = num; while (numbak! = 0) {currdigit = numbak % radio; If (currdigit <= radio-familyprimercount) {// if the previous number is the same as the current number, add the combination to fitinfo if (lastdigit = currdigit) {currincnum + = currweight; addnum = currweight; For (incnum = currweight; incnum <currincnum; incnum + = addnum) {addnum/= radio; fitinfo. addincnum (incnum, radio-currdigit-1) ;}} if the else is inconsistent, add only currweight currincnum = currweight;} fitinfo. addincnum (C Urrincnum, radio-currdigit-1);} else {currincnum = 0;} currweight * = 10; lastdigit = currdigit; numbak/= radio ;}} //************************************// method: getnummap // access: Public // describe: Get // parameter: uint32 num // parameter: uint32 digitincnum [] binary number map, the location of the index number in decimal format. // parameter: uint32 radio // returns: void // *********************************** void getnummap (uint32 Num, uint32 digitincmap [], uint32 radio) {uint32 currdigit; uint32 currweight = 1; while (num! = 0) {currdigit = num % radio; digitincmap [currdigit] | = currweight; currweight <= 1; num/= radio ;}} //************************************// method: getincweight // access: Public // describe: Obtain the weighted value according to the Binary Map. // parameter: uint32 digitincmap, for example, 1010b // parameter: vector <uint32> & incweight, for example: 1000,10 // returns: void // *********************************** void getincweight (uint32 digitincmap, vector <UI Nt32> & incweight) {uint32 currweight = 1; incweight. Clear (); While (digitincmap! = 0) {If (digitincmap & 1) {incweight. push_back (currweight);} currweight * = radio; digitincmap >>=1 ;}} //************************************// method: getvalue // access: Public // describe: Get the value from map and weight // parameter: uint32 map data bit, for example: 101 // parameter: vector <uint32> & incweight weight, for example: 1000,100, 1 // returns: uint32 value: for example: 1001 // *********************************** uint32 getvalue (uint32 map, const Vector <Uint32> & incweight) {uint32 value; uint32 index; For (value = 0, Index = 0; Map; ++ index) {If (MAP & 1) {value + = incweight [Index];} map >>=1;} return value ;} //************************************// method: parsedigitmap // access: Public // describe: Calculate the fitinfo/parameter: uint32 digitincmap of the corresponding number from digitincmap, which indicates the location of the digit, for example, 10101101 // parameter: uint32 digit range: [0, radio), indicating a number // parameter: Fit Info & fitinfo // returns: void // *********************************** void parsedigitmap (uint32 digitincmap, uint32 digit, fitinfo & fitinfo) {vector <uint32> incweight; getincweight (digitincmap, incweight); uint32 maxmapvalue = 1 <incweight. size (); For (uint32 mapvalue = 1; mapvalue <maxmapvalue; ++ mapvalue) {fitinfo. addincnum (getvalue (mapvalue, incweight), radio-digit-1 );}}//**************** * ****************** // Method: getfitinfo // access: Public // describe: Get the information of this number, replace the inconsistent numbers // parameter: uint32 num // parameter: fitinfo & fitinfo // returns: void // *********************************** void getnotadjacentfitinfo (uint32 num, fitinfo & fitinfo, uint32 familyprimercount) {uint32 digitincmap [radio] = {0}; fitinfo. clear (); fitinfo. num = num; getnummap (Num, digitincmap, radio); For (uint32 Digit = 0; digit <radio; ++ digit) {parsedigitmap (digitincmap [digit], digit, fitinfo );}} //************************************// method: calcmaxprimercount // access: Public // describe: gets the number of prime numbers in this family. // parameter: const fitinfo & fitinfo // returns: uint32 // *********************************** uint32 calcmaxprimercount (const fitinfo & fitinfo, uint32 & maxfitincnum, uint32 & maxfittimes) {uint32 maxprimer Count = 0; uint32 currprimercount = 1; uint32 fitcount = fitinfo. incnums. size (); uint32 currnum; uint32 fitindex; uint32 fitinctimes; uint32 fitinccount; uint32 fitincnum; For (fitindex = 0; fitindex <fitcount; ++ fitindex) {currprimercount = 1; // initialize to 1 because fitinfo. num is already a prime number. currnum = fitinfo. num; fitincnum = fitinfo. incnums [fitindex]; fitinctimes = fitinfo. maxinctimes [fitindex]; for (fitinccount = 0; fitinccount <fitinctimes; ++ fitinccount) {currnum + = fitincnum; If (moonmath: isprimer (currnum) {++ currprimercount; If (currprimercount> maxprimercount) {maxfitincnum = fitincnum; maxfittimes = fitinctimes; maxprimercount = currprimercount ;}}}return maxprimercount;} void testfun1 () {fitinfo; Ratio (100, fitinfo, 8 ); cout <"test OK! "<Endl;} void F1 () {cout <" Void F1 () "<Endl; // testfun1 (); detailprinter. start (); /********************************** algorithm start **** * *************************/const uint32 family_primer_count = 6; // The number of prime numbers in the prime family: fitinfo; uint32 maxfitincnum; uint32 maxfittimes; For (uint32 num = 3; num + = 2) {If (moonmath: isprimer (Num )) {getnotadjacentfitinfo (Num, fitinfo, family_primer_count); If (calcmaxprimercount (fitinfo, maxfitincnum, maxfittimes) >=family_primer_count) {break ;}}} cout <"has at least" <family_primer_count <"the smallest prime number in the prime family of prime numbers is" <fitinfo. num <Endl <"other members of the prime family:"; uint32 primer = fitinfo. num; For (uint32 I = 0; I <maxfittimes; ++ I) {primer + = maxfitincnum; If (moonmath: isprimer (primer )) {cout <primer <"" ;}} cout <Endl; /*********************************** end of the algorithm **** * **************************/detailprinter. end () ;}// main function int main () {F1 (); Return 0 ;}/ * void F1 () the smallest prime in a prime family with at least 6 prime numbers is 13. Other members are 23 43 53 73 83 total milliseconds is 4.50833by godmoonsun Mar 24 21:44:13 2013 void F1 () the smallest prime number in a prime family with at least 7 prime numbers is 56003. Other members: 56113 56333 56443 56663 56773 56993 total milliseconds is 481.138by godmoonsun Mar 24 21:43:49 2013 void F1 () the smallest prime number in a prime family with at least 8 prime numbers is 121313. Other members: 222323 323333 424343 525353 626363 828383 929393 total milliseconds is 1023.6by godmoonsun Mar 24 21:41:17 2013 */