Prime Numbers

J

j1mb0jay

I am now trying to improve upon the way in which i calculate if a number
is prime. I understand their a several probabilist prime checks out
their, but i would like one that give a yes or no rather than a very
mathematical maybe.

Firstly i would like to point out that i am going to be working with
numbers smaller than (2^64)-1 (AKA LONG) I have attached a rather large
amount of code to this post, the code is my implementation of the Sieve
of Atkin method.

The first thing i would like to know... is there are fast prime checker.
The second thing i would like to know is how could i improve upon the
execution time of the method.



-------------------A lot of Code------------------------------------------


package math;

public class Prime
{
/**
* Finds the next prime number greater than the "testSubject"
*
* @param testSubject - the number you want to find the next
prime for.
* @return - the next prime after the test subject
*/
public static long nextPrime(long testSubject)
{
//long startTime = System.currentTimeMillis();
while(!isPrime(testSubject))
{
testSubject++;
}
//System.out.println("\n" + ((double)((double)
System.currentTimeMillis() - (double)startTime) / 1000.0) + " seconds");
return testSubject;
}

/**
* Test to see if a number is a prime number. The method uses the
Sieve of Atkin
* mathmatical formula to test if a number is prime. This is one
of the quickest methods
* of our time.
*
*
* @param testSubject - the number you want to test is prime or
not.
* @return - true if "testSubject" is a prime number.
*/
public static boolean isPrime(long testSubject)
{
if(isLowPrime(testSubject))
{
return true;
}
else
{
if(fastFail(testSubject))
return false;

long mod = testSubject % 60;
if(mod == 1 || mod == 13 || mod == 17 || mod ==
29 || mod == 37 || mod == 41 || mod == 49 || mod == 53)
{
return firstStep(testSubject, 4);
}
else if(mod == 7 || mod == 19 || mod == 31 ||
mod == 43)
{
return firstStep(testSubject, 3);
}
else if(mod == 11 || mod == 23 || mod == 47 ||
mod == 59)
{
return secondStep(testSubject);
}
else
{
return false;
}
}
}

/**
* This method is called for the first and second case of the
Sieve of Atkin method.
* Thus meaning the answer to testSubject % 60 was equal to
* 1,13,17,29,37,41,49,53 for the first case or 7,19,31,43 for
the second case.
*
* This only means the number has a chance of being prime, to
find out for sure
* this method will get the sum of solutions to the follow
equation
*
* 4x^2 + y^2 = "testSubject" for the first case or 3x^2 + y^2 =
"testSubject".
*
* If the sum of the soluations is not perfectly divisable by 2
and the "testSubject"
* is a SquareFree number then the "testSubject" is prime.
*
* @param testSubject - the number that is been tested to see if
it prime.
* @param offSet - because the first and second case are so
simaler this number is either a 3 or 4.
* Depending on if the first or second case is been used.
* @return - true if "testSubject" is a prime number.
*/
private static boolean firstStep(long testSubject, int offSet)
{
//Calcualte the end point of the loop.
long endYPoint = (long)Math.sqrt(testSubject);
long endXPoint = (long)Math.sqrt((testSubject / offSet));

//Variable to store the sum of solutions.
long rightAnswers = 0;
//Loops until it has found all of the solutions.
for(int x = 1; x <= endXPoint; x++)
{
for(int y = 1; y <= endYPoint; y++)
{
if(((x*x) * offSet + (y*y)) ==
testSubject)
{
rightAnswers++;
}
}
}
//If the sum of solutions goes into two perfectly then
testSubject is not a prime.
if(rightAnswers % 2 == 0)
{
return false;
}
else
{
//If the testSubejct is SquareFree then it is a
prime number.
if(SquareFree.isSquareFree(testSubject))
{
return true;
}
else
{
return false;
}
}
}


/**
* This method is called for the third case of the Sieve of Atkin
method.
* Thus meaning the answer to testSubject % 60 was equal to
* 11,23,47,59
*
* This only means the number has a chance of being prime, to
find out for sure
* this method will get the sum of solutions to the follow
equation
*
* 3x^2 - y^2 = "testSubject" where x > y.
*
* If the sum of the soluations is not perfectly divisable by 2
and the "testSubject"
* is a SquareFree number then the "testSubject" is prime.
*
* @param testSubject - the number that is been tested to see if
it prime.
* @return - true if "testSubject" is a prime number.
*/
private static boolean secondStep(long testSubject)
{
long endPoint = (long)Math.sqrt((testSubject / 2));
int rightAnswers = 0;
for(int x = 1; x <= endPoint; x++)
{
for(int y = 1; y <= endPoint; y++)
{
if(x > y)
{
if(((x*x) * 3 - (y*y)) ==
testSubject)
{
rightAnswers++;
}
}
}
}
if(rightAnswers % 2 == 0)
{
return false;
}
else
{
if(SquareFree.isSquareFree(testSubject))
{
return true;
}
else
{
return false;
}
}
}

