Computer Science Canada help - listing prime numbers |
Author: | whoareyou [ Thu Mar 03, 2011 6:16 pm ] | ||
Post subject: | help - listing prime numbers | ||
What is it you are trying to achieve? I am trying to write a program, using loop and for, that will allow me to print the first 200 prime numbers. and have then 10 a line. What is the problem you are having? when i run my code, it is listing every single number! and i don't know how to arrange the numbers 10 to a line. Describe what you have tried to solve this problem setting a value for the number, instead of 0 Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Please specify what version of Turing you are using 4.1.1 |
Author: | Tony [ Thu Mar 03, 2011 6:19 pm ] |
Post subject: | Re: help - listing prime numbers |
whoareyou @ Thu Mar 03, 2011 6:16 pm wrote: setting a value for the number, instead of 0
How are you setting the value of 0? Can you use the same technique to set another value? |
Author: | whoareyou [ Thu Mar 03, 2011 6:27 pm ] |
Post subject: | Re: help - listing prime numbers |
Tony @ Thu Mar 03, 2011 6:19 pm wrote: whoareyou @ Thu Mar 03, 2011 6:16 pm wrote: setting a value for the number, instead of 0
How are you setting the value of 0? Can you use the same technique to set another value? i dont understand what you mean .. ![]() the number increases from 0 when it enters the loop. |
Author: | Tony [ Thu Mar 03, 2011 6:34 pm ] |
Post subject: | RE:help - listing prime numbers |
Yes. Why does it start from 0? |
Author: | whoareyou [ Thu Mar 03, 2011 6:38 pm ] |
Post subject: | RE:help - listing prime numbers |
ok, so it should start from 1, because 1 isnt prime, and then when it enters the loop, it will be 2, and that will be a prime ... ? |
Author: | ihsh [ Thu Mar 03, 2011 11:51 pm ] |
Post subject: | RE:help - listing prime numbers |
You need to add more to the code. Say your program is trying to find whether the number n is a prime. None of the numbers from 2 to n-1 can be a factor of n if n is a prime number. However, according to your program, n is a prime number as long as there is a number between 2 and n-1 that is not a factor of n. You need to go through all the numbers if the for loop and check that none of them is a factor of n before you can conclude that n is prime. Hope that helped. |
Author: | mirhagk [ Fri Mar 04, 2011 2:45 pm ] |
Post subject: | RE:help - listing prime numbers |
Ihsh has it right, basically have a variable that says whether the number is prime or not. Have it start true, and anytime a number is a factor of it, set it to false. Otherwise the number will be outputted if any of the numbers from 2..n are not a factor of it. |
Author: | whoareyou [ Fri Mar 04, 2011 3:27 pm ] |
Post subject: | RE:help - listing prime numbers |
THANKS GUYS! i got it! |
Author: | mirhagk [ Sun Mar 06, 2011 4:21 pm ] |
Post subject: | RE:help - listing prime numbers |
glad to be of help |