Posted: 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)
Turing:
var num :int:=0 loop
num +=1 for i:2..num-1 if(num mod i) > 0then put num, " prime" exit endif endfor endloop
Please specify what version of Turing you are using
4.1.1
Sponsor Sponsor
Tony
Posted: 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?
Posted: 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 ... ?
ihsh
Posted: 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.
mirhagk
Posted: 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.
whoareyou
Posted: Fri Mar 04, 2011 3:27 pm Post subject: RE:help - listing prime numbers
THANKS GUYS! i got it!
Sponsor Sponsor
mirhagk
Posted: Sun Mar 06, 2011 4:21 pm Post subject: RE:help - listing prime numbers