Results 1 to 5 of 5
Thread: PrimeNumber
- 05-01-2011, 07:31 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 17
- Rep Power
- 0
PrimeNumber
Hey
this is not my homework
I am studying my first test for final
here is the question
please help me
Consider the following unfinished class:
thank youJava Code:public class MakePrimes { int v[]; public MakePrimes () { v = new int[100]; } boolean isPrime (int n) { ??? } public void fillV() { ??? }
- 05-01-2011, 07:39 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ask a more specific question, and show some more work. What are you trying to do? Sieve of Eratosthenes? Just a regular approach? Try it and we will help you and possibly offer optimization ideas. For instance, did you know that when searching for a prime you will know whether it is prime by the time you reach the square root of n? Also, after testing 2, you can skip all even numbers from then on(2, 3, 5, 7, 9, etc.)
- 05-01-2011, 07:44 PM #3
Member
- Join Date
- Feb 2011
- Posts
- 31
- Rep Power
- 0
I found this class using a quick Google search
Java Code:/* This class writes out Prime numbers. A number is prime if it is divisible by 1 and the number itself and no other number. */ public class PrimeNumber { // This method tests whether a given number is prime or not. public static boolean isPrime ( int num ) { boolean prime = true; int limit = (int) Math.sqrt ( num ); for ( int i = 2; i <= limit; i++ ) { if ( num % i == 0 ) { prime = false; break; } } return prime; } public static void main ( String[] args ) { // This loop writes out all the prime numbers less than 1000. for ( int i = 2; i <= 1000; i++ ) { if ( isPrime ( i ) ) System.out.println ( i ); } } }
- 05-01-2011, 07:47 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I understand the intentions here are good oomrichie, however; please don't spoonfeed. If he was pushed towards the right answer he would get a lot more help than just being given the answer. I know you found this on google and so could he have. Still, it doesn't help him learn much.
- 05-01-2011, 07:52 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 31
- Rep Power
- 0
Similar Threads
-
Java primenumber application
By Crossfires in forum New To JavaReplies: 9Last Post: 10-12-2009, 06:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks