Skip to content

Instantly share code, notes, and snippets.

@viniciusd
Created May 25, 2020 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viniciusd/6f99439d7bf71492efe4b9fb53202375 to your computer and use it in GitHub Desktop.
Save viniciusd/6f99439d7bf71492efe4b9fb53202375 to your computer and use it in GitHub Desktop.
-module(tail_recursion).
-export([is_perfect/1]).
is_perfect(N) ->
2*N == lists:sum(divisors(N)).
divisors(N) ->
divisors(N, 1, []).
divisors(N, I, ACC) when I*I < N andalso N rem I == 0 ->
divisors(N, I+1, [I|[N div I|ACC]]);
divisors(N, I, ACC) when I*I == N andalso N rem I == 0 ->
divisors(N, I+1, [I|ACC]);
divisors(N, I, ACC) when I*I =< N ->
divisors(N, I+1, ACC);
divisors(_N, _I, ACC) ->
ACC.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment