Calculate PHI

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
hughv
Newbie
Posts: 3
Joined: Tue Jun 23, 2009 9:30 pm
Location: Annapolis MD
Contact:

Calculate PHI

Post by hughv »

It's been nearly 20 years since I looked at Basic, and I thought this would be easy, but I was wrong.
I was reading about a way to calculate PHI:
Take any two numbers: x, y
Add them together: z = x + y
Add the previous number: a= z + y
Continue until you have 20 numbers and divide the 20th by the 19th
The series would look like this:
x = 2
y = 5
z = 7
a = 12
How do I add the previous number?
TIA
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Where did you find this formula to use?

PHI is based on the Fibonacci sequence of numbers as far as I know.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
hughv
Newbie
Posts: 3
Joined: Tue Jun 23, 2009 9:30 pm
Location: Annapolis MD
Contact:

Post by hughv »

'The Golden Ratio" by Mario Livio
He explains it this way, " If you define a series of numbers by the property that each term (Starting with the third) is equal to the sum of the two preceding ones, then irrespective of the two numbers you started with, as long as you go sufficiently far down the sequence, the ratio of two successive terms always approaches Phi."
He suggests Euclid may have been responsible for the popularization of Phi in Books II and IV of " Elements" in regard to the construction of pentagons.
In any case, have you got a suggestion on how to do this calculation in Qbasic?
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

The following two Functions can determine the Fibonacci values using N as the number of iterations in the Fibonacci sequence. Iterations could represent the number of months that 2 rabbits have been breeding. If N = 0 then you only have no rabbits so no breeding took place.

Code: Select all

  'code by QBguy
CLS
PRINT FIB(12) ' substitute other values or use an Input statement.

FUNCTION FIB (n)
FIB = FIBITER(1, 0, 0, 1, n)
END FUNCTION

FUNCTION FIBITER (a, b, p, q, n)
'PRINT "FIBITER ("; a; ","; b; ","; p; ","; q; ","; n; ")" 'display each iteration
IF n = 0 THEN
FIBITER = b
ELSEIF n AND 1 THEN
FIBITER = FIBITER(b * q + a * q + a * p, b * p + a * q, p, q, n - 1)
ELSE
FIBITER = FIBITER(a, b, p * p + q * q, q * q + 2 * q * p, n \ 2)
END IF
END FUNCTION

Just call the FIB Function using a value of N. FIBITER uses recursive calls to itself. You can redefine the functions as LONG values for larger numbers.

Phi represents the ratio of the resulting FIB values as you stated.

You can just use: PRINT FIB(30) 'to see the results.
Last edited by burger2227 on Fri Jun 26, 2009 12:27 pm, edited 3 times in total.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
hughv
Newbie
Posts: 3
Joined: Tue Jun 23, 2009 9:30 pm
Location: Annapolis MD
Contact:

Post by hughv »

Thanks. I'll look at this later.
Post Reply