# Prime numbers

• 01-31-2008, 02:27 PM
gapper
Prime numbers
Hello Guys,

I am working on an assignment for find first 1000 prime numbers. Any help would be appreciated :D

I would like recursive method if possible.

Cheers.
• 01-31-2008, 09:47 PM
tim
Spoiler
Hello gapper

Here's a spoiler:
Code:

```public class Prime{         protected Vector<Integer> getFactors(int integer){                 Vector<Integer> result = new Vector<Integer>();                 for (int i = 1; i <= integer; i++)                         if (integer &#37; i == 0) result.add(new Integer(i));                 return result;         }         public Prime(int bound){                 System.out.println("Prime numbers up to " + bound + ":");                 for (int i = 2; i <= bound; i++){                         if (getFactors(i).size() == 2) System.out.println(i);                 }         } }```
Use the constructor to run it
Code:

`new Prime(20);`
gives Output
Code:

```Prime numbers up to 20: 2 3 5 7 11 13 17 19```
This is a classical assignment. I could not resist. :D
• 02-05-2008, 09:04 AM
Cnu
{
public static void main(String[] args)
{
int i,j;
i=1;
while(i<=1000)
{
j=2;
while(j<=i-1)
{
if(i&#37;j==0) break;
j++;
}
if(i==j)
System.out.print(i);
i++;
}
}
}
• 02-07-2008, 11:09 AM
divyachaparala
prime number....
{

public static void main(String[] args)
{

prime pr = new prime();
int number=Integer.parseInt(args[0]);
System.out.print("PRIME numbers are:");
for (int k = 0; k< primeNum.length; k++)
{
}
}
}

public class prime
{

{

int factCount=0;
int result[]=new int[4];
int k=0;

int j=0;

while(j<=primeno)
{
for(int i=2;i<=j;i++)
{
if(j%i==0)
{
factCount++;
}
}
if(factCount==1)
{
result[k]=j;

k++;
}
else
{

}
j++;
factCount=0;
}
return result;

}

}