WebAssembly 費氏數列示範

WebAssembly Fibonacci Demo

WebAssembly フィボナッチ数列デモ

這是一個小示範,呼叫由 C 編譯成 WebAssembly 的費氏數列函式。輸入一個整數後按下計算

A small demo that calls a Fibonacci function compiled to WebAssembly from C. Enter an integer and press Calculate.

C から WebAssembly にコンパイルしたフィボナッチ数列の関数を呼び出す、小さなデモです。整数を入力して計算を押してください。

參考資料

References

参考資料

動態規劃版費氏數列 (C)

Dynamic-programming Fibonacci (C)

動的計画法によるフィボナッチ数列 (C)

以下是另一個以動態規劃實作的費氏數列版本,之後會再用 WebAssembly 實作。

Below is another version of the Fibonacci series using dynamic programming. It will be implemented in WebAssembly later.

以下は動的計画法で実装したフィボナッチ数列の別バージョンです。こちらも後日 WebAssembly で実装する予定です。

#include <stdio.h>

long fib(int i);

int main()
{
    long oput = fib(30);
    printf("%ld", oput);
    return 0;
}


long output[1000] = {0};

long fib(int i){
    long result;

    result = output[i];

    if (i == 0) {
        result = 0;
    } else if (i == 1) {
        result = 1;
    } else {
        if (output[i] != 0) {
            return output[i];   // use the previously calculated fib
        } else {
            result = (fib(i - 2) + fib(i - 1));
        }
    }
    output[i] = result;
    return result;
}