Skip to content

Commit

Permalink
Another two problems solved for level 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Paramonov committed Mar 6, 2010
1 parent fa89d89 commit a0f2aeb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
32 changes: 32 additions & 0 deletions p024.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
%% Problem
%% ---------------------
%% A permutation is an ordered arrangement of objects. For example,
%% 3124 is one possible permutation of the digits 1, 2, 3 and 4.
%% If all of the permutations are listed numerically or alphabetically,
%% we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
%%
%% 012 021 102 120 201 210
%%
%% What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
%% ---------------------

-module(p024).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").


solve() -> find(1000000, "0123456789").

%% Brute force works relatively fast.
%%
find(Nth, List) -> lists:nth(Nth, perms(List)).

perms([]) -> [[]];
perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].


perms_test() ->
?assertEqual(["012", "021", "102", "120", "201", "210"], perms("012")).

find_test() ->
?assertEqual("120", find(4, "012")).
13 changes: 13 additions & 0 deletions p028.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
%% Problem
%% ---------------------
%% What is the sum of the numbers on the diagonals in a 1001 by 1001
%% spiral formed in the same way?
%% ---------------------

-module(p028).
-export([solve/0]).


%% N^2 + (N^2-N+1) + (N^2-2N+2) + (N^2-3N+3) = 4N^2-6N+6
%%
solve() -> 1 + lists:sum([ 4*N*N-6*N+6 || N <- lists:seq(3, 1001, 2) ]).

0 comments on commit a0f2aeb

Please sign in to comment.