Determine a Finonacci number

Anything Sinclair ZX Basic related; history, development, tips - differences between BASIC on the ZX80 and ZX81
Post Reply
Shaun_B
Posts: 451
Joined: Wed Apr 22, 2009 10:22 am

Determine a Finonacci number

Post by Shaun_B » Sat Jan 12, 2019 11:15 pm

Hi all,

The following BASIC program will tell you whether or not the number you have entered is in the Fibonacci number sequence, printing 1 for true and zero for false. It should allow you to work out the first 50 or so Fibanacci numbers in the sequence.

Tested with normal ROM and ZX81 ROMx2 on an emulated unexpanded machine with EightyOne.

Code: Select all

    1 INPUT A$
    2 LET A=SGN PI
    3 LET B=A
    4 LET F=VAL A$
    5 FOR I=NOT PI TO CODE "L"
    6 LET C=A+B
    7 LET A=B
    8 LET B=C
    9 IF B>=F THEN GOTO CODE "£"
   10 NEXT I
   12 PRINT STR$(B=F OR F<=SGN PI) AND F>=NOT PI;"0" AND F<NOT PI
Here's a similar listing for the ZX80:

Code: Select all

   1 INPUT F
   2 CLS
   3 LET A=1
   4 LET B=A
   5 FOR I=0 TO 23
   6 LET C=B+A
   7 LET A=B
   8 LET B=C
   9 IF B>F OR B=F THEN GO TO 11
  10 NEXT I
  11 IF (B=F OR F=0 OR F=1) THEN GO TO 14
  12 PRINT " NOT FIBONACCI"
  13 STOP
  14 PRINT "FIBONACCI"
Enjoy!

Shaun.

Moggy
Posts: 1869
Joined: Wed Jun 18, 2008 2:00 pm

Re: Determine a Finonacci number

Post by Moggy » Sun Jan 13, 2019 1:17 pm

Excellent Shaun at last something else to add to my maths folder. :D

I use the following, lifted from Rosetta Code, which may give you some further ideas.

Analytic.

10 INPUT N
20 PRINT INT (0.5+(((SQR 5+1)/2)**N)/SQR 5)

Iterative.

10 INPUT N
20 LET A=0
30 LET B=1
40 FOR I=2 TO N
50 LET C=B
60 LET B=A+B
70 LET A=C
80 NEXT I
90 PRINT B

Shaun_B
Posts: 451
Joined: Wed Apr 22, 2009 10:22 am

Re: Determine a Finonacci number

Post by Shaun_B » Mon Jan 14, 2019 8:45 am

Hi Moggy,

I see what you've done there. Thanks for the hints.

Shaun.

Post Reply