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

Username:   Password: 
 RegisterRegister   
 Outputting Prime Numbers
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
varman




PostPosted: Wed Jan 25, 2012 7:47 pm   Post subject: Outputting Prime Numbers

What is it you are trying to achieve?



What is the problem you are having?
<Well I just can't think of how to begin>


Describe what you have tried to solve this problem
<Tried thinking, but couldn't come up with any code>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


<Add your code here>



Please specify what version of Turing you are using
<4.1>
Sponsor
Sponsor
Sponsor
sponsor
varman




PostPosted: Wed Jan 25, 2012 7:49 pm   Post subject: RE:Outputting Prime Numbers

---WHAT I AM TRYING TO ACHIEVE---

I am trying to write a program to output all the prime numbers from 1 to 50.
Tony




PostPosted: Wed Jan 25, 2012 8:32 pm   Post subject: RE:Outputting Prime Numbers

The first step would be to figure out if any particular number is prime or not.

Is 4 prime?
How did you know?
Is 5 prime?
How did you know?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
varman




PostPosted: Wed Jan 25, 2012 8:49 pm   Post subject: RE:Outputting Prime Numbers

Like what I am thinking is to set up all the variables and use a IF condition. If you divide the number by itself, it should give you 1. Could you PLEASE help me. Like with a bit of code?

Thanks
ihsh




PostPosted: Wed Jan 25, 2012 10:18 pm   Post subject: Re: Outputting Prime Numbers

Just in case that you didn't know, the operator mod can give you the remainder when you divide two numbers. For example, (9 mod 5) equals to 4, and (4 mod 2) equals to 0.

Using this operator and a for loop, you can check if a number has any divisor other than 1 and itself, and thereby determine whether it is a prime or not.
Tony




PostPosted: Wed Jan 25, 2012 10:25 pm   Post subject: Re: RE:Outputting Prime Numbers

varman @ Wed Jan 25, 2012 8:49 pm wrote:
If you divide the number by itself, it should give you 1.

Is there any whole number where this is not the case?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Dreadnought




PostPosted: Thu Jan 26, 2012 12:26 am   Post subject: Re: Outputting Prime Numbers

Tony wrote:

varman @ Wed Jan 25, 2012 8:49 pm wrote:
If you divide the number by itself, it should give you 1.

Is there any whole number where this is not the case?

Well ... Zero. (depending on your definition of whole number)
But since all primes are greater than one this wont matter.

As for the topic at hand. You could read up on the Sieve of Eratosthenes.
Wikipedia even has a nice visual to demonstrate. Smile
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  [ 7 Posts ]
Jump to:   


Style:  
Search: