1. 程式人生 > >斐波那契數(C/C++,Scheme)

斐波那契數(C/C++,Scheme)

一、背景

斐波那契數的定義:

f0=0
f1=1
fi=fi1+fi2(i>1)

二、程式碼

C++語言版

int fib_iter(int a, int b, int count)
{
    if (count == 0)
        return b;
    else
        return fib_iter(a + b, a, count - 1);
}

int fib(int n)
{
    return fib_iter(1, 0, n);
}

Common Lisp語言版

(defun fib (n)
    (fib-iter 1
0 n)
)
(defun fib-iter (a b count) (if (= count 0) b (fib-iter (+ a b) a (- count 1))))

更正

以上的程式碼部分是後面新增的(2015/12/02),以下部分當時寫串了,其實是關於階層的。不過不影響大家學習,思路是一樣的。我主要也是在展示遞迴和迭代的區別,以上的斐波那契的兩個程式碼就是迭代的。

二、分析

我引用兩張表,大家一看便懂。

1.遞迴

(factorial 6)
(* 6 (factorial 5))
(* 6 (* 5 (factorial 4)))
(*
6 (* 5 (* 4 (factorial 3))))
(* 6 (* 5 (* 4 (* 3 (factorial 2))))) (* 6 (* 5 (* 4 (* 3 (2 (factorial 1)))))) (* 6 (* 5 (* 4 (* 3 (* 2 1))))) (* 6 (* 5 (* 4* 3 2)))) (* 6 (* 5 (* 4 6))) (* 6 (* 5 24)) (* 6 120) 720

2.迭代

(factorial 6)
(factorial 1 1 6)
(factorial 1 2 6)
(factorial 2 3 6)
(factorial
6 4 6)
(factorial 24 5 6) (factorial 120 6 6) (factorial 720 7 6) 720

遞迴的核心在於:不斷地回到起點。
迭代的核心在於:不斷地更新引數。

在下面的程式碼中,遞迴的核心是sum的運算,sum不斷的累乘,雖然運算的數值不同,但形式和意義一樣。

而迭代的核心是product和counter的不斷更新。如上表中,product就是factorial的前2個引數不斷的累乘更新成第一個引數;而第二個引數則是counter,其不斷的加1來更新自己。

product <- counter * product
counter < - counter + 1

三、程式碼

C語言版

#include <stdio.h>
#include <stdlib.h>

int factorialRecursive(int n);
int factorialIteration(int product, int counter, int max_count);

int main()
{
    int n;
    printf("Enter an integer: \n");
    scanf("%d",&n);

    printf("%d\n",factorialRecursive(n));
    printf("%d\n",factorialIteration(1,1,n));

    return 0;
}

int factorialRecursive(int n)
{
    int sum=1;
    if(n==1)
        sum*=1;
    else
        sum=n*factorialRecursive(n-1);
    return sum;
}

int factorialIteration(int product, int counter, int max_count)
{
    int sum=1;
    if(counter>max_count)
        sum*=product;
    else
        factorialIteration((counter*product),(counter+1),max_count);
}

C++語言版

#include <iostream>

using namespace std;

int factorialRecursive(int n);
int factorialIteration(int product, int counter, int max_count);

int main()
{
    int n;
    cout<<"Enter an integer:"<<endl;
    cin>>n;
    cout<<factorialRecursive(n)<<endl;
    cout<<factorialIteration(1,1,n)<<endl;

    return 0;
}

int factorialRecursive(int n)
{
    int sum=1;
    if(n==1)
        sum*=1;
    else
        sum=n*factorialRecursive(n-1);
    return sum;
}

int factorialIteration(int product, int counter, int max_count)
{
    int sum=1;
    if(counter>max_count)
        sum*=product;
    else
        factorialIteration((counter*product),(counter+1),max_count);
}

四、進階

Scheme語言版

(define (factorial n)
    (if (= n 1)
        1
        (* n (factorial (- n 1)))))
(define (factorial n)
    (fact-iter 1 1 n))
(define (fact-iter product counter max-count)
    (if (> counter max-count)
        product
        (fact-iter (* counter product)
                   (+ counter 1)
                   max-counter)))

相關推薦

C語言、Python實現Fibonacci

1、C語言實現   有一對兔子,從出生後第3個月起每個月都生一對兔子。小兔子長到第3個月後每個月又生一對兔子。假設所有兔子都不死,問每個月的兔子總數為多少? #include<stdio.h> int main() { int f1=1,f2=1,f3; int i;

C/C++Scheme

