Latest web development tutorials

Python includes exercises 11

Python 100 Li Python 100 Li

Title: Classical problem: a pair of rabbits from the first 3 months after birth monthly birth to a rabbit, a small rabbit to the first three months after the month long again with one pair of rabbits, if the rabbit is not dead, and asked the total number of rabbits per month for how much?

Program analysis: number of rabbits column 1,1,2,3,5,8,13,21 law ....

Source Code:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

f1 = 1
f2 = 1
for i in range(1,21):
    print '%12ld %12ld' % (f1,f2),
    if (i % 3) == 0:
        print ''
    f1 = f1 + f2
    f2 = f1 + f2

The above example output is:

           1           1            2           3            5           8 
          13          21           34          55           89         144 
         233         377          610         987         1597        2584 
        4181        6765        10946       17711        28657       46368 
       75025      121393       196418      317811       514229      832040 
     1346269     2178309      3524578     5702887      9227465    14930352 
    24157817    39088169     63245986   102334155    165580141   267914296 

Python 100 Li Python 100 Li