Re: Powers of 5





Josh wrote:
Hi, I'm an undergrad studying computer science. I discovered, while
studying for a computer science final, that I still haven't come up
with an answer to a question that was offered as extra credit on our
first quiz in the course. We were given the following Pascal code.

function power(n: integer): integer;
begin
if n <= 1 then
return (5)
else
return (5 * power(n-1))
end; {power}

We were then asked: "Can you modify the function in such a way that for
arguments of the form 2^k it computes the result with just k+1 calls?"
I'm not especially adept with math, and I have no idea how to approach
the question. Please point me in the right direction; I would really
like to understand this.

Powers of 5 of the form 2^k (i.e. 5^(2^k)) can be computed quickly
by successive squaring. For example,

5^16 = (5^8)^2

5^8 = (5^4)^2

5^4 = (5^2)^2

5^2 = (5^1)^2

This should give you the modified algorithm almost immediately.
If not, since you indicated this isn't homework just ask for
clarification.


Regards,

Rick

BTW, the argument doesn't have to be a power of 2 for a modification
to work in time less than a multiple of log n. It's a well-known
way of speeding up exponentiation.

Also, I'm sort of curious what school is still using Pascal as an
introductory language. Not that I'm complaining--I think Pascal
could still be a righteous choice for an intro vehicle; I'd just
like to know what school still has the stones to resist jumping
on the C#/C++/C/Java bandwagons.

.



Relevant Pages

  • Re: Marching to war
    ... If I were going to call you a liar, ... power. ... was in school and more or less drifted away over the years. ... have enough "drinking buddies" to match that %30 number. ...
    (rec.martial-arts)
  • Re: Exam grades and stuff (OT?)
    ... Sorry - the official `qualified teacher status' registration thingy ... went to the other school in the area when I was at school! ... Maybe it's because I don't understand bikes and I've never been on one, ... More power, yes; but not as much `more power' as you'd think because ...
    (uk.people.support.depression)
  • Re: Sleight of Hand
    ... due mainly to maternal malnutrition. ... The absence of basic health education is leading to inappropriate infant and child care and breastfeeding practices. ... The functional capacity of the health care system has degraded further by shortages of water and power supply, lack of transportation and the collapse of the telecommunications system. ... According to a field survey conducted in 1993, as quoted by UNESCO, in Central and Southern governorates 83% of school buildings needed rehabilitation, with 8.613 out of 10.334 schools having suffered serious damages. ...
    (rec.outdoors.rv-travel)
  • Interface model
    ... That's one of the main functions of school. ... I have long believed that Linux has the ... Then take a look at RoboSapiens. ... the power supplies using Lithium ...
    (Fedora)
  • Re: Exam grades and stuff (OT?)
    ... Sorry - the official `qualified teacher status' registration ... that went to the other school in the area when I was at school! ... it did so by buying a bike and hiring a rider and having ... More power, yes; but not as much `more power' as you'd think because ...
    (uk.people.support.depression)