Prime Numbers
Im trying to print prime numbers from 1 to 10,000. But,my program below prints all the way to hundred thousands,not sure what im doing wrong here.Here's what I did:
import java.math.*;
public class LargePrimeNumbers {
public static void main(String[] args) {
BigInteger prime = new BigInteger("1");
for (int i = 1; i <= 10000; i++)
System.out.println(prime = prime.nextProbablePrime());
}
}
[449 byte] By [
exl5a] at [2007-11-27 6:31:08]

You aren't printing the primes between 1 and 10,000, you're printing the first 10,000 primes. You start at one and loop 10,000 times, each time getting the next prime. So for the i=2, you get 2, for i=3 you get three, but for i=4 you get five, then 7, then 11, etc. See how the primes will end up higher than 10,000 when i is 10,000?
You need to loop until the prime you get is above 10,000, then stop.
It works for me. It prints 10,000 probable primes.
> It works for me. It prints 10,000 probable primes.The OP's wording is a bit confusing. It can be interpreted as "I want to print all primes between 1 and 10,000."
> > It works for me. It prints 10,000 probable primes.> > The OP's wording is a bit confusing. It can be> interpreted as "I want to print all primes between 1> and 10,000."I guess I'm being optimistic.
I guess youre right!! Its confusing cause obviously I just realized the wording.I would like to print the primes between 1 and 10,000,any ideas how to do that cause it does not seem thats what im doing here with the code above
exl5a at 2007-7-12 17:56:01 >

for(int i=2; i<=10000; i=prime.nextProbablePrime().intValue()){System.out.println(i);}
loop from 1 to 10000 {if current number is prime {print it out}}
while( number.nextProbablePrime() <= 10,000 ){}
This code prints an infinite loop,and it prints just 2.for(int i=2; i<=10000; i=prime.nextProbablePrime().intValue()){System.out.println(i);}Message was edited by: exl5
exl5a at 2007-7-12 17:56:01 >

Whoops.for(int i=prime.intValue(); i<=10000; i=(prime = prime.nextProbablePrime()).intValue())
> This code prints an infinite loop,and it prints just 2.Therein lies the problem of just copying someone else's code instead of working it out for yourself.
excellent..thank you so much. It prints from 1 to 9973
exl5a at 2007-7-12 17:56:01 >

OOP is confusing!!!Is printing the number of primes between 0-10,000 the same as printing the prime numbers between 0-10,000?
exl5a at 2007-7-12 17:56:01 >

My interprettion:Prime numbers between 0 and 202, 3, 5, 7, 11, 13, 17, 19Number of primes between 0 and 208
> OOP is confusing!!!
> Is printing the number of primes between 0-10,000 the
> same as printing the prime numbers between 0-10,000?
import java.math.BigInteger;
public class PrimeCounter{
public static void main(String[] args){
int count = 0;
BigInteger prime = BigInteger.ZERO;
BigInteger limit = new BigInteger("30");
while((prime = prime.nextProbablePrime()).compareTo(limit) == -1){
count ++;
System.out.println("Prime: " + prime);
}
System.out.println("Number of Primes: " + count);
}
}
import java.math.*;
public class LargePrimeNumbers {
public static void main(String[] args) {
BigInteger prime = new BigInteger("1");
while ( prime.compareTo( new BigInteger("10000") ) == -1 ){
System.out.println(prime);
prime = prime.nextProbablePrime();
}
}
}
try this code
> try this codebeat you by a minute ; )
import java.math.*;
public class LargePrimeNumbers {
public static void main(String[] args) {
BigInteger prime = new BigInteger("1");
while ( prime.compareTo( new BigInteger("10000") ) == -1 ){
System.out.println(prime);
prime = prime.nextProbablePrime();
}
}
}
try this code
echo echo echouse code tags in the future
Yeah. Thats exactly my take as well..At first it all seem confusing. Well put.Thank you guys..it works great. Now..OOP is less confusing as it was 10 minutes ago...Message was edited by: exl5
exl5a at 2007-7-21 21:57:37 >

Cool!! Thats a neat way of doing the same thing...the (-1) kinda throws you off at first..
import java.math.*;
public class LargePrimeNumbers {
public static void main(String[] args) {
BigInteger prime = new BigInteger("1");
while ( prime.compareTo( new BigInteger("10000") ) == -1 ){
System.out.println(prime);
prime = prime.nextProbablePrime();
}
}
}
try this code
I guess there are many ways to get to Rome :-)
Message was edited by:
exl5
exl5a at 2007-7-21 21:57:37 >

What does prime numbers have to do with OOP?
Nothing really.Its just the whole Java thing in general I was referring to.Sometimes the concepts in OOP are confusing specially if youre new to it.
exl5a at 2007-7-21 21:57:37 >

To be honest you really aren't using OOP in this program.
> What does prime numbers have to do with OOP?he means he had an "oops" moment when he realised he'd been wrong?