Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi can any one help me with this code to
"Write a program to determine if the sequence of numbers 11,101,1001,10001…. are all prime numbers up to and including the largest sequence value smaller than the largest integer value represent able on a PC.
This is what i did..
public class PrimeNumberTest
{
public static void main(String a[])
{
int fac=0;for(int i=2; i<100; i++)
{
for(int j=2; j<=i/2; j++)
{
if(i%j == 0)
fac++;
}if(fac > 0)
System.out.println(i+ " is Not Prime");
else
System.out.println(i+ " is Prime");
fac=0;
}
}Hi I'm an MIS Masters student.

Your program appears like will tell whether numbers between 2 and 100 is a prime. (Though not optimized).
If you are interested in finding whether the sequence 11, 101, 1001, ... etc is prime, change the outer for loop to-
for (i=11,i < 1000000000; i=(i-1)*10+1)Your series can go upto the maximum number supported by the type of i, that is int, which I guess is dependent on your OS.
--
Holla.

You seem to be using Java. Therefore the maximum value of an int is not dependent on your OS. It is 2147483647.
Your problem is how will you be able to check the larger numbers, up to 1000000001, in a reasonable time. If you do it in a naive way, the computation will take a few seconds, but with some thought you can significantly shorten it.

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

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