-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Another two problems solved for level 1
- Loading branch information
Andrey Paramonov
committed
Mar 6, 2010
1 parent
fa89d89
commit a0f2aeb
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ]). |