Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

fibonacci

%% Each new term in the Fibonacci sequence is generated by adding the
%% previous two terms. By starting with 1 and 2, the first 10 terms 
%% will be:
%%
%% 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
%%
%% By considering the terms in the Fibonacci sequence whose values
%% do not exceed four million, find the sum of the even-valued terms.

-module(helloworld).
-export([start/0]).

sum_even_fib(A, B) when A + B > 4000000 ->
    0;
sum_even_fib(A, B) when (A + B) rem 2 =:= 0 ->
    A + B + sum_even_fib(B, A + B);
sum_even_fib(A, B) ->
    sum_even_fib(B, A + B).

start() ->
    io:fwrite("Sum: ~w\n", [sum_even_fib(0, 1)]).

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.