Tony Docherty wrote:The line printing out "The number is prime" shouldn't be inside the for loop as you can only tell if a number is prime after the for loop has completed.
Tony Docherty wrote:Why do you think you still need the 'else'?
What test do you think you need to use to restrict the line to only print out if it is a prime number?
Tony Docherty wrote:You already have a variable which, once the for loop completes, tells you whether the number was prime or not.
Ulf Dittmer wrote:As an aside, you shouldn't start the for loop at 1, and it shouldn't run until "i == x" - even a prime numbers is divisible by 1 and by itself.
The first three lines are unwanted, how should I let program understand that if it's prime, it should skip those three lines ?
Tony Docherty wrote:
The first three lines are unwanted, how should I let program understand that if it's prime, it should skip those three lines ?
All numbers are divisible by 1 and themselves so are you sure you even need to print this out for composite numbers.
Akimbas Akimbasas wrote:My second loops does just that.(in the code, which is above the most recent.) But it doesn't make(I mean the results of code), I guess should think about what to write more.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Bingo. And furthermore, you should ALWAYS think before you write even one line of code.![]()
Tip for you: The real problem here is not simply to find out if a number is prime - there are lots of ways to do it by brute force - it's to work out the correct steps for doing so.
So, assuming you're going to use a "progressive factor check" - and there's absolutely nothing wrong with that - a couple more questions for you, that may help you to get to the solution:
1. You now know that you shouldn't start at 1, because ALL numbers divide by 1; but where do you end? ie: what is the largest factor you need to check?
2. Do you think you need to check ALL factors in the range? For example: if I already know that my number n doesn't divide by either 2 or 3, is there any point in me checking if it divides by 6? And if not, which factors do you think make most sense to check?
HIH
Winston
Akimbas Akimbasas wrote:What do you think about the code I posted a bit above, it does seem to work.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Yup. Looks like it to me too; but it's what I'd classify as a "brute force" check - ie, it does the job, but it contains a lot of checks that simply aren't required. And just to give you some idea how redundant it is: if you give it the number 971 (which is prime), it will spend around 98% of its time (and probably more) doing unnecessary work. But it will give you the correct answer.
So, if your only consideration is whether the program produces the correct result, then your job is done. However, if you'd like to try for the car, see if you can make it work a bit more efficiently.![]()
I should add that this is one of the very few times that I would advise looking at efficiency.
In general, if you have a solution that is
(a) correct and
(b) simple (and your program qualifies on both counts)
then your job is done.
But in this case, prime number checking is a very useful generic tool, so if you can also make it as fast as possible, you'll have a very useful method you can stick in a utility class somewhere, for later use.
Winston
Akimbas Akimbasas wrote:Thanks, since now I am a rookie, for a while I will only try to get the answer, once I get better at programming, I'll try pay to more attention to the efficiency of the program. I know that good program consists not only of a task getting done, but also about a task getting done fast.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here