56 lines
1.1 KiB
Plaintext
56 lines
1.1 KiB
Plaintext
// Fibonacci, unlimited precision.
|
|
// input: #1 = n
|
|
// return: fibonacci(n) in text register 10
|
|
//
|
|
:fibo_unlimited:
|
|
if (#1 < 2) {
|
|
Num_Str(#1, 10)
|
|
return
|
|
} else {
|
|
Buf_Switch(Buf_Free)
|
|
IC('0') IN
|
|
IC('1') IN
|
|
#10 = #1
|
|
While (#10 > 1) {
|
|
#12 = 0 // carry out
|
|
#15 = 1 // column (ones, tens, hundreds...)
|
|
Repeat (ALL) { // Sum all columns
|
|
Line(-1) // n-1
|
|
Goto_col(#15)
|
|
if (At_EOL) { // all digits added
|
|
break
|
|
}
|
|
#11 = Cur_Char - '0' + #12 // digit of (n-1) + carry
|
|
Line(-1) // n-2
|
|
Goto_Col(#15)
|
|
if (!At_EOL) { // may contain fewer digits than n-1
|
|
#11 += Cur_Char - '0'
|
|
}
|
|
Goto_Line(3) // sum
|
|
EOL
|
|
#12 = #11 / 10 // carry out
|
|
Ins_Char((#11 % 10) + '0')
|
|
#15++ // next column
|
|
}
|
|
if (#12) {
|
|
Goto_Line(3)
|
|
EOL
|
|
Ins_Char(#12 + '0') // any extra digit from carry
|
|
}
|
|
BOF
|
|
Del_Line(1) // Next n
|
|
Line(1) EOL
|
|
Ins_Newline
|
|
#10--
|
|
}
|
|
Goto_Line(2) // Results on line 2
|
|
}
|
|
// Copy the results to text register 10 in reverse order
|
|
Reg_Empty(10)
|
|
While(!At_EOL) {
|
|
Reg_Copy_Block(10, CP, CP+1, INSERT)
|
|
Char()
|
|
}
|
|
Buf_Quit(OK)
|
|
return
|