Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Prime Number Checker--working, but won't take numbers long enough.

807588Jun 10 2009 — edited Jun 12 2009
Hi, all:

I've build a prime number checker. Its purpose is to find primes after 1 trillion. It works fine to a billion, but at 10 billion the compiler gives me this:
~/Documents/ProgAndDev/JavaPractice/Speculate : Yes, Supreme Empress? $ javac Prime.java
Prime.java:21: integer number too large: 10000000000
  public static long number = 10000000000;
                              ^
1 error
Here's the class:
import java.lang.Long;
import java.lang.Math;

class Prime {

  public static long number = 1000000000;
  public static boolean p;

  public static boolean isPrime (long n) {
    boolean prime = true;
    for (long i = 3; i <= Math.sqrt(n); i += 2){
      if (n % i == 0) {
	prime = false;
	p = false;
	break;
      }
      if (( n%2 !=0 && prime && n > 2) || n == 2) {
	p = true;
	return true;
      } 
      else {
	p = false;
	return false;
      }
    }
    return prime;
  }

  public static void main(String args[]) {
    isPrime(number);
    System.out.println("It is " + p + " that n is a prime number.");
  }
}
Do I need to wrap the long?

Comments

807588
No, you need to learn how to write a long literal properly. What you have currently is a integer literal. Try
public static long number = 10000000000L;
807588
And you don't ever have to import classes in java.lang either.
807588
A good post..
thanks for it...

