Sicily-1009 the prime number of the Mason

Source: Internet
Author: User
Tags cmath

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

    1. An exponential array of 2-64 that matches the number of Mason primes and is composite is given.
    2. 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

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.