Results 1 to 4 of 4

Thread: Prime numbers

  1. #1
    gapper is offline Member
    Join Date
    Jan 2008
    Posts
    17
    Rep Power
    0

    Default 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.

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Spoiler

    Hello gapper

    Here's a spoiler:
    Java 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
    Java Code:
    new Prime(20);
    gives Output
    Java Code:
    Prime numbers up to 20:
    2
    3
    5
    7
    11
    13
    17
    19
    This is a classical assignment. I could not resist. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    Cnu
    Cnu is offline Member
    Join Date
    Feb 2008
    Posts
    13
    Rep Power
    0

    Default

    class PrimeNumbers
    {
    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++;
    }
    }
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0

    Thumbs up prime number....

    public class PrimeNumber
    {

    public static void main(String[] args)
    {

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



    public class prime
    {



    int[] primeNumbers(int primeno)
    {

    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;


    }

    }

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 03:03 PM
  2. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 08:25 PM
  3. Compare 5 numbers
    By Snowboardmylife in forum New To Java
    Replies: 5
    Last Post: 04-15-2008, 07:04 PM
  4. Computing prime numbers in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 08:39 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 02:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •