Sum digits of an integer: Difference between revisions

Content added Content deleted
(Python 3, simplify)
Line 3,719: Line 3,719:
return sum
return sum
</syntaxhighlight>
</syntaxhighlight>

=={{header|RPL}}==
RPL can natively handle numbers in bases 2,8,10 or 16, but displays them according to the current base mode. For example, when you type #256d, it will be immediately turned into #100h if HEX mode is active. As there is no way to force the base mode to the base used for input, switching to string handling looks like a reasonable approach for code clarity and size. A side effect is that it can proceed with numbers in any base between 2 and 16.
{{works with|Halcyon Calc|4.2.7}}
≪ →STR 0
1 3 PICK SIZE FOR j
"0123456789ABCDEF" 3 PICK j DUP SUB
POS DUP 1 - 0 IFTE +
NEXT
SWAP DROP
'DIGITS' STO

1 DIGITS
1234 DIGITS
#FEh DIGITS
#F0Eh DIGITS
{{out}}
<pre>
4:1
3:10
2:29
1:29
</pre>


=={{header|Ruby}}==
=={{header|Ruby}}==