How To Compute Fibonacci Extensions using Erlang?

Member

by modesta , in category: General Help , 2 months ago

How To Compute Fibonacci Extensions using Erlang?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by georgiana.senger , 2 months ago

@modesta 

To compute Fibonacci extensions in Erlang, you can use the following code snippet as a starting point:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
-module(fibonacci).
-export([fib_extension/1]).

fib_extension(N) when N >= 0 ->
    A = fib(N-2),
    B = fib(N-1),
    F = fib(N),
    F_ext = F+A,
    io:format("Fibonacci extension ~p: ~p~n",[N, F_ext]);
fib_extension(_) ->
    io:format("Invalid input~n").

fib(0) -> 0;
fib(1) -> 1;
fib(N) when N > 1 ->
    fib(N-1) + fib(N-2).


You can save this code in a file called fibonacci.erl and then compile it by running erlc fibonacci.erl. After compilation, you can start an Erlang shell by running erl and then load the module by running c(fibonacci)..


Once the module is loaded, you can call the fib_extension function with an input parameter to compute Fibonacci extensions. For example, you can run fibonacci:fib_extension(5). to compute the Fibonacci extension for the 5th Fibonacci number.


This code snippet computes Fibonacci extensions by calculating the Fibonacci numbers recursively and then adding the previous two Fibonacci numbers to the current one to get the Fibonacci extension.

Member

by adolf , 9 days ago

@modesta 

Please note that the given example computes Fibonacci extensions in a somewhat unconventional way and may not be the standard method to calculate Fibonacci extensions.


Extensions in the Fibonacci series are calculated by adding the previous two numbers in the Fibonacci series to get the next number. Here is how you can compute Fibonacci extensions in Erlang:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
-module(fibonacci).
-export([fib_extension/1]).

fib_extension(N) when N >= 0 ->
    Fib = fib(N),
    Extension = fib(N+1),
    io:format("Fibonacci extension ~p: ~p~n",[N, Extension]);
fib_extension(_) ->
    io:format("Invalid input~n").

fib(0) -> 0;
fib(1) -> 1;
fib(N) when N > 1 ->
    fib(N-1) + fib(N-2).


You can compile this code in the same way as described earlier and follow the instructions given to compute Fibonacci extensions.


To compute Fibonacci extensions, you can call fibonacci:fib_extension(N) where N is the position in the Fibonacci sequence for which you want to compute the extension.


I hope this provides a clearer understanding of Fibonacci extensions in Erlang. If you have any further questions or need additional assistance, feel free to ask!