Shorter code
This commit is contained in:
parent
ee9765dbac
commit
76458d1f0a
13
e1.erl
13
e1.erl
|
@ -1,13 +1,8 @@
|
||||||
-module(e1).
|
-module(e1).
|
||||||
-export([e1/0]).
|
-export([e1/0]).
|
||||||
|
|
||||||
add_list([], Acc) -> Acc;
|
sum_divisors(Max, Max, Acc) -> Acc;
|
||||||
add_list([X| Tail], Acc) -> add_list(Tail, Acc+X).
|
sum_divisors(Start, Max, Acc) when Start rem 3 == 0; Start rem 5 == 0 -> sum_divisors(Start+1, Max, Acc+Start);
|
||||||
|
sum_divisors(Start, Max, Acc) -> sum_divisors(Start+1, Max, Acc).
|
||||||
|
|
||||||
find_3_5_divisibles(Start, Max) -> find_3_5_divisibles(Start, Max, []).
|
e1() -> sum_divisors(2, 1000, 0).
|
||||||
|
|
||||||
find_3_5_divisibles(Max, Max, Acc) -> Acc;
|
|
||||||
find_3_5_divisibles(Start, Max, Acc) when Start rem 3 == 0; Start rem 5 == 0 -> find_3_5_divisibles(Start+1, Max, [Start |Acc]);
|
|
||||||
find_3_5_divisibles(Start, Max, Acc) -> find_3_5_divisibles(Start+1, Max, Acc).
|
|
||||||
|
|
||||||
e1() -> add_list(find_3_5_divisibles(2, 1000), 0).
|
|
||||||
|
|
3
e1.m
3
e1.m
|
@ -1,8 +1,5 @@
|
||||||
// comment
|
|
||||||
|
|
||||||
#include <objc/Object.h>
|
#include <objc/Object.h>
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
int i;
|
int i;
|
||||||
|
|
16
e1.pl
16
e1.pl
|
@ -1,13 +1,7 @@
|
||||||
range(End, End, []) .
|
divisible_3_5(N) :- 0 is N rem 3 ; 0 is N rem 5.
|
||||||
range(Start, End, [Start| Acc]) :- Next is Start + 1, range(Next, End, Acc).
|
|
||||||
|
|
||||||
divisors_3_5([], []).
|
sum_divisors(End, End, Total, Total).
|
||||||
divisors_3_5([X|Xs], [X|Ys]) :- 0 is X rem 3, divisors_3_5(Xs, Ys).
|
sum_divisors(Start, End, Acc, Total) :- divisible_3_5(Start), Next is Start + 1, NewAcc is Acc + Start, sum_divisors(Next, End, NewAcc, Total).
|
||||||
divisors_3_5([X|Xs], [X|Ys]) :- 0 is X rem 5, divisors_3_5(Xs, Ys).
|
sum_divisors(Start, End, Acc, Total) :- Next is Start + 1, sum_divisors(Next, End, Acc, Total).
|
||||||
divisors_3_5([X|Xs], Ys) :- divisors_3_5(Xs, Ys).
|
|
||||||
|
|
||||||
sum(List, Result) :- sum(List, 0, Result).
|
e1(Result) :- sum_divisors(2, 1000, 0, Result).
|
||||||
sum([], Acc, Acc).
|
|
||||||
sum([X|Xs], Acc, Num) :- Inc is X + Acc, sum(Xs, Inc, Num).
|
|
||||||
|
|
||||||
e1(Result) :- range(1,1000, R), divisors_3_5(R, Ds), sum(Ds,Result).
|
|
||||||
|
|
1
e1.py
1
e1.py
|
@ -4,5 +4,4 @@ sum = 0
|
||||||
for i in range(2,1000):
|
for i in range(2,1000):
|
||||||
if i % 3 == 0 or i % 5 ==0 :
|
if i % 3 == 0 or i % 5 ==0 :
|
||||||
sum += i
|
sum += i
|
||||||
|
|
||||||
print sum
|
print sum
|
||||||
|
|
4
e1.rb
4
e1.rb
|
@ -1,11 +1,9 @@
|
||||||
#!/usr/bin/ruby
|
#!/usr/bin/ruby
|
||||||
|
|
||||||
sum = 0
|
sum = 0
|
||||||
|
|
||||||
for i in 2..999
|
for i in 2..999
|
||||||
if i % 3 == 0 or i % 5 == 0
|
if i % 3 == 0 or i % 5 == 0
|
||||||
sum += i
|
sum += i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
puts sum
|
||||||
print sum.to_s + "\n"
|
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
erl
|
erl
|
||||||
c(filename)
|
c(filename).
|
||||||
module:function
|
module:function().
|
||||||
|
|
Loading…
Reference in New Issue