Use some Serial prints to track the values of the variables while the function runs. You may be able to see what is happening.
I printed the "num" variable and discovered it is NOT the integer I fed it with! It is some negative integer.
Must be some problem with my typecasting. I feed the function with a 60-digit string which I typecast to integer like this:
mystring is: 153934992015393499801539350040153934788014420636401539350040
That behavior is very strange, toInt() had never failed me before.
Any ideas how to overcome this issue?