(50G) Digits sum (50g)

04122020, 08:05 PM
(This post was last modified: 05032020 07:37 PM by Gene.)
Post: #1




(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. 

04132020, 09:45 AM
Post: #2




RE: Digits sum (50g)
(04122020 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. 

04132020, 08:38 PM
Post: #3




RE: Digits sum (50g)
(04122020 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. That's a pretty loop Otherwise, no loop (lol) ! So, Juan14, I propose you this one !? (HP48 : no IDIV2 available) SUMAD #F010h 97.5 « 10 → n b « n b MOD IF n b ≥ THEN n b / IP SUMAD + END » » 

04132020, 11:01 PM
Post: #4




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.


04152020, 03:49 AM
Post: #5




RE: Digits sum (50g)
That's an interesting program to experiment with, Juan!
Same answer for base 10, but a completely different method: Code: \<< 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 2000digit 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: \<< It's even faster than the original program above  a 2000digit number is summed in about 3 seconds with this code. 

04152020, 10:21 PM
(This post was last modified: 04152020 10:22 PM by John Keith.)
Post: #6




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:
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:
For integers less than 70 digits or so, I\>NL LSORT LRPCT is faster (and a lot shorter). 

04172020, 04:31 AM
Post: #7




RE: Digits sum (50g)
(04152020 10:21 PM)John Keith Wrote: ...For numbers larger than 100 digits, this strange program is actually faster: 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 wellsuited 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 thoughtprovoking concepts! 

04172020, 10:42 AM
Post: #8




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 

« Next Oldest  Next Newest »

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