Re: How do I write a program to calculate the golden section?

new topic     » goto parent     » topic index » view thread      » older message » newer message

On Wed, 10 Jun 1998, Rafael Skovron wrote:

> I want to use Euphoria to make a fibonacci series like:
>         1,2,3,5,8,13,24,37,51
> Where each term is the sum of the last two terms
>        As the terms get larger, the value for the golden section gets
> more exact. I downloaded a program that calculates pi to any number of

Basically the golden section is given by the formula:

                     1 + square_root_of 5
               Phi = --------------------
                               2

or the Iterative formula:

                             1
               Phi    = 1 + ---
                  n+1       Phi
                               n

So you could use these:
-- This code is from my mathbag.e:

global constant Phi = (1 + sqrt(5))/2
    -- The golden section in it's entirety!
global constant Psi = (1 - sqrt(5))/2
    -- The -ve "golden section"

-- Fibonacci

global function fibonacci(integer n)
    return (power(Phi,n) - power(Psi,n)) / sqrt(5)
end function

global function lucas(integer n)
    return power(Phi,n) + power(Psi,n)
end function

However I suspect you may be wanting to calculate Phi to 'n' decimal
places rather than calculate the Fibonacci & Lucas Series from Phi!

If that's the case, you'd have to write your own number handling routines
as Euphoria is usually good for only 15 decimal places...

The formula you'd have to use (assuming you don't have sqrt() available)
would be:

include bigatom.e -- This is something I'm working on. I haven't released
                  -- it yet...
bigatom One -- I'll assume this data type and associated routines have
bigatom Phi -- been coded already in bigatom.e...

One = make_bigatom(1)
Phi = One

puts(1, "Hit SpaceBar to stop me!\n")

while get_key != ' ' do
    Phi =
        add_bigatoms(One,
            divide_bigatoms(One, Phi)
        ) -- Phi = 1 + (1 / Phi)
    display_bigatom(Phi)
end while

--
Carl R White
E-mail...: cyrek- at -bigfoot.com              / Remove the hyphens before
Finger...: crwhite- at -dcsun1.comp.brad.ac.uk \ mailing or fingering...
Url......: http://www.bigfoot.com/~cyrek/

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu