'===========================================================================
' Subject: FIBONACCI SEQUENCE GENERATOR       Date: 05-27-96 (00:00)       
'  Author: Carl Gorringe                      Code: QB, QBasic, PDS        
'  Origin: FidoNet QUIK_BAS Echo            Packet: ALGOR.ABC
'===========================================================================
'>Is there any way to get greater precision than DOUBLE? I am trying to explo
'>the Fibonacci <sp> numbers, and they quickly become hundreds of digits long

'I remember once trying to calculate the Fibonacci sequence, and had the
'same problem that you did: It wouldn't go up to very many digits!
'So, I decided to try it out again and I found a way to do it!
'What I did was to store a number in a STRING variable as text, and then
'make a special function that would add it with another number stored in
'a string. What I came up with is the following. Theoretically it can go
'up to at least 32000 digits, since that's the limit for the length of a
'string variable. Of course, it would take many days to get that far! :)
'Enjoy!

'------------------------------------------
'  FIB.BAS - Fibonacci Sequence Generator
'------------------------------------------
'      (c) Carl Gorringe 5/27/96
'    Released to the PUBLIC DOMAIN.
'
'   This program will calculate the
' Fibonacci Sequence up to thousands of
' digits if let to run. It does this by
' storing very long numbers in strings.
'
' I can be contacted be sending a message to:
' CARL GORRINGE at FIDOnet's QuickBASIC echo or
' Internet e-mail: <carl.gorringe@rhosoft.com>
'
'------------------------------------------------
DECLARE FUNCTION AddStr$ (StrNum1$, StrNum2$)

'--- Credits ---
   SCREEN 0: WIDTH 80, 25
   CLS
   PRINT "FIB.BAS v1.0 - Fibonacci Sequence Generator (by Carl Gorringe)"
   PRINT "----------- Press Any Key for Next Number, <ESC> To Exit -----------"
   PRINT "Count:                Digits:"
   PRINT
   PRINT "Double Precision:"
   PRINT
   PRINT
   PRINT "String Number:"

'--- Open File ---
   'OPEN "FIBO.DAT" FOR OUTPUT AS #1    '<-- Un-rem this to Save to File!

'--- Display Fibonacci Sequence ---

   A# = 1: SA$ = "1"
   B# = 2: SB$ = "2"
   LOCATE 21, 1
   PRINT 1
   PRINT 1
   PRINT 2

   Count% = 3
   DO
      SC$ = AddStr$(SA$, SB$)
      LOCATE 3, 30
      PRINT LEN(SC$);

      VIEW PRINT 9 TO 24
      LOCATE 24, 2
      PRINT SC$
      'PRINT #1, SC$        '<-- Un-rem this to Save to File!
      SA$ = SB$
      SB$ = SC$

      VIEW PRINT
      Count% = Count% + 1
      LOCATE 3, 7
      PRINT Count%;

      IF LEN(SB$) < 300 THEN
         C# = A# + B#
         LOCATE 6, 1
         PRINT C#;
         A# = B#
         B# = C#
      ELSE
         LOCATE 6, 1
         PRINT "*** Overflow ***        ";
      END IF

      I$ = INPUT$(1)       '<-- Rem this and
      'I$ = INKEY$         '<-- un-rem this to take out Pause

   LOOP UNTIL I$ = CHR$(27)

'--- Close File ---
   'CLOSE #1         '<-- Un-rem this to Save to File!

END

FUNCTION AddStr$ (Num1$, Num2$)

' (c) Carl Gorringe  5/27/96  << v1.0 >>
'----------------------------------------------
'    Add the Numbers stored in Strings
' together and return the result as a string.
'----------------------------------------------

Len1% = LEN(Num1$)
Len2% = LEN(Num2$)

IF Len1% > Len2% THEN
   Num2$ = STRING$(Len1% - Len2%, "0") + Num2$
   Len3% = Len1%
ELSEIF Len1% < Len2% THEN
   Num1$ = STRING$(Len2% - Len1%, "0") + Num1$
   Len3% = Len2%
ELSE
   Len3% = Len1%
END IF

Num3$ = ""
Carry% = 0
FOR A% = Len3% TO 1 STEP -1
   Digit1% = ASC(MID$(Num1$, A%, 1)) - 48
   Digit2% = ASC(MID$(Num2$, A%, 1)) - 48
   Sum% = Digit1% + Digit2% + Carry%
   Digit3% = Sum% MOD 10
   Carry% = Sum% \ 10
   Num3$ = CHR$(Digit3% + 48) + Num3$
NEXT A%

IF Carry% > 0 THEN
   Num3$ = CHR$(Carry% + 48) + Num3$
END IF

AddStr$ = Num3$

END FUNCTION
