Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-module(main).
-export([start/0, d/1, sumdivs/1]).

d(0) -> [];
d(1) -> [];
d(N) -> lists:sort([] ++ divisors(1,N,math:sqrt(N))).

% if the square root is lower than the first num return empty
divisors(F,_N,L) when F > L -> [];
% if the remainder of N and the first is not 0, add 1 to the first number
divisors(F,N,L) when N rem F =/= 0 ->
divisors(F+1,N,L);
% if the number is divisible for the first, add them to the list of divisors
divisors(F,N,L) ->
[F, N div F] ++ divisors(F+1,N,L).

sumdivs(N) -> lists:sum(d(N)).


start() ->
io:fwrite("~w~n", [d(30)]).
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Divisors of a number
Count the number of divisors of a positive integer `n`.

divisors(4) // => 3 (1, 2, 4)
divisors(5) // => 2 (1, 5)
divisors(12) // => 6 (1, 2, 3, 4, 6, 12)
divisors(30) // => 8 (1, 2, 3, 5, 6, 10, 15, 30)
"""
def divisors(n):
# create a list with all the numbers from 1 to n
num = list(range(1, n + 1))
div = []
# for each number, if n % num == 0: append to the divisors list div
# else delete the number from the num list
while len(num) > 0:
if n % num[-1] == 0:
div.append(num[-1])
num.pop()
#print(div)
return len(div)

print(divisors(30))