
-----------------------------------
varman
Wed Jan 25, 2012 7:47 pm

Outputting Prime Numbers
-----------------------------------
What is it you are trying to achieve?



What is the problem you are having?



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)








Please specify what version of Turing you are using


-----------------------------------
varman
Wed Jan 25, 2012 7:49 pm

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
Wed Jan 25, 2012 8:32 pm

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?

-----------------------------------
varman
Wed Jan 25, 2012 8:49 pm

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
Wed Jan 25, 2012 10:18 pm

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
Wed Jan 25, 2012 10:25 pm

Re: RE:Outputting Prime Numbers
-----------------------------------
If you divide the number by itself, it should give you 1.
Is there any whole number where this is not the case?

-----------------------------------
Dreadnought
Thu Jan 26, 2012 12:26 am

Re: Outputting Prime Numbers
-----------------------------------

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 [url=http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes]Sieve of Eratosthenes.
Wikipedia even has a nice visual to demonstrate. :)
