Computer Science Canada

Outputting Prime Numbers

Author:  varman [ Wed Jan 25, 2012 7:47 pm ]
Post subject:  Outputting Prime Numbers

What is it you are trying to achieve?
<I need to write a program to output all the prime numbers from 1 to 50>


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>

Author:  varman [ 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.

Author:  Tony [ 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?

Author:  varman [ 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

Author:  ihsh [ 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.

Author:  Tony [ 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?

Author:  Dreadnought [ 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


: