A Mason Prime
There are infinitely many primes, but only a very small number of primes can be expressed in the form of 2p-1 (P is prime). In the prime number not greater than 257, when p=2, 3, 5, 7, 13, 17, 19, 31, 67, 127, 257, 2p-1 is prime, others are composite. The preceding 7 numbers (i.e., 2, 3, 5, 7, 13, 17, 19) have been confirmed by previous generations, and the subsequent 4 numbers (i.e. 31, 67, 127, 257) are the inferences of Mason's own. For more than 2,300 of years, only 48 Mason primes have been found in humans.
The index is composite at 11, 23, 29, 37, 41, 43, 47, 53, 59.
Two Process
- An exponential array of 2-64 that matches the number of Mason primes and is composite is given.
- A number to judge, using the method of judging primes, if it is composite, print out its prime multiplication factor.
Three Source (Timeout)
Timeout code:
1 //2 //main.cpp3 //sicily-10094 //5 //Created by Ashley on 14-10-10.6 //Copyright (c) 2014 Ashley. All rights reserved.7 //8 9#include <iostream>Ten#include <cmath> One using namespacestd; A intprimes[ -] = {2,3,5,7, One, -, -, +, at, in, to,Panax Notoginseng, A, +, -, -, -, A}; - intMainintargcConst Char*argv[]) - { the intK; -CIN >>K; - for(inti =0; Primes[i] <= k; i++) { - Long LongNumber = (Long Long) Pow (2.0, Primes[i])-1; + Long LongComposite =Number ; - BOOLFirsttime =true; + BOOLIscomposite =false; A for(Long Longj =2; J * J <= number; J + +) { at if(number% J = =0) { -Number = number/J; -Iscomposite =true; - if(firsttime) { -Firsttime =false; -cout << J <<" "; in}Else { -cout <<"* "<< J <<" "; to } + } - } the if(iscomposite) { *cout <<"* "<< number <<" "; $cout <<" = "<< Composite <<"= (2 ^"<< Primes[i] <<")-1"<<Endl;Panax Notoginseng } - } the return 0; +}
By code:
1 //2 //main.cpp3 //sicily-10094 //5 //Created by Ashley on 14-10-10.6 //Copyright (c) 2014 Ashley. All rights reserved.7 //8 9#include <iostream>Ten#include <cmath> One using namespacestd; A intprimes[ -] = {2,3,5,7, One, -, -, +, at, in, to,Panax Notoginseng, A, +, -, -, -}; - intMainintargcConst Char*argv[]) - { the intK; -CIN >>K; - for(inti =0; Primes[i] <= k; i++) { - Long LongNumber = (Long Long) Pow (2.0, Primes[i])-1; + Long LongComposite =Number ; - BOOLFirsttime =true; + BOOLIscomposite =false; A Long LongFactor =2; at for(Long LongJ = factor; J * J <= number; J + +) { - if(number% J = =0) { -Number = number/J; -Iscomposite =true; - if(firsttime) { -Firsttime =false; incout << J <<" "; -}Else { tocout <<"* "<< J <<" "; + } -}//else { the //factor++; * // } $ }Panax Notoginseng if(iscomposite) { -cout <<"* "<< number <<" "; thecout <<"= "<< Composite <<"= (2 ^"<< Primes[i] <<")-1"<<Endl; + } A } the return 0; +}
Four Add: The algorithm for generating prime numbers:
Five exponential function:POW Double pow (double base, double exponent);
Six Do not know why to remove 61, in advance calculate good? Is that interesting? The direct switch is not good?
Sicily-1009 the prime number of the Mason