一个糟糕的Erlang练习题

好吧,用的语法很糟糕。。。但是至少是做了练习。

题目

%The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
%1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
%Let us list the factors of the first seven triangle numbers:
%
% 1: 1
% 3: 1,3
% 6: 1,2,3,6
%10: 1,2,5,10
%15: 1,3,5,15
%21: 1,3,7,21
%28: 1,2,4,7,14,28
%We can see that 28 is the first triangle number to have over five divisors. %What is the value of the first triangle number to have over five hundred divisors?

好吧,我先写了个module,用来计算triangle num:

-module(triangle).
-export([triNum/1]). triNum(0) -> 0;
triNum(N) ->
N + triNum(N-1).

然后,我写了个module,来计算具体某个数有多少个triNum:

-module(divisor).
-export([num_of_divisor/2]). num_of_divisor(_, 0) -> 0;
num_of_divisor(Num, Factor) when (Num rem Factor) =:= 0 ->
2 + num_of_divisor(Num, Factor - 1);
num_of_divisor(Num, Factor) when (Num rem Factor) =/= 0 ->
num_of_divisor(Num, Factor-1).

好吧,然后,我又用了第三个module:

-module(forRange).
-export([forRange/1]).
-import(triangle, [triNum/1]).
-import(divisor, [num_of_divisor/2]). forRange(0) -> 0;
forRange(N) when N>0 ->
TriNum = triNum(N),
Res = num_of_divisor(TriNum, TriNum)/2,
if
Res >= 100 ->
io:format("~f~n", [Res]),
io:format("~b~n", [N]);
true ->
io:format("~n")
end, forRange(N-1).

。。。

最后数找到了384,我还给自己找了个冠冕堂皇的接口,我还在熟悉语法。。。

上一篇:js-Higher-base.js


下一篇:一种常见的maven打包后同名文件冲突错误