# Printing Fibonacci Numbers

November 15 2006 at 11:07 AM
Owner

Here's a fast one

Set
CONST MaxTerms = 2000
CONST nBase = 10000
and remove all the Debugging stuff

It runs as fast as PRINT allows. Would theoretically go until numbers as big as 10000^2000 (I wonder how many years?)

Mac

CONST MaxTerms = 30
CONST nBase = 100
CONST Debugging = -1: ' Set to zero when not debugging
DEFSTR A-Z
DIM MaxLen AS INTEGER: MaxLen = LEN(STR\$(nBase)) - 2
DIM f(3, MaxTerms) AS INTEGER
DIM Terms AS INTEGER: Terms = 1
CLS
f(1, 1) = 0: PRINT 0
f(2, 1) = 1: PRINT 1
LINE INPUT "Press Enter to see more Fibonacci numbers"; e\$
DIM i AS INTEGER, Carry AS INTEGER, Temp AS INTEGER
DO
'======================================
FOR i = 1 TO Terms
Temp = f(1, i) + f(2, i) + Carry
IF Temp >= nBase THEN
Carry = 1
f(3, i) = Temp - nBase
ELSE
Carry = 0
f(3, i) = Temp
END IF
NEXT i
IF Carry > 0 THEN
Terms = Terms + 1
IF Terms > MaxTerms THEN EXIT DO
f(3, Terms) = f(3, Terms) + Carry
Carry = 0
END IF
PRINT LTRIM\$(STR\$(f(3, Terms)));
FOR i = Terms - 1 TO 1 STEP -1
PRINT RIGHT\$(STR\$(nBase + f(3, i)), MaxLen);
NEXT i: PRINT
GOSUB Debug
'======================================
FOR i = 1 TO Terms
Temp = f(2, i) + f(3, i) + Carry
IF Temp >= nBase THEN
Carry = 1
f(1, i) = Temp - nBase
ELSE
Carry = 0
f(1, i) = Temp
END IF
NEXT i
IF Carry > 0 THEN
Terms = Terms + 1
IF Terms > MaxTerms THEN EXIT DO
f(1, Terms) = f(1, Terms) + Carry
Carry = 0
END IF
PRINT LTRIM\$(STR\$(f(1, Terms)));
FOR i = Terms - 1 TO 1 STEP -1
PRINT RIGHT\$(STR\$(nBase + f(1, i)), MaxLen);
NEXT i: PRINT
GOSUB Debug
'======================================
FOR i = 1 TO Terms
Temp = f(3, i) + f(1, i) + Carry
IF Temp >= nBase THEN
Carry = 1
f(2, i) = Temp - nBase
ELSE
Carry = 0
f(2, i) = Temp
END IF
NEXT i
IF Carry > 0 THEN
Terms = Terms + 1
IF Terms > MaxTerms THEN EXIT DO
f(2, Terms) = f(2, Terms) + Carry
Carry = 0
END IF
PRINT LTRIM\$(STR\$(f(2, Terms)));
FOR i = Terms - 1 TO 1 STEP -1
PRINT RIGHT\$(STR\$(nBase + f(2, i)), MaxLen);
NEXT i: PRINT
GOSUB Debug
LOOP
PRINT "Exceeded capacity"
SYSTEM
Debug:
IF NOT Debugging THEN RETURN
DIM Debug1 AS DOUBLE, Debug2 AS DOUBLE, Debug3 AS DOUBLE
IF Debug2 = 0 THEN Debug2 = 1
Debug3 = Debug1 + Debug2: PRINT LTRIM\$(STR\$(Debug3)): PRINT "---"
Debug1 = Debug2: Debug2 = Debug3
IF INPUT\$(1) = CHR\$(27) THEN SYSTEM
RETURN

 Respond to this message
 Response Title Author and Date You're probably wondering why three duplicate sections Mac on Nov 15 Doesn't run for years Mac on Nov 15 *My short attention span thanks y... Pete on Nov 15 More Mac on Nov 15