What error in while loop? moon turn on

Asked

Viewed 73 times

4

What’s the error in the loop? I wrote this code to print the sum of the first 10 prime numbers, I’ve reviewed the loops but I couldn’t find the error. It was to print the sum of the cousins until 29 but only printed 2. Vlwzao/

n=2
cont=0
soma=0


while n<=29 do
k=1
    while k<=n do
        if n%k==0 then
            cont=cont+1
        end
    k=k+1
    end
    if cont==2 then
        soma=soma+n
    else soma=soma
    end
n=n+1
end 
print(soma)
  • What’s the symptom that tells you there’s some mistake?

  • at the time of the print of the sum the value I printed is 2, it was to print the sum of the primes until 29.

  • Edit your question and put this information, please

  • 2+3+5+7+11+13+17+19+23+29=129

  • You missed zeroing the variable cont within the while

1 answer

4


You missed zeroing the variable cont within the while:

while n<=29 do
k=1
cont = 0
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.