
-----------------------------------
Johnny19931993
Tue Feb 03, 2009 3:53 pm

Need help for outputing prime number
-----------------------------------
I'm trying to make a program that calculates all the prime numbers from 1 to 50
but i don't know why my program didn't output anything
can anyone help me?


const QUANTITY := 50
var number : array 1 .. QUANTITY of int
var counter := 0

for x : 1 .. QUANTITY
    number (x) := x
    for y : 1 .. x
        if number (x) mod y = 0 then
            counter += 1
        end if
    end for
    if counter = 2 then
        put number (x)
    end if
end for


-----------------------------------
cavetroll
Tue Feb 03, 2009 4:08 pm

Re: Need help for outputing prime number
-----------------------------------
Your problem is you need to reset the counter for every loop through. Try something like:


const QUANTITY := 50 
var number : array 1 .. QUANTITY of int 
var counter := 0 

for x : 1 .. QUANTITY 
    counter := 0
    number (x) := x 
    for y : 1 .. x 
        if number (x) mod y = 0 then 
            counter += 1 
        end if 
    end for 
    if counter = 2 then 
        put number (x) 
    end if 
end for 

-----------------------------------
DanielG
Tue Feb 03, 2009 5:33 pm

RE:Need help for outputing prime number
-----------------------------------
your mistake is going from 1 .. x, it should be 1.. x-1, also, make sure you don't check one, since it is NOT a prime but will be shown as one by the formula you have written

-----------------------------------
Johnny19931993
Tue Feb 03, 2009 10:03 pm

Re: RE:Need help for outputing prime number
-----------------------------------
your mistake is going from 1 .. x, it should be 1.. x-1, also, make sure you don't check one, since it is NOT a prime but will be shown as one by the formula you have written

even though my problem is already solved by the help of cavetroll, I still want to point out that the program will not output 1 whatsoever
because if i put 

for x : 1..1

the loop will only go once, therefore, it is impossible for the value of "counter" to be 2
so if "number" is equal to 1, the program will not output 1

-----------------------------------
andrew.
Wed Feb 04, 2009 5:23 pm

RE:Need help for outputing prime number
-----------------------------------
Why don't you make it like if the number is 1, then put 1, if it's anything else, then go into your for loop.
