Post Reply 
Sum of Digits.
03-26-2016, 06:17 PM
Post: #21
RE: Sum of Digits.
(03-26-2016 06:02 PM)Dieter Wrote:  Four steps is easy – one example is your solution, another one is...

Code:
#009
RMDR
x=0?
X<> L
Yes, I also found this solution, but it doesn't work for 0 (whereas the other one does).
Quote: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. ;-)
Well, it also doesn't mean that it does exist. ;-)

I doubt that it can really be reduced by one step.

Franz
Visit this user's website Find all posts by this user
Quote this message in a reply
03-26-2016, 06:22 PM
Post: #22
RE: Sum of Digits.
Do you use EXPT, INC x in your solution ?

Gérard.
Find all posts by this user
Quote this message in a reply
03-26-2016, 06:27 PM
Post: #23
RE: Sum of Digits.
(03-26-2016 06:22 PM)ggauny@live.fr Wrote:  Do you use EXPT, INC x in your solution ?

No, it's much easier than that.
Take a look at the links in post #20.

Dieter
Find all posts by this user
Quote this message in a reply
03-26-2016, 08:15 PM
Post: #24
RE: Sum of Digits.
Hi,
After seeying post #20 link in french, I am as before.
I think it is very simple solution but only for you.
Of course you have the solution of this challenge.
Me I will try again, with eyes I see how to do, but difficult to make the wp34s
see with eyes.

15 : 1 5 I add and it is 6.

In 3 steps ????

Gérard.
Find all posts by this user
Quote this message in a reply
03-26-2016, 08:18 PM
Post: #25
RE: Sum of Digits.
And if 1 5 6 it is 12

And etc !

In sudoku it is noted "diabolic" !

Gérard.
Find all posts by this user
Quote this message in a reply
03-26-2016, 09:06 PM
Post: #26
RE: Sum of Digits.
(03-26-2016 08:15 PM)ggauny@live.fr Wrote:  After seeying post #20 link in french, I am as before.
I think it is very simple solution but only for you.

The solution is simple because it is just x mod 9. ;-)
(Or 9 if this yields zero).

x=156: 1+5+6 = 12. Then 1+2 = 3
156 mod 9 = 3

x=12345: 1+2+3+4+5 = 15. Then 1+5 = 6
12345 mod 9 = 6

x=987654321: 9+8+7+6+5+4+3+2+1 = 45. Then 4+5 = 9
987654321 mod 9 = 0 => result is 9

Voilà – it's as simple as that.

(03-26-2016 08:15 PM)ggauny@live.fr Wrote:  15 : 1 5 I add and it is 6.

15 mod 9 = 6

(03-26-2016 08:15 PM)ggauny@live.fr Wrote:  In 3 steps ????

Well... OK, that's a real challenge. ;-)
But it can be done in four steps.

Franz used the more correct solution 1 + (x–1) mod 9 which can be coded in four steps as well. At least on the 34s.

Dieter
Find all posts by this user
Quote this message in a reply
03-27-2016, 07:10 AM
Post: #27
RE: Sum of Digits.
Well, well, well, now I understand ! Modulation ways is helpfull here.
Thank you and Franz.

Gérard.
Find all posts by this user
Quote this message in a reply
03-29-2016, 11:34 AM (This post was last modified: 03-30-2016 10:02 AM by ggauny@live.fr.)
Post: #28
RE: Sum of Digits.
Hi,
I think this little routine give the answer of the Dieter's challenge :

Code:

LBL'SDM' // M it is for Me
ENTER
LOG
IP
9
-
10^X
/
9
MOD  // or RMDR
X[=!]0 ?  // It is X different of 0
R/S
Drop
9
RTN

It run.

Have a nice day.

Gérard.
Find all posts by this user
Quote this message in a reply
03-29-2016, 07:36 PM
Post: #29
RE: Sum of Digits.
(03-29-2016 11:34 AM)ggauny@live.fr Wrote:  It run.

Gérard, have you really entered and run this program? I would be surprised if it returned anything else but 1 – for any input. ;-)

These steps...

Code:
ENTER
LOG
9
-
10^X
/

...actually calculate \(\frac{x}{10^{log x - 9}} = \frac{x}{x \cdot 10^{-9}} = 10^{9}\)
And 109 mod 9 = 1.

Dieter
Find all posts by this user
Quote this message in a reply
03-30-2016, 10:00 AM
Post: #30
RE: Sum of Digits.
Hi,

Well catch as say Marcus, Yes it run but I have omited step 04 it is IP.
I scuse me for my omit.

Thank you for remarq.

I promis you that it run on my hp41c and CX and WP34s.

Gérard.
Find all posts by this user
Quote this message in a reply
03-30-2016, 10:04 AM
Post: #31
RE: Sum of Digits.
I have modified my first post, then now it run the routine !

Thank's to Dieter !

