Re: Powers of 5



Josh wrote:
Rick Decker wrote:
....
Powers of 5 of the form 2^k (i.e. 5^(2^k)) can be computed quickly
by successive squaring. For example,
....
That's perfect. I wish I had thought of that. I rewrote the function
in Python for the sake of being concise, but now the function takes k
rather than 2^k as its argument. So I guess I haven't really answered
the question.

def power(k):
if (k <= 1):
return 5
else:
return power(k-1)**2
....
At any rate, I digress. I'm just missing how to get the function to
take 2^k as its argument. If you don't mind helping a little more, I
would be grateful.
....

You merely replaced "return (5 * power(n-1))" with
"return power(k-1)**2". Instead, make the function
split three ways -
if n <= 1 return 5,
else if n is even return the square of something,
else return power(n-1)*5.

[Helpful example: x^14 = (x^7)^2.]
-jiw
.



Relevant Pages

  • Re: which feature of python do you like most?
    ... I've never used Perl, but I know other c-like laguages, and I can tell ... you what I like about python: ... - It is concise, ...
    (comp.lang.python)
  • Re: Is it possible to set the date/time of a directory in windows with Python? If so how?
    ... Just for the sake of completeness ... ... correct dates but the dirs didn't as doing the extraction with python ... command line to do the extraction but I lost interest at that point. ...
    (comp.lang.python)
  • Re: pack a three byte int
    ... Struct module has codes B, H, I, Q for unsigned integers of ... since 2.5, but that would be neither concise, nor since 2.3. ... A failure on my part to think in Python? ... X12Inquiry, allocationLength) ???? ...
    (comp.lang.python)
  • Re: list index()
    ... Python 3. ... obsolete by "in", which is faster and more concise. ... Is there really some reason "key" IN dict can be implemented ...
    (comp.lang.python)
  • Py2exe Question - Deployment Advice
    ... I wrote my first Python script today and was very impressed at how ... concise the code was and development time. ...
    (comp.lang.python)

Loading