%%%-------------------------------------------------------------------
%%% @author jaejin <>
%%% @copyright (C) 2012, jaejin
%%% @doc
%%% 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?
%%% @end
%%% Created : 31 Jan 2012 by jaejin <>
%%%-------------------------------------------------------------------
-module(euler12).
-author(jaejin).
-compile(export_all).
trianglenumbers(N)->
trianglenumbers(N,0).
trianglenumbers(N,Result) when N > 1 ->
trianglenumbers(N-1,Result + N);
trianglenumbers(1,Result) ->
Result +1.
divisors(N)->
dict:fold(fun(_Key,Value,Sum)-> (Value+1) * Sum end,1, divisor_dict(N)).
divisor_dict(N)->
divisor_dict(N,2,dict:new()).
divisor_dict(N,M,Dict) when N =:= M ->
dictStore(Dict,M);
divisor_dict(N,M,Dict) when N rem M =:= 0 ->
divisor_dict(N div M,M,dictStore(Dict,M));
divisor_dict(N,M,Dict) when N rem M =/= 0 ->
divisor_dict(N,M+1,Dict).
dictStore(Dict,Key)->
Is_key = dict:is_key(Key,Dict),
if Is_key ->
dict:store(Key,dict:fetch(Key,Dict)+1,Dict);
true ->
dict:store(Key,1,Dict)
end.
run() ->
run(7,28).
run(N,Result)->
C = divisors(Result),
%% % io:format("Result => ~p ~n",[{Result,N,C}]),
%% if C >= 200 ->
%% N;
%% C < 200 ->
%% run(N+1,Result+N)
end.
이걸로 했더니. 답이 안나온다. ㅡㅡ;;
안되서 자바로 했다.
import java.util.HashMap;
/**
* Created by IntelliJ IDEA.
* User: jaejinyun
* Date: 12. 4. 2.
* Time: 오후 11:11
* To change this template use File | Settings | File Templates.
*/
public class Euler11 {
public static int divisor(int n) {
HashMap<Integer,Integer> divisor = new HashMap<Integer, Integer>();
int find = 2;
int m = n;
System.out.println("find = "+find + " n = " + n + " m ="+m);
while(find <= m) {
System.out.println(find);
if( m % find == 0) {
m = m / find;
if( divisor.get(find) != null ) {
divisor.put(find,divisor.get(find)+1);
} else {
divisor.put(find,1);
}
} else {
find++;
}
}
int result =1;
for(int value : divisor.values()) {
result *= (value+1);
}
return result;
}
public static void find() {
int n = 7;
int result = 28;
int divisorResult = 0;
while(divisorResult <= 500) {
n++;
result += n;
divisorResult = divisor(result);
System.out.println("n ="+n+" triagle num="+result + " divisor ="+divisorResult);
}
}
public static void main(String[] args) {
System.out.println("test");
find();
}
}
댓글 없음:
댓글 쓰기