Fibonacci ecample

Started by Frank Brübach, November 19, 2024, 01:35:48 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frank Brübach

simple fibonacci example, oxygen

uses console

indexbase 1
color 0xf2

'int a,i,le
SetConsoleTitle "fibonacci demo:"

Dim As sys a = -1, b = 1, c = 0, n = 60
Dim As String   title = "fibonacci test "

setpos 2, 2 

Printl title     ' title
Printl String(Len(title) + 2, "-") 
printl           '

int i
For i = 1 To n
    c = a + b
    a = b
    b = c
    Printl i + cr + b + cr
Next

printl "end"
wait
' ends

Frank Brübach

Morning and hello..

I tried to translate this fibonacci bigger example but it fails result Outputs are wrong

' translation try from freebasic
'
uses console

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
    int x
    For x = 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):Goto skip '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) ''Ltrim(term,"0")
        skip:
        sl=l
        l=term
    Next x
    Function =term
    end if
End Function

string s, cr
'cr=chr(13)
long n
for n = 0 to 20 '450   
    ' a)  original: s=s+ "F"&n & string(12-len(str(n))," ") &fibonacci(n) &!"\n"
 
    ' problem zone --------------------------------------------- //
      ' 1)
      's=s+ "F" cr n & string(12-len(str(n))," ") cr fibonacci(n) cr "\n"

    ' 2)
       s=s+ "F " cr n & string(12-len(str(n))," " )  cr fibonacci(n) cr "\n"
     ' problem zone --------------------------------------------- //
     
      'printl "F " + cr + n + cr + fibonacci(n)
next
printl s
print " ends"
wait
'result should be: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

Frank Brübach

#2
Good morning Charles can you Help to fix this Translation? Would be great I have Problem with ltrim and Arrays Here Thanks, Frank

Charles Pegge

Hi Frank,

I don't think it is worth deconstructing this one. At a glance it seems to be using a string number algorithm for adding huge numbers.

The fibonacci series is simply generated by adding the two previous numbers to make the next number, starting with 0 and 1.

As the numbers in the series become larger the ratio between a number and the previoius number converges to (sqr(5)+1)/2 and the inverse is (sqr(5)-1)/2. Now that is interesting :)

Roland Stowasser

Hi Frank,

You may also be interested in two programs in the \demos\math folder:

Fibolter.o2bas:
This allows you to create a sequence up to 46 numbers:

uses console

long f[46]={1,1}
printl f[1]
printl f[2]
int i
for i=3 to 46
f[i]=f[i-2]+f[i-1]
printl f[i]
next
waitkey

There is also BigNumbers.o2bas, where you can create very big sequences. Charles has commented the routine for Fibonacci out, but I think with a small modification this would also work very nice:

...
print "fibonacci" cr
sa=0
sb=1
print 1 tab sb cr
for i=2 to 100
  sr=add sa,sb
  print i tab sr cr
  sa=sb
  sb=sr
next
print "Enter to continue ..." : waitkey
...

The results up to 50 can be compared with:
https://de.wikipedia.org/wiki/Fibonacci-Folge

Frank Brübach

Thanks for replies Charles and Roland..

I have Adept the uncommented example in bignumber.o2bas example for a new fibonacci example and its working very Well.

Two examples included o2

uses console
indexbase 1

namespace bignum

print "1st fibonacci results" cr

long ff[20]={1,1}
printl ff[1]
printl ff[2]
int ii
for ii=3 to 20 '1 to 20
ff[ii]=ff[ii-2]+ff[ii-1]
printl ff[ii]+cr
next
printl "next example.." cr
print "Enter to continue ..." 
waitkey

' 2nd example

  int negative
  string remainder
  '
  macro tobin(s,  bb,le) {
  ========================
  int le=len s
  sbyte bb at strptr s
  while le
    bb-=48
    @bb++ : le--
  wend
  }
  macro toasc(s,  bb,le) {
  ========================
  int le=len s
  sbyte bb at strptr s
  while le
    bb+=48
    @bb++ : le--
  wend
  }
  '
  macro tobin2(s,  bb,bu,le) {
  ============================
  int le=len s
  sbyte bb at strptr(s)+len(s)-1
  sbyte bu at @bb-1
  while le
    bb-=48
    le--
    if le then
      bu-=48
      bb=bu*10+bb 'consolidated byte 0..99
      bu=0
      le-- : @bb-=2 : @bu-=2
    end if
  wend
  }
  macro toasc2(s,  bb,bu,le) {
  ============================
  int le=len s
  sbyte bb at strptr(s)+len(s)-1
  sbyte bu at @bb-1
  while le
    le--
    if le then
      bu=bb\10
      bb=edx+48 'remainder edx   
      le-- : @bb-- : @bu-=2
    end if
    bb+=48 : @bb--
  wend
  }
 
function AlignLeft(string *n1,*n2)
  ===================================
  int l1=len n1
  int l2=len n2
  if l1>l2 then n2+=stringbytes(l1-l2,0) : return
  if l2>l1 then n1+=stringbytes(l2-l1,0) : return
  end function

  function AlignRight(string *n1,*n2)
  ===================================
  int l1=len n1
  int l2=len n2
  if l1>l2 then n2=stringbytes(l1-l2,0)+n2 : return
  if l2>l1 then n1=stringbytes(l2-l1,0)+n1 : return
  end function


'------------------------ //
function add(string sa,sb) as string
  ====================================
  string a=sa
  string b=sb
  tobin a
  tobin b
  alignRight a,b
  int la=len a
  int lb=la
  int lr=la
  lr++ 'allow for carry
  string r=nuls lr
  sbyte aa at strptr(a)+la-1
  sbyte bb at strptr(b)+lb-1
  sbyte rr at strptr(r)+lr-1
  sbyte d,carry
  while lr>1
    d=aa+bb+carry
    if d>9 then d-=10 : carry=1 else carry=0
    rr=d
    @aa-- : @bb-- : @rr--
    la-- : lb-- : lr--
  wend
  if carry then
    rr=carry
  else
    if len(r)>1 then r=mid r,2
  end if
  toasc r
  return r
  end function
 

print "2nd fibonacci results" cr
string sa=0 ' int ?
string sb=1 ' int ?
string sr
print 1 tab sb cr
for ii=2 to 100
  sr=add sa,sb
  print ii tab sr cr
  sa=sb
  sb=sr
next
printl "" cr
print "Enter to continue ..."
print "ok, ends"
waitkey