Gérard.
Find all posts by this user
Quote this message in a reply
03-30-2016, 11:11 AM (This post was last modified: 03-30-2016 11:27 AM by BarryMead.)
Post: #32
RE: Sum of Digits.
(03-26-2016 01:02 PM)fhub Wrote:  
Code:

DEC X
9
RMDR
INC X

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

Franz
This version works great on POSITIVE values but for negative values it returns incorrect answers when (X MOD 9) is -8 or Zero. I haven't got a fix yet, but I thought you would like to know that it isn't quite right.
Find all posts by this user
Quote this message in a reply
03-30-2016, 11:24 AM (This post was last modified: 03-30-2016 11:46 AM by BarryMead.)
Post: #33
RE: Sum of Digits.
(03-26-2016 06:02 PM)Dieter Wrote:  Four steps is easy – one example is your solution, another one is...
Code:
#009
RMDR
x=0?
X<> L
In addition to handling Zero incorrectly, this version also has a problem when the value is negative if (X MOD 9 = 0) returns Positive 9 instead of -9 as it should.
Find all posts by this user
Quote this message in a reply
03-30-2016, 12:42 PM
Post: #34
RE: Sum of Digits.
(03-30-2016 10:00 AM)ggauny@live.fr Wrote:  Well catch as say Marcus, Yes it run but I have omited step 04 it is IP.

This way the entered number is filled up with zeroes until it has 10 digits.

123 => 1230000000
12345 => 1234500000
12345678 => 1234567800

Adding zeros will of course not change the digit sum.
But it does not make any sense either. So why do you do this?

Dieter
Find all posts by this user
Quote this message in a reply
03-30-2016, 12:46 PM (This post was last modified: 03-30-2016 12:57 PM by Dieter.)
Post: #35
RE: Sum of Digits.
(03-30-2016 11:24 AM)BarryMead Wrote:  
(03-26-2016 06:02 PM)Dieter Wrote:  Four steps is easy – one example is your solution, another one is...
Code:
#009
RMDR
x=0?
X<> L
In addition to handling Zero incorrectly, this version also has a problem when the value is negative if (X MOD 9 = 0) returns Positive 9 instead of -9 as it should.

That's why my post said "Well, at least for x>0".

I wonder how digit sums are defined for negative values. Would –12345 yield –6?
In this case a solution is easy:

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

OK, that's eight steps.

Dieter
Find all posts by this user
Quote this message in a reply
03-30-2016, 05:37 PM
Post: #36
RE: Sum of Digits.
On my hp 41 I have not 00000,

For instance 12345 is displayed 12345 and this give me 6, in FIX 0, or
6,00000 in FIX 5.

May be I have bad understood your question ?

Gérard.
Find all posts by this user
Quote this message in a reply
03-30-2016, 06:01 PM
Post: #37
RE: Sum of Digits.
(03-30-2016 05:37 PM)ggauny@live.fr Wrote:  May be I have bad understood your question ?

You said the corrected code starts with...

Code:
LBL'SDM'
ENTER
LOG
IP
9
-
10^X
/

This fills up any entered number with zeroes:
12345 ENTER LOG IP 9 – 10^x / yields 1234500000
123 ENTER LOG IP 9 – 10^x / yields 1230000000
etc.

This of course does not change the digit sum.

123 mod 9 = 6
1230000000 mod 9 = 6

1234 mod 9 = 1
1234000000 mod 9 = 1

So what is the reason behind this? As far as I can see the first six lines after the label are completely unnecessary. What am I missing here?

Dieter
Find all posts by this user
Quote this message in a reply
03-30-2016, 07:54 PM
Post: #38
RE: Sum of Digits.
(03-30-2016 12:46 PM)Dieter Wrote:  
Code:
SIGN
RCL L
ABS
#009
RMDR
x=0?
X<> L
x
This solution is very impressive. Since the SIGN function returns 3 values -1 for negative +1 for positive and Zero for Zero it also corrects the X=0 case in addition to fixing the negative value cases. Thanks
Find all posts by this user
Quote this message in a reply
03-31-2016, 07:33 AM (This post was last modified: 03-31-2016 07:49 AM by ggauny@live.fr.)
Post: #39
RE: Sum of Digits.
Dieter,

You are right of course, my routine was part of an old program from my HP41cx
and I have not see the problem,
Of course the 7 steps after the label are not necessary !

Code:

LBL'SDM'
9
MOD  // RMDR
X[=!]0 ?
R/S
DROP
9
RTN

Thanks ! (And apologise).

Gérard.
Find all posts by this user
Quote this message in a reply
03-31-2016, 12:50 PM
Post: #40
RE: Sum of Digits.
(03-31-2016 07:33 AM)ggauny@live.fr Wrote:  
Code:
LBL'SDM'
9
MOD  // RMDR
X[=!]0 ?
R/S
DROP
9
RTN

Now simply replace the last steps with X=0? X<> L and the code is shorter and has a common exit point.

Dieter
Find all posts by this user
Quote this message in a reply
Post Reply 




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