Permalink
Browse files

Another two problems solved for level 1

  • Loading branch information...
1 parent fa89d89 commit a0f2aeb67dfef45e350c7cad06fbd3c2269eb69d Andrey Paramonov committed Mar 6, 2010
Showing with 45 additions and 0 deletions.
  1. +32 −0 p024.erl
  2. +13 −0 p028.erl
View
@@ -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")).
View
@@ -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.