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

Username:   Password: 
 RegisterRegister   
 Playing with Prime Numbers [Help!]
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
-JP-




PostPosted: Wed Apr 13, 2005 6:06 pm   Post subject: Playing with Prime Numbers [Help!]

Well, it's been a while I came here due to some packed up projects/assignments, but yea, I need help on this...T_T

Quote:

Write a program to display all the prime numbers from 1 to 50.


Now, of course using the put function and just putting all the prime numbers by yourself isn't fun in Turing, right? This program exercise was one in my textbook in the If Statements and Case Structure chapters, so it maybe need if statements.

Of course, we know that prime numbers have only 2 factors or less. I have understand to the part where it goes like this:


code:
var num: int:= 2 % Any value is fine
var count: int:= 0
for counter: 1..num
if num mod counter = 0 then
count:= count+1
end if
end for
if count <= 2 then
put "This integer is a prime number."
else
put "This integer is not a prime number."
end if


As you can see, I used the mod function to determine whether if it is a prime number of not. By using mod and dividing it to any number, that number will be a factor of the number you want to determine if it is a prime number if the result is 0. Of course, this only works for a specific integer given by the user, rather than making a list of prime numbers between numbers.

I can use the counted loop and make it from 1-50 and then put another counted loop inside it and determining it, but for some reason, when putting end for to end the counted loops, the loops doesn't go in the order I wanted to and thus, the calculation either displays only one number (1), or numbers beginning from 1, increasing to 50 and displays every number four times! O.o;

Even my teacher can't figure it out. Probably she's a basic Turing teacher.

Thanks.

~JP~
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Apr 13, 2005 6:22 pm   Post subject: (No subject)

mmm... primes
-JP-




PostPosted: Wed Apr 13, 2005 6:49 pm   Post subject: (No subject)

O_O! Whoa. Ahaha...I think I posted something wrong, but that guide really helped me. Thanks~ ^^

EDIT: However, there is a part in that code I don't get.
code:
a += 1 %number to divide by


I mean, the += part I don't get since I don't think I learned that O.o; (even though it looks basic...)
Bacchus




PostPosted: Wed Apr 13, 2005 7:02 pm   Post subject: (No subject)

short form, a+=1 is the same as a:=a+1
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  [ 4 Posts ]
Jump to:   


Style:  
Search: