Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 help - listing prime numbers
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
whoareyou




PostPosted: 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) > 0 then
     put num, " prime"
     exit
     end if
end for
end loop




Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
whoareyou




PostPosted: 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 .. Shocked
the number increases from 0 when it enters the loop.
Tony




PostPosted: Thu Mar 03, 2011 6:34 pm   Post subject: RE:help - listing prime numbers

Yes. Why does it start from 0?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
whoareyou




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: Fri Mar 04, 2011 3:27 pm   Post subject: RE:help - listing prime numbers

THANKS GUYS! i got it!
Sponsor
Sponsor
Sponsor
sponsor
mirhagk




PostPosted: Sun Mar 06, 2011 4:21 pm   Post subject: RE:help - listing prime numbers

glad to be of help
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: