classification
Title: Tutorial: Fibonacci numbers start with 1, 1
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: docs@python, mark.dickinson, rhettinger, skyhein, steven.daprano, terry.reedy
Priority: normal Keywords: patch

Created on 2017-10-11 14:40 by skyhein, last changed 2017-10-14 14:36 by rhettinger. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3991 merged rhettinger, 2017-10-14 05:48
Messages (8)
msg304144 - (view) Author: Heinrich Schnermann (skyhein) * Date: 2017-10-11 14:40
In https://docs.python.org/3/tutorial/controlflow.html#defining-functions both examples produce fibonacci numbers starting with 0. They should start with 1, as in the example in https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming

The first example should change the lines
    while a < n:
        print(a, end=' ')
to
    while b < n:
        print(b, end=' ')

and the second example should change the lines
    while a < n:
        result.append(a)    # see below
to
    while b < n:
        result.append(b)    # see below
msg304156 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2017-10-11 16:07
Is there any particular reason you want to start with 1? While not universal, it's standard to define `Fib(0) = 0`, and to start the sequence at `0`. (And note that Python usually starts indexing things from 0, so it makes sense to start with `Fib(0)` rather than `Fib(1)`.)

In principle, one could define `Fib(0)=1`, `Fib(1)=1`, `Fib(1)=2`, and so on, but there's a strong reason not to do so: it breaks (or at least uglifies) many nice number-theoretic properties, like `gcd(Fib(m), Fib(n)) == Fib(gcd(m, n))`.
msg304157 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2017-10-11 16:14
A nice response to the question of whether to start with 0 or 1 here: https://stackoverflow.com/a/5901955

> The definition with Fib(0) = 1 is known as the combinatorial
> definition, and Fib(0) = 0 is the classical definition. Both
> are used in the Fibonacci Quarterly, though authors that use
> the combinatorial definition need to add a sentence of
> explanation.

I confess that I hadn't heard of the Fibonacci Quarterly before.
msg304167 - (view) Author: Heinrich Schnermann (skyhein) * Date: 2017-10-11 18:00
I would not insist of starting with 1 instead of 0 (I follow your arguments here), but perhaps it would be nice if it would behave the same way in both chapters. The first fibonacci number examples in https://docs.python.org/3/tutorial/introduction.html#first-steps-towards-programming start with 1.

There are three examples here, in the first,

while b < 10:
    print(b)

should change to

while a < 10:
    print(a)

The output of this first example would have an additional 0:
0
1
1
...

And in the third example

while b < 1000:
    print(b, end=',')

should change to

while a < 1000:
    print(a, end=',')

where the output of this third example would change from

1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

to

0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
msg304174 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2017-10-11 19:18
I agree it would be good to be consistent.
msg304363 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2017-10-13 21:24
I agree that we should be consistent -- with the current standard definition -- with the changes suggested above.  Heinrich, can you, and do you want to, submit a patch?  If so, please also sign the contributor agreement.  See https://www.python.org/psf/contrib/

The Fibonacci numbers start with 1 if there is no F(0), as in Fibonacci's rabbit model.  Before 0 was accepted as a number, the series had to start with F(1) = F(2) = 1.  See https://en.wikipedia.org/wiki/Fibonacci_number
msg304375 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-10-14 05:19
I update the example in "First Steps Towards Programming" to match the one in "Defining Functions" (which is also shown on the home page at www.python.org.
msg304394 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2017-10-14 14:36
New changeset 8c26a34f93db7ae96b42bcce6b557437436c7721 by Raymond Hettinger in branch 'master':
bpo-31757: Make Fibonacci examples consistent (#3991)
https://github.com/python/cpython/commit/8c26a34f93db7ae96b42bcce6b557437436c7721
History
Date User Action Args
2017-10-14 14:36:58rhettingersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-10-14 14:36:11rhettingersetmessages: + msg304394
2017-10-14 05:48:37rhettingersetkeywords: + patch
stage: patch review
pull_requests: + pull_request3967
2017-10-14 05:19:47rhettingersetmessages: + msg304375
2017-10-14 05:14:32rhettingersetassignee: docs@python -> rhettinger

nosy: + rhettinger
versions: + Python 3.7, - Python 3.6
2017-10-13 21:24:28terry.reedysetnosy: + terry.reedy
messages: + msg304363
2017-10-11 23:50:27steven.dapranosetnosy: + steven.daprano
2017-10-11 19:18:19mark.dickinsonsetmessages: + msg304174
2017-10-11 18:00:28skyheinsetmessages: + msg304167
2017-10-11 16:14:21mark.dickinsonsetmessages: + msg304157
2017-10-11 16:07:56mark.dickinsonsetnosy: + mark.dickinson
messages: + msg304156
2017-10-11 14:40:19skyheincreate