C # Find Mason primes

Source: Internet
Author: User
Tags square root

C # find Mason primes

The number of Mason is from Mason.

The so-called Mason number, is a type such as 2p-1, where the exponent p is prime, often recorded as MP. If the mason number is prime, it is called a mason prime. For example, 2^2-1=3,2^3-1=7,2^5-1=31,2^7-1=127, index p is also prime, also expressed in M2,M3,M5,M7.

The factorization method can be used to prove that if the 2n-1 is a prime number, exponential n is also prime; Conversely, when N is prime, the 2n-1 (MP) is not necessarily prime. The first few of the smaller mason numbers are prime, but the greater the Mason number, the more difficult it is for Mason primes to appear.

On August 23, 2008, Smith, a computer expert at the University of California, Los Angeles, finally found more than 10 million Mason primes. It has a 12,978,189-digit number, and if the macro is printed continuously with a normal font size, it can be longer than 50 kilometers. The achievement was ranked in 29th place by the American Times Magazine as one of the 50 best inventions of the 2008.

In January 2013, a team led by Curtis Coux Cooper, a professor of mathematics at the University of Central Missouri, found the 48th prime number of Mason. [12] The discovery was rated one of the ten breakthroughs in natural science by the British New Scientist magazine.

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Using System.Diagnostics; Namespace ConAppPrimeNumber2 {//mersenne prime number//Mason prime number of the formula 2^p-1 (with 2 power of the square minus 1, where P is also a prime)//such as 2^2-1=3,2^3-1
        =7,2^5-1=31,2^7-1=127, exponent p is also prime number//also used M2,M3,M5,M7 to represent class program {static void Main (string[] args)


            {System.Diagnostics.Stopwatch s = new Stopwatch ();
            Primenumber.testpower ();

            System.Console.WriteLine ();
            S.reset ();
            S.start ();
            Primenumber.find5 ();
            S.stop (); System.Console.WriteLine ("Watch time:{0}m, {1}s, {2}ms", s.elapsed.minutes, S.elapsed.seconds, S.elapsedmilliseconds

            );

            System.Console.WriteLine ();
            S.reset ();
            S.start ();
            Primenumber.find6 ();
            S.stop (); System.Console.WriteLine ("Watch time:{0}m, {1}s, {2}ms", s.elapsed.minutes,S.elapsed.seconds, S.elapsedmilliseconds);   } public class Primenumber {static int n = 100000;
        Find the prime number within a (n=100)//static int n = 1000000;  static bool ispn = FALSE;   Locate the prime number and set it to true static int count = 0; Counter/Find the number of Mason, way one (from the prime number to find)//low performance, find the fifth bit M17 (time consuming 10 minutes)//After optimized code, find the sixth bit M19 (take 30 seconds)//m31-> 2ms//finally find the day nineth bit M61 (time consuming 2
            Min. 10 sec) public static void Find5 () {ispn = false; System.Console.Write (2 + "T");

            Prime number 2 or here the output//count = 1;

            Count = 0; int MAXMP = 63; Can not be set to 64, because the 0~63 is actually already 64 bits, long is the largest 64, 2^64 has been zero, like bit=0~255,2^8-1 long maxmpn = Poweroftwo (MAXMP)-

            1; int temp = 2;//starting with index 2, p=2 for (long i = 1; i < long. MaxValue; i + + 2) {for (Long j = 3; J < I; J + + 2)//Here the i=3 will not be executed, because j<i, do not meet the conditions, even if j<=i, then i%
          J will also think of 3=i as composite {          if (i% J = = 0) {ispn = false;
                    Break
                        } if (J * J > I)//if (j> math.sqrt (i)) {
                        ISPN = true;
                    Break     } if (i = = 2 | | i = = 3) Ispn = true;             2 will not be executed, because the front loop is (i=1;i+=2)//if (i = = 3) Ispn = true; Prime number 3 because to participate in the calculation of whether the Mason prime number, so put here if (ispn) {//system.console.write (i + "\
                    T ");

                    count++;
                        for (int p = 2; p <= maxmp p++)//for (int p = temp; p <= maxmp; p++) {   if (P!= 2 && p% 2 = 0) continue;   M62 execution, Dead loop, set to 61 o'clock look no problem to//if (P!= 3 && p% 3 = 0) continue; M63 same LOng MP = Poweroftwo (p)-1;
                            if (I < MP) {i = MP-2;
                        Break
                            } if (i = = MP) {count++;
                            System.Console.Write ("[No.{0} M{1}]", count, p);
                            System.Console.Write ("Mersenne prime Number:2^{0}-1={1}" + "\ n", p, i);             if (p = = MAXMP) return; If you have found the exit, the back of the I loop does not need to be executed (for example, set MAXMP to 32, compared to M32, then I=m32~long.                     MaxValue is not performed)//temp = p + 1;      The front is not compared, has been tested, performance does not improve//i = Poweroftwo (P + 1)-1-2;                           No minus 2 will miss M3 => 2^3-1=7//m61-> 2.23m i = MP;                                             
                           The front is not compared//m61-> 3.10m Break }} else {if (i > Maxmpn)
                return;//if the largest 2^maxmp-1 has been compared, exit the program.
        } System.Console.WriteLine ("Count: {0}", count); }//Find the prime number of Mason, mode two (from the Mason number to find the Mason Prime)//high performance, can find the eighth bit M31 (time consuming 15 seconds)//Nineth bit found, M61-> 23s//Mason number: Meet the conditions 2^p-1, but this number is not necessarily a prime, such as 2^4-
            1=15//Mason primes: for example 2^3-1=7 public static void Find6 () {ispn = false;

            Count = 0; Known Mason index mp:p=2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, ...

            Currently found the largest p=57,885,161, is the 48th digit, its length has 17,425,170 bits/s known Mason primes: 3, 7, 31, 127, 8191, 131071, 524287, 2147483647, ... Because long is 64 digits, it can only be computed to 64 bits for (int p = 2; p <=; p++) {Long MP = (Po
                Weroftwo (p)-1); Long mpsqrt = p% 2 = 0?
                Poweroftwo (P/2): Poweroftwo (p + 1)/2); Long mpsqrt = (long) MaTh. Sqrt (MP),//used. NET built-in object mathematical function evaluation (open square root) for (long j = 3; J < (MP); j = 2) {//system .
                        Console.title = J.tostring ();//Note index p less than 20 can be used, otherwise easy to panic if (MP% J = = 0) {
                        ISPN = false;
                        System.Console.Write ("is not Mersenne prime number:2^{0}-1={1}" + "\ n", p, MP);/Meet condition 2^p-1, but not Mason primes
                    Break
                        //if (J * J > (MP)) if (J > Mpsqrt) {
                        ISPN = true;
                    Break

                } if (p = = 2) ispn = true;//when exponent p is 2 o'clock, will get prime number 3, because to participate is the calculation of the Mason prime number, so add this condition here
                    if (ispn) {count++;

            System.Console.Write ("[No.{2} m{3}] Mersenne prime number:2^{0}-1={1}" + "\ n", p, MP, Count, p);//Mason Prime number} }
            System.Console.WriteLine ("Count: {0}", count);

            }//Lock the range of the Mason primes public static long poweroftwo (int p) {Long num = 2;
            if (p = = 0) return 1;

            if (p = = 1) return 2;
                    Switch (p) {Case 0:num = 1;
                Break
                    Case 1:num = 2;
                Break
                    Default:num = 2;
            Break
            for (int i = 1; i < P i++) {num *= 2;
        return num;
                public static void Testpower () {for (int i = 0; I <=; i++) {
            System.Console.Write ("2^{0}={1}" + "T", I, Poweroftwo (i));
 }
        }
    }
}

When writing this article, the number of Mason only found 48, today sorted out the code, only to know that the 49th is now found.

January 7, 2016, Cooper also found the 49th Mason Prime 274207281-1. This oversized prime has 22,338,618 digits and is the largest known prime number.

Copyright, reprint Please indicate the source of the article Http://blog/csdn.net/cadenzasolo

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.