Created
May 25, 2020 14:06
-
-
Save viniciusd/6f99439d7bf71492efe4b9fb53202375 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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