Please Help Explain

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Wed Nov 5 23:20:02 EST 2003


On Thu, 06 Nov 2003 04:01:21 GMT, Jakle wrote:
> I was at a job interview one day and the owner of the start up company
> asked me if I'd rather make $1000 dollars a day, or start off with a
> penny a day and double the amount every day for 30 days.

A classic surprise in mathematics.  A doubling series gets large
unexpectedly quickly, until you've seen it happen a few times.

It's the subject of an ancient parable (of Arabic origin, I believe).  A
pauper manages to beat the king at chess, and the king condescendingly
offers the pauper a hearty meal as reward.  The pauper asks for a more
modest reward: he asks the king to place a single rice grain on the
first square of the board, two on the next, four on the next, eight on
the next, and so on for all sixty-four squares of the chessboard.  The
king, believing he can be rid of the pauper for the price of a bowl or
two of rice, agrees.  By the end of the exercise, the pauper is owed
enough rice to empty all the royal granaries.  (2 to the power 64 is
18,446,744,073,709,551,616.)

The same principle occurs in bacterial infection.  A single bacterium
infects the host, and then after a short time divides in two.  This is
repeated indefinitely; for a while, the host notices nothing, until
quite suddenly the infection blossoms.  The rate of doubling hasn't
altered appreciably; but the increase after the initial slow period is
dramatic.

> I was too lazy to sit down with paper and pen to figure out how much
> the second choice came out to.

They weren't testing your ability to perform the arithmetic (2 to the
power 30 is 1,073,741,824); they were testing your familiarity with the
principle.

> I came up with this formula: n = n*2.

Yep, that's the formula (actually y = x * 2, so that it's clear there's
an old value and a new one).

> def calc(n, days):
>     i = 1
>     while i <= days:
>         n = n*2
>         i = i+1
>     return n/float(100)
> 
> I completely stumbled across it and it works.

Copngratulations (and further points for wanting to understand why it
works).

> I just don't understand how the value of n is assigned to the second n
> in "n=n*2" once it loops back again.

The assignment conceptually occurs in two steps:

    - evaluate ("make a single value from") the right-hand side
    - asign the value to the left-hand side

So the evaluation creates a new value that is (n*2).  Then, this new
value is assigned to n, and whatever value n held previously is
forgotten.  None of this is visible to you; it's all handled by the
Python engine.  All you see is the result (that n has the new calculated
value).

> I would understand if it was a recursive function and passed as an
> argument, but I don't think I completely understand how the "while"
> statement handles variable values.

Hopefully this helps resolve the dilemma of a self-referential
assignment.

> I am learning from "How to Think Like a Computer Scientist: Learning
> With Python"

A good text.  Also work through these:

    "Python tutorial" (as soon as you can)
    <http://www.python.org/doc/tut/>

    "Dive Into Python" (when you're ready for some more)
    <http://www.diveintopython.org/>

-- 
 \          "It was half way to Rivendell when the drugs began to take |
  `\     hold"  -- Hunter S. Tolkien, _Fear and Loathing in Barad-Dûr_ |
_o__)                                                                  |
Ben Finney <http://bignose.squidly.org/>




More information about the Python-list mailing list