[fasting weight loss|http://www.efastingweightloss.com]
807588
tommyers wrote:
A good post..
thanks for it...
Yeah great.

Why don't you go play in traffic for awhile. Your account and all your posts will be deleted. Nobody here is so stupid as to not be able to recognize a spammer in about .3 seconds.
807588
Thanks for the help!

So, I have it checking properly now...and though the class WORKS, I don't think it's very elegant. I'd like to start with 1 trillion and 1, b/c obviously 1 trillion isn't a prime number. I want to find twin primes, which just means that if you add 2 to a given prime and it also is a prime, you've found a pair of twin primes. Though my class finds that 1 trillion and 1 & 1 trillion and 3 are twin primes, there's no iterating going on, and I think it's kinda ugly. Any suggestions on how I can improve this?
class Prime {

  public static long number = 100000000001L;
  public static long plusTwo = number + 2; 
  public static boolean p;
  public static boolean q;

  public static boolean isPrime (long n) {
    boolean prime = true;
    for (long i = 3; i <= Math.sqrt(n); i += 2){
      if (n % i == 0) {
	prime = false;
	p = false;
	break;
      }
      if (( n%2 !=0 && prime && n > 2) || n == 2) {
	p = true;
	return true;
      } 
      else {
	p = false;
	return false;
      }
    }
    return prime;
  }

  public static boolean isTwinPrime (long o) { 
    boolean nextPrime = true;
    for (long i = 3; i <= Math.sqrt(o); i += 2){
      if (o % i == 0) {
	nextPrime = false;
	q = false;
	break;
      }
      if (( o%2 !=0 && nextPrime && o > 2) || o == 2) {
	q = true;
	return true;
      } 
      else {
	q = false;
	return false;
      }
    }
    return nextPrime;
  }
807588
Ok, I broke it again. I got myself stuck in an infinite loop here:
class Prime {

  public static long number = 100000000003L;
  public static long plusTwo = number + 2; 
  public static boolean p;
  public static boolean q;
  public static boolean t;

  public static boolean isPrime (long n) {
    boolean prime = true;
    for (long i = 3; i <= Math.sqrt(n); i += 2){
      if (n % i == 0) {
	prime = false;
      }
      if (( n%2 !=0 && prime && n > 2) || n == 2) {
	return true;
      } 
      else {
	return false;
      }
    }
    p = prime;
    return prime;
  }

  public static boolean isTwinPrime (long o) { 
    boolean nextPrime = true;
    for (long i = 3; i <= Math.sqrt(o); i += 2){
      if (o % i == 0) {
	nextPrime = false;
	break;
      }
      if (( o%2 !=0 && nextPrime && o > 2) || o == 2) {
	return true;
      } 
      else {
	return false;
      }
    }
    q = nextPrime;
    return nextPrime;
  }

  public static boolean checker () {
    boolean twins = true;
    if (p == true && q == true) {
      long r = (number + plusTwo)/2;
      System.out.println("The average of the first twin primes after 1 trillion is: " + r);
    }
    else {
      System.out.println("p = " + p + ", and q = " + q );
      twins = false;
      number = number + 2;
    }
    t = twins;
    return twins;
  }

  public static void main(String args[]) {
    isPrime(number);
    isTwinPrime(plusTwo);
    checker();
    while (t == false) {
	isPrime(number);
	isTwinPrime(plusTwo);
	checker();
    }
  }
}
This should iterate through all numbers after 1 trillion and 3 to find twin primes; instead I get repeated "p is false and q is false" output. What did I do?
DrClap
Well, you're using one of the slowest possible algorithms for determining whether a number is prime. There are some trivial changes you could do -- like not calculating Math.sqrt(n) inside the loop -- but they aren't going to make much difference.

There has been well over 50 years of research into primality testing, it goes way back before computers were even invented. You could make use of some of that research, I'm sure.
807588
DrClap wrote:
Well, you're using one of the slowest possible algorithms for determining whether a number is prime. There are some trivial changes you could do -- like not calculating Math.sqrt(n) inside the loop -- but they aren't going to make much difference.

There has been well over 50 years of research into primality testing, it goes way back before computers were even invented. You could make use of some of that research, I'm sure.
I'm less concerned about the computational speed; I'd rather get the code elegant and THEN fiddle with it. I already know that 1000000000001 and 10000000000003 are twin primes; why when I start the value of "number" at 1000000000001 do I still get that loop? Theoretically speaking, it should stop and print the success message on the first iteration, but it does not.
JosAH
1000000000001 == 73*137*99990001
1000000000003 == 61*14221*1152763
10000000000001 == 11*859*1058313049
10000000000003 == 13*29*547*48492137

kind regards,

Jos
Tolls
tarahmarie101 wrote:
I'm less concerned about the computational speed; I'd rather get the code elegant and THEN fiddle with it. I already know that 1000000000001 and 10000000000003 are twin primes; why when I start the value of "number" at 1000000000001 do I still get that loop? Theoretically speaking, it should stop and print the success message on the first iteration, but it does not.
Because, rightly or wrongly (I don't know), isTwinPrime(plusTwo) returns false. I've just run it.

As for why it keeps looping, I don't think you increment plusTwo at all, so you're always getting a check against 10000000000003 for plusTwo which always returns false in the call to isTwinPrime().
JosAH
Those two numbers aren't prime (see my previous reply).

kind regards,

Jos
Tolls
JosAH wrote:
Those two numbers aren't prime (see my previous reply).

kind regards,

Jos
Well, yes...as you've shown. However the OP clearly hasn't run a test with these values using his own algorithm, since it rejects the second one in any case (though it accepts the first which you've shown to be incorrect)...so why they think it should stop the first time round I don't know.
807588
Tolls wrote:
JosAH wrote:
Those two numbers aren't prime (see my previous reply).

kind regards,

Jos
Well, yes...as you've shown. However the OP clearly hasn't run a test with these values using his own algorithm, since it rejects the second one in any case (though it accepts the first which you've shown to be incorrect)...so why they think it should stop the first time round I don't know.
I've run the test, it's just that the algorithm bites. Here's the algorithm, and if IT works properly, my code should:
  public static boolean isPrime (long n) {
    System.out.println("number = " + number);
    boolean prime = true;
    for (long i = 3; i <= Math.sqrt(n); i += 2){
      if (n % i == 0) {
	prime = false;
	p = false;
	break;
      }
      if (( n%2 !=0 && prime && n > 2) || n == 2) {
	System.out.println("First Prime Found with value of " + n);
	p = true;
	return true;
      } 
      else {
	p = false;
	return false;
      }
    }
    return prime;
  }
Should this not successfully test whether any given number is in fact a prime?
807588
tarahmarie101 wrote:

Should this not successfully test whether any given number is in fact a prime?
No. try it with 35.
It just seems to find numbers that are not divisible by 2 or 3.
I think that what it is doing. It seems unnecessarily complex for determing if a number is prime or not.

I know you have some artifacts from the original class in there (I'm not liking them too much I have to say).
But perhaps it would be easier for you to debug if the method simply determined if the supplied number as prime or not.
It should not be setting outside flags et cetra.
807588
Ok, I have a better algorithm now, and the code works. Here's the class, and some sample output:
class Prime {

  public static long number = 1000000000070L;
  public static long output;
  public static long test;
  public static long nextPrime;
  public static long storage;
  public static long root;
  public static boolean isPrime;

  public static long isPrime(long query){//is primes algorithm
    if (query<=2) {
      System.out.println(2);
      System.exit(0); 
    }
    for (int k=3;k<9;k+=2) {
      if (query<=k) {
	System.out.println(k);
	System.exit(0); 
      } 
    }
    if (query == ((query >> 1) << 1)) { 
      query+=1; 
    }
    for (long i=query;;i+=2) {
      root=(long)Math.sqrt(i);
      for (long j=3;j<=root;j++) {
	if (i == (i/j)*j) {
	  isPrime=false;
	  break; 
	}
	if (j==root) { 
	  isPrime=true; 
	} 
      }
      if (isPrime==true) {
	output = i;
	break; 
      } 
    }
    return output;//the first prime to test against a second.
  } 

  public static long findNextPrime (long nextQuery) {
    storage = nextQuery;//stores the value of the first prime
    test = nextQuery + 2;//next number to test
    nextPrime= isPrime(test);//stores value of prime check on test.
    System.out.println("The smaller prime is " + storage + ", and the larger prime is " + nextPrime);
    return nextPrime;
  }

  public static void iterator () {
    do {
      findNextPrime(output);
    } while ((nextPrime - storage)!= 2);
    System.out.println("Twin primes found.");
    finish();
  }

  public static void finish() {
    long average = (nextPrime + storage)/2;
    System.out.println("The average of the smallest twin primes with a value of greater than one trillion is: " + average + ".");
  }

  public static void main(String args[]) {
      isPrime(number);
      findNextPrime(output);
      iterator();
  }
}
tarahmarie@tarahmarie-laptop:~/Speculate$ javac Prime.java
tarahmarie@tarahmarie-laptop:~/Speculate$ java Prime
The smaller prime is 1000000000091, and the larger prime is 1000000000121
The smaller prime is 1000000000121, and the larger prime is 1000000000163
The smaller prime is 1000000000163, and the larger prime is 1000000000169
The smaller prime is 1000000000169, and the larger prime is 1000000000177
The smaller prime is 1000000000177, and the larger prime is 1000000000189
The smaller prime is 1000000000189, and the larger prime is 1000000000193
The smaller prime is 1000000000193, and the larger prime is 1000000000211
The smaller prime is 1000000000211, and the larger prime is 1000000000271
The smaller prime is 1000000000271, and the larger prime is 1000000000303
The smaller prime is 1000000000303, and the larger prime is 1000000000331
The smaller prime is 1000000000331, and the larger prime is 1000000000333
Twin primes found.
The average of the smallest twin primes with a value of greater than one trillion is: 1000000000332.
Are there any glaringly obvious errors?
1 - 15
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 10 2009
Added on Jun 10 2009
15 comments
515 views