Post Reply 
digits in x
11-22-2015, 04:29 PM
Post: #1
digits in x
hi,
In many programs, I need to know how many *digits* are in reg x.
For instance : 88209 are 5 digits, 81 are 2 and 5 are only one digit.

Me I do like this (for ex 88209) :

g LG give me 4,9455....
f IP give me 4
and 1 + give me the number of the digits of the reg x.

I have readed all manuals that people said me to read, but not a such function is. I think it would be good to put it in a future firmware, may be ?

Regards

dWink.

Gérard.
Find all posts by this user
Quote this message in a reply
11-22-2015, 04:58 PM
Post: #2
RE: digits in x
On the WP 34s you can do: g LG CEIL (in h X.FCN)
Find all posts by this user
Quote this message in a reply
11-22-2015, 05:22 PM
Post: #3
RE: digits in x
(11-22-2015 04:58 PM)Didier Lachieze Wrote:  On the WP 34s you can do: g LG CEIL (in h X.FCN)

Yes it is very tricky, thre steps economised !

Thank you

Gérard.
Find all posts by this user
Quote this message in a reply
11-22-2015, 05:40 PM
Post: #4
RE: digits in x
(11-22-2015 04:58 PM)Didier Lachieze Wrote:  On the WP 34s you can do: g LG CEIL (in h X.FCN)
Not correct for powers of 10: 10, 100, 1000 ...

Franz
Visit this user's website Find all posts by this user
Quote this message in a reply
11-22-2015, 05:43 PM
Post: #5
RE: digits in x
(11-22-2015 05:40 PM)fhub Wrote:  
(11-22-2015 04:58 PM)Didier Lachieze Wrote:  On the WP 34s you can do: g LG CEIL (in h X.FCN)
Not correct for powers of 10: 10, 100, 1000 ...
Good catch! You can still do g LG IP INC X

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
Find all posts by this user
Quote this message in a reply
11-22-2015, 05:46 PM (This post was last modified: 11-22-2015 05:51 PM by Dieter.)
Post: #6
RE: digits in x
(11-22-2015 04:58 PM)Didier Lachieze Wrote:  On the WP 34s you can do: g LG CEIL (in h X.FCN)

Sure you can do this – but you'll get the wrong result for all integer powers of 10. #-)
10000 LG gives 4, and CEIL(4) is ....4 again. Remember: CEIL is not just IP+1.

But there is another way that does not require logs at all: EXPT. The number of digits (again, for x≥1) is simply EXPT+1. So EXPT  INC X does it. Or SDL 1  EXPT.

EXPT also is faster than methods with logs. And, more important, there are no problems with cases where roundoff errors may spoil the result. Try 9,999999999999999 in SP mode and LG returns a plain 1 (which is correctly rounded up) so that even the 1+IP(LG x) method will fail.

So EXPT is the way to go. That's why this command exists. As well as its companion MANT. ;-)

Dieter
Find all posts by this user
Quote this message in a reply
11-22-2015, 05:49 PM
Post: #7
RE: digits in x
(11-22-2015 05:43 PM)Marcus von Cube Wrote:  
(11-22-2015 05:40 PM)fhub Wrote:  Not correct for powers of 10: 10, 100, 1000 ...
Good catch! You can still do g LG IP INC X

...and get errors due to roundoff errors for x very close to integer powers of 10.
The 34s has a better option: EXPT, cf. my respective post which I wrote before Franz and you responded. #-)

Dieter
Find all posts by this user
Quote this message in a reply
11-22-2015, 05:51 PM
Post: #8
RE: digits in x
(11-22-2015 05:49 PM)Dieter Wrote:  The 34s has a better option: EXPT, cf. my respective post which I wrote before Franz and you responded. #-)

I just read your post and must admit that I could have known...

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
Find all posts by this user
Quote this message in a reply
11-22-2015, 05:56 PM
Post: #9
RE: digits in x
(11-22-2015 05:43 PM)Marcus von Cube Wrote:  
(11-22-2015 05:40 PM)fhub Wrote:  Not correct for powers of 10: 10, 100, 1000 ...
Good catch! You can still do g LG IP INC X

Yes, I overlooked the powers of 10...

Here is another solution in two steps: EXPT INC X
Find all posts by this user
Quote this message in a reply
11-22-2015, 06:33 PM
Post: #10
RE: digits in x
(11-22-2015 05:56 PM)Didier Lachieze Wrote:  Here is another solution in two steps: EXPT INC X

I adopte this, economy of one step, your are cunning like a Sioux, we say in french !

I was not thinking in 10 powered, because in Kaprekar number it is not possible (I think).

My solution was walking with 10 powered, but one step is one step. Once upon a time, when machine has no mutch memory, we search to short routine.

Gérard.
Find all posts by this user
Quote this message in a reply
11-23-2015, 11:58 AM (This post was last modified: 11-23-2015 11:59 AM by fhub.)
Post: #11
RE: digits in x
(11-22-2015 06:33 PM)ggauny@live.fr Wrote:  I was not thinking in 10 powered, because in Kaprekar number it is not possible (I think).
You're right, powers of 10 are no Kaprekar numbers by definition.

BTW, here's my final version for Kaprekar numbers - different to my first version that I've sent you via PM (with 22 steps, optimized for size), this version is MUCH faster, but has 37 steps (now also showing the first number 1, without 1 you could even save 3 more steps 0004-0006).
Code:

