Author Topic: MYOFS  (Read 3029 times)

0 Members and 1 Guest are viewing this topic.

Offline STxAxTIC

  • Library Staff
  • Forum Resident
  • Posts: 1091
  • he lives
    • View Profile
MYOFS
« on: December 15, 2020, 06:12:18 am »
This is a bite-size piece of code that's been lingering on my desktop for a while and it needs a new home.

MYOFS stands for "Make your own Fibonacci sequence". It turns out that that usual 1 1 2 3 5 8 13... thing can be generalized. A little backstory first. The Fibonacci sequence, as we all know it, can actually be generated by doing something cute with the golden ratio. That is, a number close to 1.618 seeds the entire sequence. This program lets you change the seed parameter from the golden ratio to whatever you want, generating the appropriate sequence.

Once that gets boring, the game becomes finding new sequences that are all integers, not just a bunch of decimals. Welp, turns out there are many of these cases. Un-comment some other initial x-values to get them:

Code: QB64: [Select]
  1.  
  2. DIM Coefficient(21) AS DOUBLE
  3. x = 1 / 2 + SQR(5) / 2
  4. 'x = 1 + SQR(2)
  5. 'x = 3 / 2 + SQR(13) / 2
  6. 'x = 2 + SQR(5)
  7.     CLS
  8.     IF (_KEYDOWN(18432) = -1) THEN ' Up-arrow
  9.         x = x + .01
  10.     END IF
  11.     IF (_KEYDOWN(20480) = -1) THEN ' Down-arrow
  12.         x = x - .01
  13.     END IF
  14.     _KEYCLEAR
  15.     Coefficient(1) = 1
  16.     Coefficient(2) = x - 1 / x
  17.     PRINT "x="; x
  18.     PRINT "C_1="; Coefficient(1)
  19.     PRINT "C_2="; Coefficient(2)
  20.     FOR k = 3 TO UBOUND(Coefficient)
  21.         Coefficient(k) = -1 * Coefficient(k - 2) + x ^ (k - 1) + (-1) ^ (k - 1) * 1 / (x ^ (k - 1))
  22.         PRINT "C_"; LTRIM$(RTRIM$(STR$(k))); "="; Coefficient(k) ', Coefficient(k) / Coefficient(k - 1)
  23.     NEXT
  24.     _LIMIT 60
  25.     _DISPLAY
  26.  
« Last Edit: December 15, 2020, 06:23:02 am by STxAxTIC »
You're not done when it works, you're done when it's right.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: MYOFS
« Reply #1 on: December 15, 2020, 10:34:59 am »
I suspect Pythagorean Triples are in play for all integer sequences, I will check this out after I figure out how to defeat a certain pattern of moves against AI, ha, ha!

@STxAxTIC what's your favorite charity?