Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-31-2008, 02:27 PM
Member
 
Join Date: Jan 2008
Posts: 17
gapper is on a distinguished road
Prime numbers
Hello Guys,

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

I would like recursive method if possible.

Cheers.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-31-2008, 09:47 PM
tim's Avatar
tim tim is offline
Senior Member
 
Join Date: Dec 2007
Location: South Africa
Posts: 334
tim is on a distinguished road
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 % 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.
__________________
If your ship has not come in yet then build a lighthouse.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-05-2008, 09:04 AM
Cnu Cnu is offline
Member
 
Join Date: Feb 2008
Posts: 13
Cnu is on a distinguished road
class PrimeNumbers
{
public static void main(String[] args)
{
int i,j;
i=1;
while(i<=1000)
{
j=2;
while(j<=i-1)
{
if(i%j==0) break;
j++;
}
if(i==j)
System.out.print(i);
i++;
}
}
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-07-2008, 11:09 AM
Member
 
Join Date: Feb 2008
Posts: 3
divyachaparala is on a distinguished road
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;


}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare 5 numbers Snowboardmylife New To Java 5 04-15-2008 08:04 PM
Computing prime numbers in Java Java Tip java.lang 0 04-12-2008 09:39 PM
Finding Largest Prime Factor perito New To Java 2 03-26-2008 07:04 AM
random numbers carlos123 New To Java 1 12-22-2007 03:56 AM
Counting numbers up and down radio New To Java 3 12-01-2007 08:08 PM


All times are GMT +3. The time now is 12:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org