title: Sum of primes in a number - Lua
body: <p>I'm trying to calculate the sum of the prime numbers in a given number. For instance, for the number 123456, the result will be 10 because 2+3+5 = 10.</p>
  
  <p>I tried to write a code that does that in Lua but I had some issues.</p>
  
  <p>First, here is the code:</p>
  
  <pre><code>    function isPrime(num)
  if(num == 1 or (num ~= 2 and num%2 == 0)) then
    return false
  end
  for i=3, math.sqrt(num), 2 do
    if(num%i == 0) then
      return false
    end
  end
  return true
  end
  
  function sumOfPrimes(num)
  local sum = 0
  for digit in string.gmatch(num,"%d") do
    local prime = isPrime(digit)
    if(isPrime(digit)) then
      sum = sum + digit
    end
    print(digit)
  end
  return sum
  end
  
  function main()
  print(sumOfPrimes(123456))
  end
  main()
  </code></pre>
  
  <p>It returnes 9 instead of 10. Another thing I've noticed is it adds 1 also to sum, but 1 isn't a prime. What's the problem here?</p>