/*
  Next Kaprekar number

  Input:  X:n (start number: 0<=n<999999)
  Output: X:k (next Karprekar number k>n)
          R/S --> next number ...
*/

0001 ****LBL'KAP'
0002 LocR 003
0003 STO .00
0004 INC X
0005 x=1?
0006 RTN
0007 **LBL 00
0008 INC .00
0009 RCL .00
0010 # 009
0011 MOD
0012 x[<=]1?
0013 SKIP 002
0014 RCL- L
0015 STO- .00
0016 RCL .00
0017 x[^2]
0018 STO .02
0019 EXPT
0020 10[^x]
0021 SDL 001
0022 STO .01
0023 **LBL 01
0024 [cmplx]RCL .01
0025 SDR 001
0026 STO .01
0027 IDIV
0028 x[>=]? .00
0029 GTO 00
0030 [cmplx]RCL .01
0031 MOD
0032 x=0?
0033 GTO 00
0034 +
0035 x[!=]? .00
0036 GTO 01
0037 END

Franz
Visit this user's website Find all posts by this user
Quote this message in a reply
11-23-2015, 04:48 PM
Post: #12
RE: digits in x
Hi,

I'm close to finish mine, I'll put after.

BTW congratulations, as usual !

Gérard.
Find all posts by this user
Quote this message in a reply
11-24-2015, 09:34 AM
Post: #13
RE: digits in x
(11-23-2015 11:58 AM)fhub Wrote:  BTW, here's my final version for Kaprekar numbers - different to my first version that I've sent you via PM (with 22 steps, optimized for size), this version is MUCH faster, but has 37 steps (now also showing the first number 1, without 1 you could even save 3 more steps 0004-0006).
Franz

Hi,
Very impressive celerity even for numbers up after 999.999 !
I love your fashion to do because if a number is Kaprekar you stop and display it, and if it is not one you display the follow that is.

Well done, mine say only "yes" or "no". And many steps for this ! I have to master flowcharts and greats functions of our WP34s.

"Damned !" you win again (LOL) !

Your fully.

Gérard.
Find all posts by this user
Quote this message in a reply
11-24-2015, 11:05 AM
Post: #14
RE: digits in x
I was telling : on the emulator, of course a bit slowly on the truth WP34s,
but it retire nothing of what I said.
:-)

Gérard.
Find all posts by this user
Quote this message in a reply
11-24-2015, 12:07 PM (This post was last modified: 11-24-2015 12:09 PM by fhub.)
Post: #15
RE: digits in x
(11-24-2015 11:05 AM)ggauny@live.fr Wrote:  .. of course a bit slowly on the truth WP34s, ..
Yes, a calculator is just not the right tool for running longer programs.
Although I've already reduced the tested numbers by 80% (because only numbers n with n MOD 9 = 0 or 1 can be Kaprekar numbers), but up to 1 million this still makes 200000 numbers which have to be checked - a hard task for a real calculator.

BTW, removing the 2 labels LBL 00/01 and replacing the GTO 00/01 by BACK xx, you can even save 2 more steps ... (just in case you're a fan of minimalistic programs) Wink

Franz
Visit this user's website Find all posts by this user
Quote this message in a reply
11-24-2015, 07:03 PM
Post: #16
RE: digits in x
(11-24-2015 12:07 PM)fhub Wrote:  
(11-24-2015 11:05 AM)ggauny@live.fr Wrote:  .. of course a bit slowly on the truth WP34s, ..
Yes, a calculator is just not the right tool for running longer programs.
Although I've already reduced the tested numbers by 80% (because only numbers n with n MOD 9 = 0 or 1 can be Kaprekar numbers), but up to 1 million this still makes 200000 numbers which have to be checked - a hard task for a real calculator.

BTW, removing the 2 labels LBL 00/01 and replacing the GTO 00/01 by BACK xx, you can even save 2 more steps ... (just in case you're a fan of minimalistic programs) Wink

Franz

Yes I understand now why you MOD by 9, I was not understood !
Yes too with my olds calculators, they say vintage now, as it is my way, I even today search for shorter routine, a old way for me.
In this moment I try to ported for WP34s a old program I love again to day, it is "Moon Roket Lander". With because of you and others I learned in good programmation, I think I bet.
Only miss me an instruction that is "PAUSE" in hp67, surely she exist in WP34s and I read IOP, at this moment,to catch she.
I use complex store, Locr and local flags, and I am sure in few days I will...
I hope. (I don't have said my last word, it is a sentence french, no know if have a sens in english)

Have a good evening, here it is GMT+1 = 20h02 at my watch relied atomic center
Frankfurt (Thank Dieter and Sylvain).

Gérard.
Find all posts by this user
Quote this message in a reply
11-24-2015, 07:39 PM
Post: #17
RE: digits in x
Franz,

I have finded, it is the same name "PSE" but in ticks, well, well I have what missed me. I know what tick is, I only multiplicate to have one second or many.

I think that "relied" is no english, I was think of "linked" yes ?

I have modified your program :
1/- 3 steps
2/- with back, it remember HP67 gto i, with i negative

Thank my freund.

Gérard.
Find all posts by this user
Quote this message in a reply
Post Reply 




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