Python与Go斐波那契数列

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 斐波那契数列


def fibonacci_sequence(num):
    aa = 0
    b = 1
    li = list()
    li.append(aa)
    li.append(b)
    for i in range(1, num):
        aa, b = b, a + b
        li.append(b)
    return li


if __name__ == '__main__':
    a = fibonacci_sequence(10)
    print(a)
package main

// 斐波那契数列
import "fmt"

func FibonacciSequence(num int) (array []int) {
    a := 0
    b := 1
    array = append(array, a)
    array = append(array, b)
    for i := 1; i < num; i ++ {
        a, b = b, a + b
        array = append(array, b)
    }
    return
}

func main() {
    a := FibonacciSequence(10)
    fmt.Println(a)
}

 

posted @ 2018-10-23 11:18  失落的黎明  阅读(214)  评论(0编辑  收藏  举报