Question : Mersenne Primes algorithm

Hi,

I need to implement an algorithm that calculates and displays as many Mersenne Primes as possible.  This has to be done in a console application.  Does anyone know how to do this?

Answer : Mersenne Primes algorithm

kaufmed is right, you won't get a lot of numbers without having your own data structure. This said, below is a solution with unsigned integers to go as far as p=31 which is Mp = 2147483647. Here is a list of those numbers in wikipedia:
http://en.wikipedia.org/wiki/Mersenne_prime

here is how to get the list of numbers from the code I gave you:
 for (uint i = 2; i < 31; i++)
            {

                if (IsMersennePrime(i))
                    Console.WriteLine("p:"+i + "\tMp:" + ((int)Math.Pow(2, i)-1));
            }
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
public static bool IsMersennePrime(uint n)
        {

            var x = Math.Pow(2, n) - 1;

            return IsPrime((uint)x);
        }

        public static bool IsPrime(uint candidate)
        {
            //
            // Test whether the parameter is a prime number.
            //
            if ((candidate & 1) == 0)
            {
                if (candidate == 2)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            int num = (int)Math.Sqrt((double)candidate);
            for (int i = 3; i <= num; i += 2)
            {
                if ((candidate % i) == 0)
                {
                    return false;
                }
            }
            return true;
        }
Random Solutions  
 
programming4us programming4us