(50G) Digits sum (50g) +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (50G) Digits sum (50g) (/thread-14839.html) |
(50G) Digits sum (50g) - Juan14 - 04-12-2020 03:05 PM 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. RE: Digits sum (50g) - DavidM - 04-14-2020 10:49 PM 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 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: \<< It's even faster than the original program above -- a 2000-digit number is summed in about 3 seconds with this code. RE: Digits sum (50g) - John Keith - 04-15-2020 05:21 PM 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). RE: Digits sum (50g) - Gerald H - 04-17-2020 05:42 AM 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 RE: (50G) Digits sum (50g) - gor1060 - 12-11-2022 06:15 AM My asm-version of programm: " :: CODE C=DAT1 A ?C#0 A GOYES INTGR? LA 00201 *ERROR GOVLNG =Errjmp *INTGR? CD1EX A=DAT1 A D1=C R1=C A LC 02614 ?A=C A GOYES MAIN LA 00202 GOTO ERROR *MAIN SAVE C=R1 A D1=C C=DAT1 A D1=C D1=D1+5 A=DAT1 A LC 00006 ?A=C A GOYES ZERO A=A-5 A A=A-1 A A=A-1 A B=0 W C=0 W D1=D1+4 *Loop D1=D1+1 C=DAT1 1 B=B+C W A=A-1 A GONC Loop A=B W GOTO Skip *ZERO A=0 W *Skip GOSBVL HXDCW GOSBVL FLOAT SETHEX GOVLNG PUSH%LOOP ENDCODE SWAPDROP FPTR2 ^R>Z ; @" |