How To Compute Fibonacci Extensions in Erlang?

Member

by larissa , in category: Third Party Scripts , 2 months ago

How To Compute Fibonacci Extensions in Erlang?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by aniya.jaskolski , 2 months ago

@larissa 

To compute Fibonacci extensions in Erlang, you can create a recursive function that calculates the Fibonacci sequence up to a certain number of terms and then calculates the Fibonacci extensions based on the last two numbers in the sequence.


Here is an example implementation in Erlang:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-module(fibonacci).
-export([fibonacci/1]).

fibonacci(0) -> [];
fibonacci(N) -> fibonacci(N, [0, 1]).

fibonacci(1, Seq) -> Seq;
fibonacci(N, Seq) ->
    [A, B | Rest] = Seq,
    Next = A + B,
    NewSeq = Seq ++ [Next],
    fibonacci(N - 1, NewSeq).

compute_extensions(Seq) ->
    {_, B | _} = lists:last(Seq),
    {_, A | _} = lists:nth(-2, Seq),
    Extensions = [B, A + B, (A + B) * 1.618],
    Extensions.

% Example usage
Seq = fibonacci(10),
Extensions = compute_extensions(Seq),
io:format("Fibonacci Sequence: ~p~n", [Seq]),
io:format("Fibonacci Extensions: ~p~n", [Extensions]).


You can compile this code using the Erlang compiler and run it in an Erlang shell to compute the Fibonacci sequence and extensions. The fibonacci/1 function generates the Fibonacci sequence up to a specified number of terms and the compute_extensions/1 function calculates the Fibonacci extensions based on the last two numbers in the sequence.


You can adjust the input parameter to the fibonacci/1 function to generate a longer or shorter Fibonacci sequence and compute the corresponding extensions.