Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hello everyone. i am playing around with generating prime numbers using the isProbablePrime function in the BigInteger class. However, I would like to generate a very large number of primes in sequential order. I have this part figured out, however i would like to store them all in an array of a given size. is this possible? if so how do i do it? thanks very much.

--- More BigInteger Stuff ---
is there anyway to set a variable as type BigInteger and place a value into that
for ex. BigInteger temp=5;
my goal is to fill an array (see above) with say the first 100,000 primes. i want to do this with testing every odd number untill the array is filled. so basically i would like to say...
BigInteger value = 3;
while (position
{
if(value.isProbablePrime(50))
{
array[position]=value;
position++;
}
else
System.out.println(value + "not prime");value=value+2
}syntax not the best, but i hope it gives a general idea. if anyone knows how this can be done please let me know. if this is not possible, please let me know that as well...could save some headaches. thanks everyone.

Just wanted to comment that testing every odd number for prime gives very poor performance. A much faster way is to check numbers against previously found primes, up to the squareroot of the number. Since you are already trying to store primes in an array this is fairly easy and you don't have to use custom defined types, the standard int or long int should be fine. The code is pretty quick, giving about 1000 primes per sec here when printed to the screen, probably much faster if the output was omitted or sent to a file.
The code won't format well posted here, so:
http://66.24.125.81/prime.cpp

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |