# Fibonacci numbers in Basic

Example for versions
QBasic 1.1,
QuickBASIC 4.5

Fibonacci numbers are calculated using Binet’s formula. The resulting numbers can differ from actual ones slightly due to calculation imprecision; to remove this effect, we use function `INT`

which truncates fractional part of the number.

```
DECLARE FUNCTION FIBONACCI (n)
DIM S AS STRING
S = ""
FOR i = 1 TO 16:
S = S + STR$(INT(FIBONACCI(i) + .1)) + ","
NEXT i
S = S + "..."
PRINT S
FUNCTION FIBONACCI (n)
p1 = ((1 + SQR(5)) * .5) ^ n
p2 = ((1 - SQR(5)) * .5) ^ n
FIBONACCI = (p1 - p2) / SQR(5)
END FUNCTION
```

## Comments

]]>blog comments powered by Disqus

]]>