| | |
Help to find prime numbers without using an array
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
Hey all,
In computer class as homework we're to create a code to find prime numbers between two values m and n without using an array (since we havn't learned about how to use them yet).
so far the code I have is:
This are the two methods - prime and isPrime - I created to find the prime numbers between the two numbers m and n. The program works fine, however if m = 10 and n = 10000, the program takes a very long time to print out all the prime numbers inbetween 10 and 10000. I would like to shorten this time. I assume this is because of the for loop inside the isPrime method. Hopefully someone will be able to help me here since I have been struggling with this problem for quite some time.
Thanks
In computer class as homework we're to create a code to find prime numbers between two values m and n without using an array (since we havn't learned about how to use them yet).
so far the code I have is:
Java Syntax (Toggle Plain Text)
public static void prime (int m, double n){ int prime = m; while (prime <= n){ if(isPrime(prime)){ System.out.print(prime+", "); } prime ++; } } public static boolean isPrime (int p){ for (int x = 2; x < p; x++){ if (p%x == 0){ return false; } } return true; }
This are the two methods - prime and isPrime - I created to find the prime numbers between the two numbers m and n. The program works fine, however if m = 10 and n = 10000, the program takes a very long time to print out all the prime numbers inbetween 10 and 10000. I would like to shorten this time. I assume this is because of the for loop inside the isPrime method. Hopefully someone will be able to help me here since I have been struggling with this problem for quite some time.
Thanks
Well, you probably don't want to get into the more esoteric algorithms for finding primes, but there are a few simple modifications that will reduce the number of checks you have to make.
If
If that is not the case, you can start the loop at 3 and increment by 2 for each iteration. You don't need to check any other even numbers.
Your loop also doesn't need to check any higher than
Maybe that helps a bit.
(I'm assuming you don't need to accomodate the check for p=1)
If
p%2==0, it's prime.If that is not the case, you can start the loop at 3 and increment by 2 for each iteration. You don't need to check any other even numbers.
Your loop also doesn't need to check any higher than
sqrt(p).Maybe that helps a bit.
(I'm assuming you don't need to accomodate the check for p=1)
Last edited by Ezzaral; Oct 30th, 2007 at 8:18 pm.
•
•
Join Date: Oct 2007
Posts: 2
Reputation:
Solved Threads: 0
Wow that really makes sense. Thank you so much for the help. The only clarification that I need is that for , where is that applied? Is it at the part where it says ?
Thanks
Java Syntax (Toggle Plain Text)
sqrt(p)
Java Syntax (Toggle Plain Text)
for (int x = 2; x < p; x++)
Thanks
Last edited by nucleareactr; Oct 30th, 2007 at 11:06 pm. Reason: Clarification
•
•
•
•
Wow that really makes sense. Thank you so much for the help. The only clarification that I need is that for, where is that applied? Is it at the part where it saysJava Syntax (Toggle Plain Text)
sqrt(p)?Java Syntax (Toggle Plain Text)
for (int x = 2; x < p; x++)
Thanks
![]() |
Similar Threads
- Finding Prime numbers without using Boolean (C++)
- how do u find prime numbers in an array (C)
- C++ prime numbers (C++)
- prime numbers (C++)
- prime numbers (C++)
Other Threads in the Java Forum
- Previous Thread: parent/child
- Next Thread: Help on this Java error message. Part 1
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






