Quote:
Quote:
Actually, I modified your code a bit for my program. Because the
exponent is incremented before the first test, I initially want
the largest integer that returns a string less than 101 digits
long.
I had checked my code fragment against yours
before I posted it and the results were the same.
Okay, and I noticed that my previous code could be improved, thus
the slight modifications.
Quote:
Quote:
The smallest 101-digit number is 10^100, so I want the largest
integer less than n such that 2^n=10^100.
This statement from your program comment is slightly off: You will
not find an integer with the property
2n=10100, this should be an
inequality. That's the reason why the code uses the FLOOR (or in
my version CEIL) function to round the logarithm to the
neighboring integer.
Hmm.... I don't think that my comment implies that n would be an
integer, unless you take it that using "n" instead of, for
example, "x" implies an integer. I was very sleepy when I wrote
the comments, and overlooked that. In fact, I very carefully wrote
it that way to slightly emphasize the fact that 2^n=10^100 is not
an integer. If n is taken to be integer, then clearly 2^n=10^100
doesn't have any solution, but if we allow n to be a "value" (in
this case an irrational number), then it does have a solution,
which can be approximated as a "real number" (actually, rational
number in decimal notation) on the calculator, which can be
rounded to an integer. For the next revision, I've revised the
comment to "The smallest 101-digit number is 10^100, so I want the
largest integer less than the (non-integer) value of x such that
2^x=10^100." Does that completely clarify what I meant to say?
Quote:
There is another issue with your code: As you stated, 10^100 is a
number with 101 digits, a one and 100 zeroes. By rounding down its
base two logarithm we get an exponent of 332 and a resulting power
of two ~= 8.7E99. This is the largest power of two with less then
101 digits and therefore the correct starting point for the search
loop. My original code (and yours too, see above) resulted in an
exponent of 329 and a power of two ~= 1.09E99. This is the
smallest power of two with 100 decimal digits and therefore
slightly less then the "best" lower limit for the loop. Not much
of an issue, I think.
Quite so. I think that my previous code started checking with the
next integer higher than the integer exponent which resulted in a
lowest power of 2 with
less than 101 digits (in case
the doubling resulted in an integer
with more than 101 digits). I realized that it would be better to
start checking with the exponent that would result in the lowest
power of 2 with
at least 101 digits, thus the change.
As you write, not much of an issue, but it does save three
iterations of a loop.
Quote:
BTW, LOG(1.E100) is a trivial operation and can be further
"optimized", try it on your own calc.
Yes, I was well aware of that, but was thinking that for someone
trying to follow the program code, "1.E100 LOG" might be a little
easier than simply "100.", and since it's outside of any loop, it
makes a relatively tiny difference in the execution time. But for
the next revision, I've changed the code (and comments) to simply
use "100.".
Another potential optimization that I've thought of was to start
checking for digit pairs at 99 and work my way down to 00. I
didn't have any good reason for expecting that this
would be any faster, but it seemed to me that it
might be. But, experimentally, it turns out that that
seems to be slightly slower instead of faster, so I've stuck with
starting at 00 and working my way up to 99.
I'm surprised that I missed a more important optimization, since
I've often used it to shave a little off of the execution time
with no change in the program size. "DUP +" accomplishes the same
as "2 *", and is faster. Usually not much of an issue, but within
a loop, particularly with large "exact integers", a worthwhile
optimization.
The latest revisions get the execution time down to about 90.2
seconds on my 49g+, and about 201 seconds on my 49G.
The source code for this revision can be found
here.
Regards,
James
Edited: 11 Oct 2006, 6:10 a.m. after one or more responses were posted