The QBasic / QB64 Discussion Forum      Other Subforums, Links and Downloads
 
 

 Return to Index  

Printing Fibonacci Numbers

November 15 2006 at 11:07 AM
Mac  (Premier Login iorr5t)
Forum 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 TitleAuthor and Date
You're probably wondering why three duplicate sectionsMac on Nov 15
Doesn't run for yearsMac on Nov 15
   *My short attention span thanks y...Pete on Nov 15
MoreMac on Nov 15
 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement  

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums