'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