Sum of Digits.
03-25-2016, 11:16 AM
Post: #1
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
Sum of Digits.
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*

Gérard.
03-25-2016, 01:41 PM
Post: #2
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Sum of Digits.
(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
03-25-2016, 04:37 PM
Post: #3
 Didier Lachieze Senior Member Posts: 1,367 Joined: Dec 2013
RE: Sum of Digits.
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.
03-25-2016, 05:46 PM
Post: #4
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
RE: Sum of Digits.
Hi,
Very thanks for your encouragements and compliments. And yours tricks !

Gérard.
03-25-2016, 06:34 PM (This post was last modified: 03-25-2016 08:34 PM by Dieter.)
Post: #5
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Sum of Digits.
(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.

SCNR,
Dieter
03-25-2016, 07:44 PM
Post: #6
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
RE: Sum of Digits.
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 !

Gérard.
03-25-2016, 08:26 PM
Post: #7
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
RE: Sum of Digits.
May be, if register X is greather then 9, XEQ the routine again ?

#009
X<Y ?
XEQ 'SD'
END

Am-I right ?

Gérard.
03-25-2016, 08:33 PM (This post was last modified: 03-25-2016 08:37 PM by Dieter.)
Post: #8
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Sum of Digits.
(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
03-25-2016, 08:37 PM
Post: #9
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
RE: Sum of Digits.
You are diabolic and I am on the grill, but I will find, you will see !

Regards.

Gérard.
03-25-2016, 08:46 PM
Post: #10
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
RE: Sum of Digits.
FP
SDL 001
+

?

Gérard.
03-25-2016, 08:48 PM
Post: #11
 Massimo Gnerucci Senior Member Posts: 2,332 Joined: Dec 2013
RE: Sum of Digits.
(03-25-2016 07:44 PM)ggauny@live.fr Wrote:  What is the meaning of : SCNR, I dont' see on "urban speaking".

see here. :)

Greetings,
Massimo

-+×÷ ↔ left is right and right is wrong
03-25-2016, 08:48 PM
Post: #12
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Sum of Digits.
(03-25-2016 08:46 PM)ggauny@live.fr Wrote:  FP
SDL 001
+

?!?
How is this supposed to work?

Dieter
03-25-2016, 08:49 PM
Post: #13
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
RE: Sum of Digits.
No, not good. Because if the result is for example 156, not work.
I need to more reflexion.

Gérard.
03-25-2016, 08:59 PM (This post was last modified: 03-25-2016 09:00 PM by Dieter.)
Post: #14
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Sum of Digits.
(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
03-26-2016, 07:52 AM
Post: #15
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
RE: Sum of Digits.
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.

Gérard.
03-26-2016, 07:54 AM
Post: #16
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
RE: Sum of Digits.
Hi,
Thank you Massimo !

Gérard.
03-26-2016, 01:02 PM (This post was last modified: 03-26-2016 02:28 PM by fhub.)
Post: #17
 fhub Member Posts: 188 Joined: Dec 2013
RE: Sum of Digits.
(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.

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-26-2016, 01:17 PM
Post: #18
 ggauny@live.fr Senior Member Posts: 528 Joined: Nov 2014
RE: Sum of Digits.
(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.

Gérard.
03-26-2016, 06:02 PM (This post was last modified: 03-26-2016 06:16 PM by Dieter.)
Post: #19
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Sum of Digits.
(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, 06:16 PM (This post was last modified: 03-26-2016 06:28 PM by Dieter.)
Post: #20
 Dieter Senior Member Posts: 2,397 Joined: Dec 2013
RE: Sum of Digits.
(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
 « Next Oldest | Next Newest »

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