
-----------------------------------
whoareyou
Thu Mar 03, 2011 6:16 pm

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)



var num :int := 0
loop
num +=1
for i:2..num-1
     if (num mod i) > 0 then
     put num, " prime"
     exit
     end if
end for 
end loop




Please specify what version of Turing you are using
4.1.1

-----------------------------------
Tony
Thu Mar 03, 2011 6:19 pm

Re: help - listing prime numbers
-----------------------------------
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?

-----------------------------------
whoareyou
Thu Mar 03, 2011 6:27 pm

Re: help - listing prime numbers
-----------------------------------
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 ..  :shock: 
the number increases from 0 when it enters the loop.

-----------------------------------
Tony
Thu Mar 03, 2011 6:34 pm

RE:help - listing prime numbers
-----------------------------------
Yes. Why does it start from 0?

-----------------------------------
whoareyou
Thu Mar 03, 2011 6:38 pm

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
Thu Mar 03, 2011 11:51 pm

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
Fri Mar 04, 2011 2:45 pm

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
Fri Mar 04, 2011 3:27 pm

RE:help - listing prime numbers
-----------------------------------
THANKS GUYS! i got it!

-----------------------------------
mirhagk
Sun Mar 06, 2011 4:21 pm

RE:help - listing prime numbers
-----------------------------------
glad to be of help