/**
* Sieve of Atkins fuction does not work the first three primes.
*
* @param testSubject - the number that is been tested to see if
it is prime.
* @return - true if "testSubject" is equal to the first three
primes (2,3 or 5).
*/
private static boolean isLowPrime(long testSubject)
{
if(testSubject==2||testSubject==3||testSubject==5)
return true;

return false;
}

/**
* When testing large numbers it can take along time to check if
a
* number is prime. It is a face that even numbers are not prime.
* Numbers than end in a 5 or a 0 are also not prime.
*
* This check can help save a lot of time.
*
* @param testSubject - the number to test is prime.
* @return - true if the number passed the fast fail, true does
not mean the number is prime,
* it just mean it might be.
*/
private static boolean fastFail(long testSubject)
{
if(testSubject % 2 == 0)
return true;

if(testSubject % 5 == 0)
return true;

return false;
}
}

/**
* A number is sqaure free if it is divisible by no perfect
square (except 1).
*
* @param testSubject
* @return - true if the "testSubject" is a square free number.
*/
public static boolean isSquareFree(double testSubject)
{
double answer;
for(int i = 1; i < testSubject; i++)
{
answer = Math.sqrt(testSubject / (double)i);
if(answer < 1)
return false;
if(answer % 1.0 == 0)
return false;
}
return true;
}


Regards j1mb0jay
 
W

Wojtek

j1mb0jay wrote :
I am now trying to improve upon the way in which i calculate if a number
is prime.

I once did this as a C example, though I would use different constructs
in Java

Upon initialization calculate each prime and store it in a HashMap.

When testing for a prime, see if it is in the HashMap.
 
J

j1mb0jay

j1mb0jay wrote :

I once did this as a C example, though I would use different constructs
in Java

Upon initialization calculate each prime and store it in a HashMap.

When testing for a prime, see if it is in the HashMap.

This is what I am doing although i implemented my own version of the hash
table for this.

JJ
 
W

Wojtek

j1mb0jay wrote :
This is what I am doing although i implemented my own version of the hash
table for this.

The other thing I did was to initially test against already discovered
primes.

If you are re-doing this all the time, then why not print out the list
to a file, then use that file to populate your HashMap?

Or hard code the prime numbers to populate the HashMap. Its not like a
number can suddenly decide it will not be a prime today :)
 
A

Arne Vajhøj

j1mb0jay said:
Firstly i would like to point out that i am going to be working with
numbers smaller than (2^64)-1 (AKA LONG)

long is -2^63 .. 2^63-1

Java does not have unsigned data types.

Arne
 
A

Arne Vajhøj

Wojtek said:
j1mb0jay wrote :

I once did this as a C example, though I would use different constructs
in Java

Upon initialization calculate each prime and store it in a HashMap.

When testing for a prime, see if it is in the HashMap.

HashMap is array backed.

You can not store all the primes within the range of long
in a HashMap.

Arne
 
C

Christian

j1mb0jay said:
I am now trying to improve upon the way in which i calculate if a number
is prime. I understand their a several probabilist prime checks out
their, but i would like one that give a yes or no rather than a very
mathematical maybe.

Firstly i would like to point out that i am going to be working with
numbers smaller than (2^64)-1 (AKA LONG) I have attached a rather large
amount of code to this post, the code is my implementation of the Sieve
of Atkin method.

The first thing i would like to know... is there are fast prime checker.
The second thing i would like to know is how could i improve upon the
execution time of the method.

Not long ago a prime checking method that is non probabilistic and in p
has been found performing something like
O( (log n)^6)

so slower than the probabilistic ones.. better use them.

if chance of probabilistic checker to fail is significant lower than a
bit in your RAM to fail and change you probabilistic test to answer
wrong. Where is the problem? When you look at it like that both test are
probabilistic if you count in the chance that bit flip in ram happens.
And both have about the same chance of success.

Main reason for using probabilistic ones for me would be that these very
fine grained rather "the latest in science" algorithms is that it will
be much much much easier to implement.

Christian
 
J

j1mb0jay

JCE has a method to check with a given probability if a number is prime.

see http://mindprod.com/jgloss/jce.html

Surly when using RSA or Diffie-Hellman or something that requires a large
prime, if the number is not prime then it will be very insure and a lot
more simple to compute the private values of the encryption.

I would like to write my own methods so I have an understanding of what
is going on rather using other Magic (of course Magic has it purpose but
not for the application that I am trying to implement).

