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...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.Well, it also doesn't mean that it does exist. ;-) I doubt that it can really be reduced by one step. Franz |
|||
03-26-2016, 06:22 PM
Post: #22
|
|||
|
|||
RE: Sum of Digits.
Do you use EXPT, INC x in your solution ?
Gérard. |
|||
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 |
|||
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. |
|||
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. |
|||
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. 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 |
|||
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. |
|||
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:
It run. Have a nice day. Gérard. |
|||
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 ...actually calculate \(\frac{x}{10^{log x - 9}} = \frac{x}{x \cdot 10^{-9}} = 10^{9}\) And 109 mod 9 = 1. Dieter |
|||
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. |
|||
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. |
|||
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: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. |
|||
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-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 |
|||
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...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 OK, that's eight steps. Dieter |
|||
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. |
|||
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' 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 |
|||
03-30-2016, 07:54 PM
Post: #38
|
|||
|
|||
RE: Sum of Digits. | |||
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:
Thanks ! (And apologise). Gérard. |
|||
03-31-2016, 12:50 PM
Post: #40
|
|||
|
|||
RE: Sum of Digits. | |||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)