一、背景 斐波那契數的定義: f0=0 f1=1 fi=fi−1+fi−2(i>1) 二、程式碼 C++語言版 int fib_iter(int a, int b, int count) { if (count == 0)

求第n個分別用遞迴和非遞迴兩種方法求解

斐波那契數列指的是這樣一個數列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55……這個數列從第3項開始,每一項都等於前兩項之和。 這裡分別用遞迴和非遞迴的方法實現: 遞迴 #define _CRT_SECURE_NO_WARNINGS 1 #include&l

JAVA實現

public class Fibonacci {    /*輸出斐波那契數*/ publicstaticvoid printFibonacciNumber(long f1,long f2,int n){//the first number, the second number,the totel fib

求第n個(用遞迴的形式

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int Fib(int n) { if (n == 1 || n == 2) // |按位或,||邏輯或 { retur

不使用迭代法的數列兔子生兔子問題解決同時輸出兩個數字的問題

問題: 有一對兔子,生長三個月後。開始生第一對兔子,並且以後每月生一對兔子,小兔子生長三個月後,也開始生兔子,問N個月後兔子的總數量? 通用解法: 使用迭代法,此方法網上有n多版本,再次不再贅述。 不使用迭代法: 在不適用迭代法,而僅僅用for迴圈時,會出現一個問題

HDU 4549 M數列矩陣快速冪3+費馬小定理

C - M斐波那契數列 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB    

C語言----的n種實現方法

斐波那契數列(Fibonacci):第1,2兩個數為1,1。從第三個數開始,該數是其前面兩個數之和。 1.使用簡單程式碼實現 (1)每次迴圈只輸出後一位數 思想:前兩個數為1,1。先定義前兩個變數a,b。第三個數為前兩兩個數之和,定義第三個變數c,c=a+b;現在有三個數,為了避免冗餘,把

數列C++類

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 352457

[c語言]用遞迴和非遞迴求第n個

程式碼 //1.1遞迴求第n個斐波那契數 #include<stdio.h> int fib(int n) { if(n<=2) return 1; else return fib(n-1)+fib(n-2); } int main

C語言遞迴與非遞迴實現求第n個

一、非遞迴實現第N個菲波那切數列: 程式如下: #include <stdio.h> int fib(int n) { int a1 = 1; int a2 = 1; int a3

C語言編程實現數列遞歸與非遞歸

() code tdi clu return include 位置 c語言編程 數組 一.非遞歸 <1>數組 #include<stdio.h> #include<stdlib.h> int main() { int a[1000

問題 : 來簡單地個數大數模擬計算+區間

sample 一個 輸入數據 輸出 一個數 兩個 turn led ycm 題目描述 這是一個斐波那契數列: f1 = 1 f2 = 2 fn = fn-1 + fn-2 (n>=3) 蔡老板想知道,給你兩個數 a、b,你能否求出在區間[a,b]裏有多少個斐波那

[luoguP1962] 數列矩陣快速冪

truct ons 技術 pan opera http 快速冪 printf ble 傳送門 解析詳見julao博客連接 http://worldframe.top/2017/05/10/清單-數學方法-——-矩陣/ —&

hdu 4549 M數列矩陣高速冪高速冪降冪

else if stdlib.h article 1.0 ostream void 我們 memset font http://acm.hdu.edu.cn/showproblem.php?pid=4549 f[0] = a^1*b^0%p,f[1] = a^0*b

的python語言實現---遞歸和叠代

put bsp print span return spa number n-2 遞歸實現 叠代實現如下: def fab(n): n1 = 1 n2 = 1 if n<1: print

[luoguP2626] 數列升級版模擬

sub std [1] 斐波那契數 == cnblogs () ios git 傳送門 模擬 代碼 #include <cmath> #include <cstdio> #include <iostream>

用遞歸方法計算數列Recursion Fibonacci Python)

n-1 html pri style strong ans rdo 黃金分割 nac 先科普一下什麽叫斐波那契數列,以下內容摘自百度百科: 斐波那契數列(Fibonacci sequence),又稱黃金分割數列、因意大利數學家列昂納多·斐波那契(Leonardoda Fib

SICP 1.2.2 樹形遞歸 ()

mce oid nbsp dig efi del 叠代 reat public (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1))

數列大數加法

斐波那契 ++ add ret div 加法 clas 註意 cin 題意: 求斐波那契的前10000項目 分析: 模擬豎式加法, 用string作為數字的儲存形式 #include<bits/stdc++.h> using namespace std; st