Has anyone used this? I've been trying Project Euler challenges in it, but I hit a wall. For a bunch of problems, I need to add up the digits of a number, but I don't know how to go about doing this. Any ideas?
I usually use this code for getting the digit:
[cpp]int getDigit(int number, unsigned int index, unsigned int base = 10)
{
return (number / (unsigned int)pow((float)base,(float)index)) % base;
}[/cpp]
It C++, but maybe it could be helpful. To get the number of digits I do this:
[cpp]
for(unsigned int i = 0; i != (unsigned int)log10((float) num)+1; i++)
{
total += getDigit(num,i);
}
[/cpp]
That usually does the trick, It only works with integers though.
Sorry, you need to Log In to post a reply to this thread.