From f31e2f4a4e452a709187824d81c952ce35fe6c40 Mon Sep 17 00:00:00 2001 From: Edera Date: Sun, 10 Oct 2021 21:35:56 +0200 Subject: [PATCH] added: my solutions in python and in erlang to the divisor challenge --- .../solutions/valentinadagata/main.erl | 21 +++++++++++++++++ .../solutions/valentinadagata/main.py | 23 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 challenges/easy/divisor-of-a-number/solutions/valentinadagata/main.erl create mode 100644 challenges/easy/divisor-of-a-number/solutions/valentinadagata/main.py diff --git a/challenges/easy/divisor-of-a-number/solutions/valentinadagata/main.erl b/challenges/easy/divisor-of-a-number/solutions/valentinadagata/main.erl new file mode 100644 index 0000000..db5a8ce --- /dev/null +++ b/challenges/easy/divisor-of-a-number/solutions/valentinadagata/main.erl @@ -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)]). \ No newline at end of file diff --git a/challenges/easy/divisor-of-a-number/solutions/valentinadagata/main.py b/challenges/easy/divisor-of-a-number/solutions/valentinadagata/main.py new file mode 100644 index 0000000..92669b1 --- /dev/null +++ b/challenges/easy/divisor-of-a-number/solutions/valentinadagata/main.py @@ -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))