This content has been marked as final.
Show 4 replies

1. Re: Find prime numbers of BigInteger
TPDOpitz Feb 3, 2013 4:57 PM (in response to 831755)Looks like java does not apply autoboxing in this case. It cannot, because a BigInteger can hold values much larger that int could ever hold.
Depending on how big your initial value can get it might not be suitable to convert the BigInteger to an int, because you'll lose leading digits. So your only chance may be to use a plain <tt>while</tt> and the methods BigInteger offers.
bye
TPD 
2. Re: Find prime numbers of BigInteger
mycoffee Feb 4, 2013 2:12 PM (in response to 831755)Also, it looks like your algorithm is not efficient. You try to scan ALL numbers from 1 to n and and scan from i to 1 that is N^2. For big number, it can run forever ...
To check a number whether prime or not you should check with prime numbers only
So create a List to hold the prime numbers and add to it every time you find one. Then use the List to check the bigger numbers 
3. Re: Find prime numbers of BigInteger
TPDOpitz Feb 4, 2013 2:31 PM (in response to mycoffee)mycoffee wrote:
Additionally you can stop checking when the next prime devider is bigger that the numbers half (which means that the result of the devision is less than two), eg: when checking <tt>23</tt> you will find the result of <tt>11</tt> beeing <tt>2.09...</tt>. The result of <tt>13</tt> is <tt>1.79...</tt>, so you don't need to check <tt>17</tt>.
To check a number whether prime or not you should check with prime numbers only
So create a List to hold the prime numbers and add to it every time you find one. Then use the List to check the bigger numbers
bye
TT 
4. Re: Find prime numbers of BigInteger
mycoffee Feb 4, 2013 4:07 PM (in response to TPDOpitz)TPD OpitzConsulting com wrote:
Yes, but not a half, it is sqrt of the number. For example, when check number 101, you should check if it divisible to 2, 3, 5, 7 and that is it, since the next prime, 11, 11^2 > 101mycoffee wrote:
Additionally you can stop checking when the next prime devider is bigger that the numbers half (which means that the result of the devision is less than two), eg: when checking <tt>23</tt> you will find the result of <tt>11</tt> beeing <tt>2.09...</tt>. The result of <tt>13</tt> is <tt>1.79...</tt>, so you don't need to check <tt>17</tt>.
To check a number whether prime or not you should check with prime numbers only
So create a List to hold the prime numbers and add to it every time you find one. Then use the List to check the bigger numbers
bye
TT
Anyway, this is about Math, not about java and I think the forum is not to solve the homework problem :)