(50G) Digits sum (50g)
04-12-2020, 08:05 PM (This post was last modified: 05-03-2020 07:37 PM by Gene.)
Post: #1
 Juan14 Junior Member Posts: 34 Joined: Jan 2014
(50G) Digits sum (50g)
The program below finds the sum of the digits of an integer, for example for 236067 the program returns 2+3+6+0+6+7 = 24.

«
0 SWAP
WHILE DUP
REPEAT
10 IDIV2 ROT + SWAP
END DROP
»

If you change the number 10 next the command IDIV2 to 8, you will get the sum of digits of the number in base 8, for example 236067 will return 20 and 236067 in base 8 is 751043, the sum of the digits is 20.

Returning to base 10, you apply the program again and again until you get to single digit number. For example applying successively the program to 7897869769, we get 76, 13 and 4.

I was trying factorials and notice that after 6!, if you reduce to a single digit, this is always 9. I know there are guys out there with better knowledge that can explain this.
04-13-2020, 09:45 AM
Post: #2
 grsbanks Senior Member Posts: 1,017 Joined: Jan 2017
RE: Digits sum (50g)
(04-12-2020 08:05 PM)Juan14 Wrote:  I was trying factorials and notice that after 6!, if you reduce to a single digit, this is always 9. I know there are guys out there with better knowledge that can explain this.

6! = 1 x 2 x 3 x 4 x 5 x 6 = 1 x 2 x 3 x 4 x 5 x 2 x 3

3 is a factor of 6! twice, which means that it's a multiple of 9, as is the factorial of any integer above 6.

The phenomenon you describe is a property of all multiples of 9.

There are only 10 types of people in this world. Those who understand binary and those who don't.
04-13-2020, 08:38 PM
Post: #3
 Nihotte(lma) Junior Member Posts: 41 Joined: Mar 2020
RE: Digits sum (50g)
(04-12-2020 08:05 PM)Juan14 Wrote:  The program below finds the sum of the digits of an integer, for example for 236067 the program returns 2+3+6+0+6+7 = 24.

«
0 SWAP
WHILE DUP
REPEAT
10 IDIV2 ROT + SWAP
END DROP
»

That's a pretty loop
Otherwise, no loop (lol) !

So, Juan14, I propose you this one !? (HP48 : no IDIV2 available)

«
10
→ n b
« n b MOD
IF n b ≥ THEN
n b / IP SUMAD +
END
»
»
04-13-2020, 11:01 PM
Post: #4
 Juan14 Junior Member Posts: 34 Joined: Jan 2014
RE: Digits sum (50g)
Thank you grsbanks and Nihotte(Ima) for your replays. The recursive program is very interesting, the 50g can handle huge integer numbers and a recursive program would take a lot of resources.
04-15-2020, 03:49 AM
Post: #5
 DavidM Senior Member Posts: 768 Joined: Dec 2013
RE: Digits sum (50g)
That's an interesting program to experiment with, Juan!

Same answer for base 10, but a completely different method:

Code:
\<<    R\->I \->STR    0 SWAP    WHILE       DUP SIZE    REPEAT       DUP TAIL SWAP HEAD       NUM 48. -       ROT +       SWAP    END    DROP    R\->I \>>

IDIV2 is very slow, and I usually go out of my way to avoid using it for anything repetitive (which ironically is where it would be most useful). A 2000-digit number passed to your original program takes about 240 seconds to complete on my 50g. That same number passed to the above program finished in about 32 seconds.

The ListExt library contains a couple commands (I→NL, LSUM) which make all of this much easier. In particular, I→NL creates a list whose elements are the individual digits of the original number. LSUM is a more forgiving version of ΣLIST (it also accepts lists with 0 or 1 element):
Code:
\<<    I\->NL    LSUM    R\->I \>>

It's even faster than the original program above -- a 2000-digit number is summed in about 3 seconds with this code.
04-15-2020, 10:21 PM (This post was last modified: 04-15-2020 10:22 PM by John Keith.)
Post: #6
 John Keith Senior Member Posts: 526 Joined: Dec 2013
RE: Digits sum (50g)
I was going to post about I->NL but you beat me to it.

Also I->BL for bases other than 10.

For integers up to 100 digits or so, I->NL LSUM is fastest. For numbers larger than 100 digits, this strange program is actually faster:

Code:
 \<<   \->STR 0 SWAP 1. 9.     FOR j j 48. + CHR "" SREPL j * ROT + SWAP     NEXT DROP \>>

SREPL is crazy fast, I have used it for lots of odd purposes like this.

Here is a similar program that returns a list of 10 numbers which are the counts of each digit from 1 to 9 in a large integer:

Code:
 \<<     \->STR 48. 57.     FOR j j CHR "" SREPL SWAP     NEXT DROP 10. \->LIST \>>

For integers less than 70 digits or so, I\->NL LSORT LRPCT is faster (and a lot shorter).
04-17-2020, 04:31 AM
Post: #7
 DavidM Senior Member Posts: 768 Joined: Dec 2013
RE: Digits sum (50g)
(04-15-2020 10:21 PM)John Keith Wrote:  ...For numbers larger than 100 digits, this strange program is actually faster:

Code:
 \<<   \->STR 0 SWAP 1. 9.     FOR j j 48. + CHR "" SREPL j * ROT + SWAP     NEXT DROP \>>

SREPL is crazy fast, I have used it for lots of odd purposes like this.

This is a great example of the power of SREPL. I never would have thought of using it for that purpose, but it works quite well as you have indicated. The others are similarly well-suited for the tasks you identified.

SREPL is almost entirely written in Saturn code, and it makes repeated use of MOVE instructions which execute at native ARM speed. So it really is a speed demon.

Thanks to Juan, grsbanks, Nihotte(lma), and John Keith for all the thought-provoking concepts!
04-17-2020, 10:42 AM
Post: #8
 Gerald H Senior Member Posts: 1,441 Joined: May 2014
RE: Digits sum (50g)
Very nice & fast programme, John Keith.

So fast that a sys version is not really necessary, but I couldn't resist:

Code:
Size: 69.5 CkSum: # 2F18h ::   CK1&Dispatch   # FF   ::     FPTR2 ^Z>S     ZEROSWAP     BINT10     ONE_DO     INDEX@     BINT48     #+     #>CHR     CHR>$NULL$     FPTR F 1A     COERCE     INDEX@     #*     ROT#+     SWAPLOOP     DROP     UNCOERCE   ; ;
 « Next Oldest | Next Newest »

User(s) browsing this thread: 1 Guest(s)