HP Forums

Full Version: Sum of Digits.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi all friends,
Playing with my WP34s, as each days, I was thinking that, in number theory,
often we need know what is the sum of digits presents in register X.
So, based on an pretty old routine from John Kennedy, I would like submit
you this little routine.
Be aware it is not good for "perfects numbers", I am testing another routine
for this special case.
But it is may be helpfull for someone. Here we go :
Code:

001 LBL'TOT'
002 LocR 001
003 STO .00
004 STO- .00
005 # 010
006 /
007 STO+ .00
008 IP
009 STO- .00
010 x[!=]0?
011 BACK 006
012 # 010
013 STO[times] .00
014 RCL .00
015 RTN
016 END

For example, if you type : 123 ==> 6
31416==>15

Hoping servicing you

STO[times] = STO*
(03-25-2016 11:16 AM)ggauny@live.fr Wrote: [ -> ]Playing with my WP34s, as each days, I was thinking that, in number theory,
often we need know what is the sum of digits presents in register X.
So, based on an pretty old routine from John Kennedy, I would like submit
you this little routine.

Here is one that uses only the stack:

Code:
001 LBL'SD'
002 RCL- X
003 RCL L   ' keep x and set y=0 ;-)
004 RCL X
005 # 010
006 RMDR
007 STO+ Z  ' once again a case where
008 -       ' a dedicated DIVMOD command
009 # 010   ' would be nice
010 IDIV
011 x≠0?
012 BACK 008
013 +       ' or DROP, if you prefer
014 END

But I see you had time to improve your programming skills on the 34s. This also shows up in the reverse digits program. ;-)

Dieter
Nice programs, here is a shorter version that keeps the stack unchanged excepted for X replaced by it's sum of digits.

Code:
001 LBL'SD'
002 LocR 001
003 SDR 001
004 STO+ .00
005 IP
006 STO- .00
007 x[!=]0?
008 BACK 005
009 x<> .00
010 SDL 001
011 END

Notes: Local registers allocated by LocR are initialized to 0, so no need to clear them. Division and multiplication by a power of 10 can be replaced by the single step instructions SDR and SDL.
Hi,
Very thanks for your encouragements and compliments. And yours tricks !
(03-25-2016 05:46 PM)ggauny@live.fr Wrote: [ -> ]Very thanks for your encouragements and compliments. And yours tricks !

Yes, I also like Didier's solution. Did I already say that I'm a big fan of SDL and SDR?
But now for the next challenge – the sum of digits of the sum of digits:

12345 => 15 => 6

Any solution less than four steps (!) (w/o LBL and END) is accepted. Big Grin

SCNR,
Dieter
Well,
I am going to reflexion this night (I bad sleep), but in less 4 steps !
It seem very hard. I will find.

What is the meaning of : SCNR, I dont' see on "urban speaking".

Good night and thanks for the challenge.

It is really a pleasure to learn with all of you greats programmers !
May be, if register X is greather then 9, XEQ the routine again ?

#009
X<Y ?
XEQ 'SD'
END

Am-I right ?
(03-25-2016 08:26 PM)ggauny@live.fr Wrote: [ -> ]May be, if register X is greather then 9, XEQ the routine again ?

#009
X<Y ?
XEQ 'SD'
END

Am-I right ?

"SD" also works for x≤9.
But your proposed code does not work as it returns SD(9) = 9 for any x>9, and 9 for the rest. In other words, it will always return ...9.

OK, you could even do it in two lines:
XEQ SD
XEQ SD

But that's not the real solution as it requires an external program with a significant number of steps.
Actually the solution is much, much simpler, and of course it does not require calling an external program. ;-)

Dieter
You are diabolic and I am on the grill, but I will find, you will see !

Regards.
FP
SDL 001
+


?
(03-25-2016 07:44 PM)ggauny@live.fr Wrote: [ -> ]What is the meaning of : SCNR, I dont' see on "urban speaking".

see here. :)
(03-25-2016 08:46 PM)ggauny@live.fr Wrote: [ -> ]FP
SDL 001
+

?!?
How is this supposed to work?

Dieter
No, not good. Because if the result is for example 156, not work.
I need to more reflexion.
(03-25-2016 08:49 PM)ggauny@live.fr Wrote: [ -> ]No, not good. Because if the result is for example 156, not work.

It won't work for any number as the initial FP yields 0 for any integer... #-)

By the way, in this case the program should return 156 => 12 => 3.

Dieter
hi,
Ich gebe meine Zunge , um die Katze !
I give my tongue to the cat !
Je donne ma langue au chat !

I have really no solution for this challenge.

Good Easter for all.
Hi,
Thank you Massimo !
(03-25-2016 06:34 PM)Dieter Wrote: [ -> ]But now for the next challenge – the sum of digits of the sum of digits:

12345 => 15 => 6

Any solution less than four steps (!) (w/o LBL and END) is accepted. Big Grin

Well, not less than 4, but exactly 4 steps (I guess this is what you meant?):
Code:

DEC X
9
RMDR
INC X

Edit: replaced MOD by RMDR, so it also works for number 0.

Franz
(03-25-2016 08:59 PM)Dieter Wrote: [ -> ]
(03-25-2016 08:49 PM)ggauny@live.fr Wrote: [ -> ]No, not good. Because if the result is for example 156, not work.

It won't work for any number as the initial FP yields 0 for any integer... #-)

By the way, in this case the program should return 156 => 12 => 3.

Dieter

Dieter said it is not good, 156 give 3.

But may be you are right, I dont' know.
I think it is very difficult to answer for me.
(03-26-2016 01:02 PM)fhub Wrote: [ -> ]Well, not less than 4, but exactly 4 steps (I guess this is what you meant?):

Usually I mean what I say. ;-)
Four steps is easy – one example is your solution, another one is...

Code:
#009
RMDR
x=0?
X<> L

Well, at least for x>0. ;-)

So four steps is trivial. For less it takes some real art of programming.
No, I do not have a three-step-solution either. Which does not mean it doesn't exist. ;-)

Dieter
(03-26-2016 01:17 PM)ggauny@live.fr Wrote: [ -> ]But may be you are right, I dont' know.
I think it is very difficult to answer for me.

The solution is quite easy. Take a look here (Français) or here (English) or here (Deutsch).

That's why checking whether a number is divisible by 3 is so easy: just add its digits and check if that sum can be divided by 3. The sum is the remainder of a division by 9. So if this remainder is 0, 3 or 6 the number can also be divided by 3.

Dieter
Pages: 1 2
Reference URL's