Created
May 20, 2020 10:28
-
-
Save stormwatch/dd19a752c35df17b600d1f8871bf53f6 to your computer and use it in GitHub Desktop.
Tail recursion, perfect numbers. https://www.futurelearn.com/courses/functional-programming-erlang/3/steps/488098/
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(perfect). | |
-export([perfect/1]). | |
perfect(N) -> | |
perfect(N, 2, 1). | |
perfect(N, D, Sum) when D * 2 < N, Sum < N -> | |
if | |
N rem D =:= 0 -> | |
perfect(N, D + 1, Sum + D + N div D); | |
true -> | |
perfect(N, D + 1, Sum) | |
end; | |
perfect(N, _D, Sum) -> | |
N =:= Sum. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment