Prime Numbers

Have you ever had one of those days when you wake up thinking about prime numbers?
Of course you have! Everyone has! Today, that was me. I rolled out of bed and stumbled over to my computer to find out about prime numbers.

For those of you unacquainted with theoretical mathematics, a prime number is a natural number that can only be divided by itself and 1. Here's more information about prime numbers if you are interested.

Anyways, its theorized that there are an infinite number of primes. When they start getting larger, you run into numbers known as Mersenne Primes. These are prime numbers that can be expressed in the form (2^n)-1. It was these prime numbers that grabbed my attention this morning. I wondered just what some of these numbers were, and the fact that there are lots of people trying to find larger and larger Mersenne Primes intruiged me. I thought to myself, "Why, if they can do it, so can I!"

So I set about writing a simple program in Java. It asks the user how many Mersenne Primes they would like to find and then spits out all the numbers of the form (2^n)-1. The problem is that in Java, there is a limit to how big a number value can be, and when dealing with java's basic exponent function (java.lang.Math.pow(x,y);) this peaks at the value limit for integers. Therfore the most Mersenne Primes you can find with my program is 30.

So here are the first 30 Mersenne Primes (1 is on the list but I don't count it because I know its not a prime number):

1
3
7
15
31
63
127
255
511
1023
2047
4095
8191
16383
32767
65535
131071
262143
524287
1048575
2097151
4194303
8388607
16777215
33554431
67108863
134217727
268435455
536870911
1073741823
2147483647

My code:
/* Prime Number Finder
* Created by Tyler Ash
* (c) August 12 2009
* --------------------
* Searches for prime numbers within a user defined amount of numbers starting at 1.
*/
import java.util.Scanner;
public class PrimeFinder
{
public static void main(String args [])
{
int a = 1;
double k;
int n;
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Mersenne Prime Number Finder. This program searches for Mersenne primes within a given amount of numbers, starting at 1.\nHow many numbers do you want to search?\n");
n = input.nextInt();
if (n<1)
{
System.out.println("Please enter a number greater than 1.\n");
n= input.nextInt();
}
while(a<=n)
{
k= java.lang.Math.pow(2,a)-1;
System.out.println((int)k);
a++;
}
System.out.println("\nSearch for the first " + n +" Mersenne Prime Numbers competed.");
}
}

Oh, and I got a haircut today.

2 comments:

Anonymous said...

My гelаtіves every timе say that I am kіlling my time herе аt ωeb, but I know I am getting experiеncе
ԁaily by rеading suсh ρleasant cοntent.


mу web ѕіte arjun kanuri

Anonymous said...

Hoωԁy, i геаd уour blοg from
timе tо time and i own a sіmilаr one and i was ϳuѕt
wonԁering іf уou get a lοt of spam
сomments? If so how dο yоu ѕtop it,
anу plugin or anything уou саn recommend?
Ӏ get ѕo much latelу it's driving me mad so any assistance is very much appreciated.

my homepage :: reputation management

Post a Comment