FreeBASIC マニュアルのトップに戻る

FreeBASIC フィボナッチ数

目次→フォーラム→FreeBASIC→補足The Fibonacci series←オリジナル・フォーラム

フィボナッチ数 左にメニュー・フレームが表示されていない場合は、ここをクリックして下さい

←リンク元に戻る プログラム開発関連に戻る

 srvaldez さんと dodicat さんが The Fibonacci series で公開している、フィボナッチ数 を表示するプログラム例です。

フィボナッチ数については、下記のサイトで解説されています。

 多くの植物に潜む「フィボナッチ数列」の不思議
http://logmi.jp/157794

 芸術・自然と数学の融合
http://www.cwo.zaq.ne.jp/bfaby300/math/fibona.html

 一般項の求め方をマスターしよう
https://studyplus.jp/445


FreeBASIC でフィボナッチ数を表示するプログラムは、下記の項でも紹介しています。
Allocate
GMP ライブラリを使ったユーザ定義型の活用例
ロゼッタ・コード
Big number wrapper(GMP int 使用)


'The Fibonacci series
'https://www.freebasic.net/forum/viewtopic.php?f=2&t=26032
'Post by srvaldez ≫ Oct 18, 2017 12:13 

'prints the first 116 Fibonacci numbers 
'http://www.flyingcoloursmaths.co.uk/ask-uncle-colin-is-the-fibonacci-series-witchcraft/

'F115までを列挙

#Include Once "gmp.bi"

Dim As __mpf_struct fib
Dim As String s
Dim As ZString Ptr buf=Allocate(3000)
Dim As Integer i, j

mpf_init2(@fib,9600)
Print "フィボナッチ数の最初の116を計算する"
s=String(23,"9")+"8"+String(24,"9")
mpf_set_str(@fib, s, 10)
mpf_ui_div (@fib, 1, @fib) 'fib=1/fib
gmp_sprintf(buf, "%2885.2880Ff", @fib)
s=*buf
s=Mid(s,6)

j=1
For i=1 To 58
   Print Mid(s,j,24),Mid(s,j+24,24)
   j+=48
Next
DeAllocate(buf)
mpf_clear(@fib)

Sleep
このページの先頭に戻る↑ トップページに戻る

'The Fibonacci series
'https://www.freebasic.net/forum/viewtopic.php?f=2&t=26032
'Post by dodicat ≫ Oct 18, 2017 13:09 

'fibonacci by data type -- string.
'following Wiki format
'I.e.
'The first few Fibonacci numbers Fn for n = 0, 1, 2, …
'F0   F1   F2   F3   F4   F5   F6   F7   F8   F9   F10   F11   F12   F13   F14   F15   F16   F17   F18  F19  F20
' 0    1    1    2    3    5    8   13   21   34    55    89   144   233   377   610   987   1597 2584 4181 6765


Function fibonacci(n As Long) As String
    Static As UByte Qmod(0 To 19)={48,49,50,51,52,53,54,55,56,57,48,49,50,51,52,53,54,55,56,57}
    Static As UByte Qbool(0 To 19)={0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1}
    Dim As UByte addup,addcarry
    Dim As Long diff,n_,LL
    If n=0 Then Return "0"
    If n=1 Or n=2 Then Return "1"
    If n=3 Then
        Return "2"
        Else
    Dim As String sl="1",l="2",term
    For x As Long= 1 To n-3
        LL=Len(l)
        diff=-(LL<>Len(sl))
        term="0"+l
        addcarry=0
        For n_=LL-1 To diff Step -1
            addup=sl[n_-diff]+l[n_]-96
            term[n_+1]=Qmod(addup+addcarry)
            addcarry=Qbool(addup+addcarry)
        Next n_
        If addcarry=0 Then  term=LTrim(term,"0"):GoTo skip
        If n_=-1      Then term[0]=addcarry+48  :GoTo skip
        For n_=n_ To 0 Step -1
            addup=l[n_]-48
            term[n_+1]=Qmod(addup+addcarry)
            addcarry=Qbool(addup+addcarry)
            If addcarry=0 Then Exit For
        Next n_
        term[0]=addcarry+48
       If addcarry=0 Then term=LTrim(term,"0")
        skip:
        sl=l
        l=term
    Next x
    Function =term
    End If
End Function

Width 130,500
Dim As String s
For n As ULong=0 To 450
    s=s+ "F"&n & String(12-Len(Str(n))," ") &fibonacci(n) &!"\n" 'position + gap +fibonacci number + new line
   'print "F";n, fibonacci(n)
Next
Print s
Sleep
 
補足 に戻る
←リンク元に戻る プログラム開発関連に戻る
ページ歴史:2017-10-22 作成:2017-10-22
日本語翻訳:WATANABE Makoto、原文著作者:srvaldez、dodicat

ホームページのトップに戻る

表示-非営利-継承