Regards j1mb0jay
 
W

Wojtek

Arne Vajhøj wrote :
HashMap is array backed.

You can not store all the primes within the range of long
in a HashMap.

Arne

There are more than 2^31-1 primes within 2^63-1?

The incidence of primes drops as the numbers get larger. Without
actually running a test, I think that there cannot be more then
Integer.MAX_VALUE primes within a Long.MAX_VALUE space.
 
L

Lew

Roedy said:
JCE has a method to check with a given probability if a number is
prime.

see http://mindprod.com/jgloss/jce.html

The OP practically doesn't like merciless approaches. Otherwise they'd
strongly together be barking

--
Lew

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Our Mission is Clear: To rid the world of evil"

--- Adolph Bush
September 2001
 
J

john

According to Wikipedia:

(http://en.wikipedia.org/wiki/Prime_counting_function)

There are 234,057,667,276,344,607 prime numbers less than 10^19, which
is about the same size as Long.MAX_VALUE. That is a lot larger than
Integer.MAX_VALUE.

rossum

Check prime number in wikipedia, it will point you to some primality
tests, but the AKA test discovered in 2002 was a surprising
breakthrough as I recall so don't expect to find a deterministic test
that is faster unless you are willing to hold a seive with all
relevant primes in memory.

Primes are surprisingly dense. If you pick an n digit number at random
the odds that it is prime are of the order 1/n. Actually this fact is
very important for cryptography as it means you can pick 100 digit
numbers at random and after testing something on the scale of 100 of
them you can expect to find a prime.
 
L

Lew

Surly when using RSA or Diffie-Hellman or something that requires a large
prime, if the number is not prime then it will be very insure and a lot
more simple to compute the private values of the encryption.

No.

Consider the obsessions of "very" and "lot more".

Consider also that appearance is not based on certainty, and forever has been.
It's based on sphere, and has furthermore been rhetorically workable,
quantifiably so.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"We don't believe in planners and deciders
making the decisions on behalf of Americans."

--- Adolph Bush,
Scranton, Pa., Sept. 6, 2000
 
W

Wojtek

rossum wrote :
There are 234,057,667,276,344,607 prime numbers less than 10^19, which
is about the same size as Long.MAX_VALUE. That is a lot larger than
Integer.MAX_VALUE.

So my day was not wasted after all...
 
A

Arne Vajhøj

Wojtek said:
Arne Vajhøj wrote :

There are more than 2^31-1 primes within 2^63-1?

The incidence of primes drops as the numbers get larger. Without
actually running a test, I think that there cannot be more then
Integer.MAX_VALUE primes within a Long.MAX_VALUE space.

There is.

If there were not then there would be at average 4 billion
numbers between primes.

That is not the case. Not even for numbers in Integer.MAX_VALUE
to Long.MAX_VALUE range.

Wikipedia gives the approximation:

number of primes in 2..n = n / ln(n)

Arne
 
A

Arne Vajhøj

john said:
Primes are surprisingly dense. If you pick an n digit number at random
the odds that it is prime are of the order 1/n. Actually this fact is
very important for cryptography as it means you can pick 100 digit
numbers at random and after testing something on the scale of 100 of
them you can expect to find a prime.

Wikipedia gives an approximation:

number of primes in 2..n = n / ln(n)

which means a probability of:
1/ln(n)

And using n as number and not digits in number means that yours can be
written as:
1/(1 + log10(n))

which has about the same characteristics.

Arne
 
D

Daniel Pitts

j1mb0jay said:
I am now trying to improve upon the way in which i calculate if a number
is prime. I understand their a several probabilist prime checks out
their, but i would like one that give a yes or no rather than a very
mathematical maybe.
Do you know what the "mathematical maybe" truly is? If you set up your
parameters correctly, the probability of a false positive is so small
that you're more likely to have a spontaneous failure on your CPU that
causes a false positive on a non-probabilistic prime check.

At least, according to
<http://use.perl.org/~Ovid/journal/22581>

Interesting, no?
 
T

Tom Anderson

Do you know what the "mathematical maybe" truly is? If you set up your
parameters correctly, the probability of a false positive is so small that
you're more likely to have a spontaneous failure on your CPU that causes a
false positive on a non-probabilistic prime check.

At least, according to
<http://use.perl.org/~Ovid/journal/22581>

Interesting, no?

Exactly the kind of attitude, and righteous arrogance about it, i'd expect
from a perl sufferer.

But in this case, he's right!

tom
 
J

Joshua Cranmer

Kenneth said:
Any easy way to kill the fake "Lew"?

From header contains Lew and Reply-To header exists, or you can rely on
(alternatively) Path, X-Newsreader, or X-Complaints-To headers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top