Results 1 to 5 of 5
- 12-22-2011, 09:59 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Feedback on my PrimeNumber Finder code please.
Hi guys.
I am a beginner in programming, and trying to learn JAVA.
I wrote this code myself where I am looking for prime numbers and printing them on the screen:
It works fine, but I have no experienced people around me, so there is no one to say, nobody would do it like this.Java Code:package myPackage; import java.util.ArrayList; public class TestClass { public static void main(String[] args) { ArrayList<Integer> primeNumbers = new ArrayList<Integer>(); primeNumbers.add(2); primeNumbers.add(3); int m; ArrayList<Integer> controller = new ArrayList<Integer>(); for(int isThisAPrimeNumber=4;isThisAPrimeNumber<40;isThisAPrimeNumber++) { for(int k=0;k<primeNumbers.size();k++) { m = primeNumbers.get(k); controller.add(isThisAPrimeNumber%m); } if(controller.contains(0)) { controller.clear(); } else { primeNumbers.add(isThisAPrimeNumber); controller.clear(); } } for(int t:primeNumbers) { System.out.println(t); } } }
Can you have a peek and tell me if this is ok?
- 12-22-2011, 10:15 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Feedback on my PrimeNumber Finder code please.
It's a reasonable approach provided you realise that it relies on building up a list of primes.
Notice that you find *all* of the prime factors of each number (these are the ones where test%m is zero) but in fact you know that the number being tested is *not* a prime once you have found the first prime factor. You could break out of the inner for loop at this point. With that change it might make sense to remove controller altogether.
- 12-22-2011, 10:21 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Feedback on my PrimeNumber Finder code please.
I am trying it doing without controller, but I am not very successful at it :)
- 12-22-2011, 10:43 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Feedback on my PrimeNumber Finder code please.
That's less important than working on the fact that you can act once you have found the first prime factor.
Does that make sense?Java Code:for each number isPrime = true for each prime if it is a prime factor isPrime = false break // don't check any more primes if the number isPrime add it to the primes list
isPrime is behaving a bit like your controller. But it is reset just *before* the inner for loop. And it is reset to true because the function of the inner loop is to detect things which are *not* prime.
- 12-22-2011, 10:43 PM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Similar Threads
-
PrimeNumber
By zit1343 in forum New To JavaReplies: 4Last Post: 05-01-2011, 07:52 PM -
Path Finder
By OmerHalit in forum AWT / SwingReplies: 2Last Post: 04-05-2010, 08:09 PM -
Java primenumber application
By Crossfires in forum New To JavaReplies: 9Last Post: 10-12-2009, 06:06 PM -
sign finder
By iPetey in forum New To JavaReplies: 6Last Post: 05-01-2009, 05:24 AM -
Library Finder 1.2
By JavaBean in forum Java SoftwareReplies: 0Last Post: 07-15-2007, 04:43 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks