Archive for the ‘Fun’ Category

Announcements

Monday, April 28th, 2008

Thought I’d go ahead and announce, mainly to myself, that I will be working through SICP. The rub…doing it in Javascript. Seems as though most other languages are covered (I know Erlang is taken) and since I am doing an increasingly large amount of Javascript, coupled with the eventual prevalence of server-side Javascript, I figured it best to start getting intimate. What I like about this task is that since SICP has been so widely covered on the web, I have many resources to aid in better understanding the material (and it is some thick material). Anyway, I’ve begun chapter one and will post the chapters, as well as excerpts I find interesting, in no pre-defined timeframe.

Oh yeah, and I’m engaged.

Portland is fun

Saturday, December 8th, 2007

While the weather has not cooperated and given me many sunny days yet, I have been enjoying exploring the cyber-presence Portland has, in particular the Craigslist offerings. Today, however, brings a new entry in the NW Nerdery: a movement to rename 42nd Ave to Douglas Adams Blvd. The site, rename42nd.org, is making a serious effort to have the 42nd Ave renamed in honor of Douglas Adams, most notable for authoring the Hitchhiker’s Guide to the Galaxy series. My inner geek smiles wide for this effort and hope they succeed. If you are a Portlander, support the movement. I know I am…

Pambiche

Friday, October 5th, 2007

While highlights abound on the Portland tour, one in particular deserves mention. I’ve dined at some pretty sweet places and eaten some fairly exotic foods. While they’ve all been good and memorable, I found that a little Cuban place in Portland has taken the top spot in my dining history. What’s amazing about Pambiche is the unassuming atmosphere surrounding the restaurant. Walking up to, I had no idea what I was about to experience.

Sadly, I was only able to eat there once while in the city, so I can only speak to one dish, Ajiaco. Described as a “one pot meal that comes brimming with a variety of tropical roots and vegetables, corn dumplings, creole seasoned pork and beef”, this tasty meal was unlike any other food I’d tasted. Very subtle flavors and aromas with each spoonful pleased my tastebuds and tummy. And while Cuban food is not traditionally spicy, Pambiche had a homemade spicy sauce which complimented the dish wonderfully.

I am not a food critic so it’s hard to do the experience justice. Next time Portland dining is available, head over to 2811 NE Glisan and taste it firsthand. I plan on doing just that!

Airplanes!

Tuesday, June 19th, 2007

It has been a while since a good time wasting game came across my browser. This one is fun, but once you learn the secret, it’s less challenging and more luck to get the super high distances.

Throw a paper airplane and see where you stand. Me, I’m currently 5608 globally with a distance of 114.452m.

[Update 6/19/2007 5:31 CST] 114.717m for a global ranking of 2766.

Pi Day 2007

Tuesday, March 13th, 2007

Happy Pi Day (3-14, duh). Search pi for sequences of numbers, like your birthday, SSN, phone number, etc here. The page also has a good bit of Pi trivia.

Crazy addictive game

Sunday, February 4th, 2007

Flow in Games provides quite an adventure in gaming. It is no secret that I am not a big player of mainstream console and computer games, and games like Flow confirm that I am not missing out. The combination of visual, aural, and tactical elements creates quite a pleasant experience. I hardly noticed that I had wasted three hours playing! The rules are simple (like most fun games) - eat and evolve, kill or be killed. I think this is a great game for kids because it has a requires some anticipation and coordination skills to time attacks and stirke at the food. I know many more of my hours will be spent playing.

Clean up once in a while

Thursday, February 1st, 2007

Perfect. Last night, amidst the snow and wind, Jason and I went to Gabriel’s and got his old washer and dryer. Nothing says dedication to clean laundry like moving a washer and dryer at 10:30pm in sub-freezing temperatures with snow falling all around you. The roads were bad enough that going over 25mph caused fishtailing - with the washer and dryer over the rear wheels. Other than one minor glitch and some configuration adjustments, the duo is in place and busy with action. Finally, I don’t have to leave the house to do laundry.

With my new-found cleaning apparatus on my mind, wouldn’t you know it; on reddit today, this hilarious commercial for IKEA:

The commercial

Yikes

Futuristic interaction with computers…now!

Sunday, January 21st, 2007

Holy heck. This demo will blow you away. Makes the iPhone look silly. Minority Report-esque computer interaction is very near. View the demo here.

UPDATE: More UI eye candy!

E-99: 31-40

Sunday, January 7th, 2007

Right on the heals of 21-30 comes 31-40. These problems began to delve into mathematics, with a great emphasis on prime numbers and their generation. Quite interesting to work with, since prime numbers are the basis behind encryption of any non-trivial strength. Enjoy:

%% Determine whether a given integer number is prime.
%% Example: (is-prime 7) -> T
p31(2) -> true;
p31(N) when N rem 2 =:= 0 -> false;
p31(N) -> p31is_prime(N, 3, N div 2 ).

p31is_prime(_N, K, Limit) when K > Limit -> true;
p31is_prime(N, K, Limit) ->
case N rem K of
0 -> false;
_Else -> p31is_prime(N, K+2, Limit)
end.

%% Determine the greatest common divisor of two positive integer numbers.
%% Use Euclid’s algorithm.
%% Example: (gcd 36 63) -> 9
p32(A, 0) -> A;
p32(A, B) when B > A -> p32(B, A);
p32(A, B) -> p32(B, A rem B).

%% Determine whether two positive integer numbers are coprime.
%% Two numbers are coprime if their greatest common divisor equals 1.
%% Example: (coprime 35 64) -> T
p33(A, B) -> p32(A, B) =:= 1.

%% Calculate Euler’s totient function phi(m).
%% Euler’s so-called totient function phi(m) is defined as the number of positive integers r (1 < = r < m) that are coprime to m.
%% Example: m = 10: r = 1,3,7,9; thus phi(m) = 4. Note the special case: phi(1) = 1.
%% (totient-phi 10) -> 4
%% Find out what the value of phi(m) is if m is a prime number.
%% psuedo-code if is_prime(m), phi(m) = m-1, else compute phi(m).
%% Euler’s totient function plays an important role in one of the most widely used public key cryptography methods (RSA).
%% In this exercise you should use the most primitive method to calculate this function (there are smarter ways that we shall discuss later).
p34(1) -> 1;
p34(M) -> p34totient_phi(M, 1, []).

p34totient_phi(M, M, L) -> length(L);
p34totient_phi(M, R, L) ->
case p33(M, R) of
true -> p34totient_phi(M, R+1, [R | L]);
false -> p34totient_phi(M, R+1, L)
end.

%% Determine the prime factors of a given positive integer.
%% Construct a flat list containing the prime factors in ascending order.
%% Example: (prime-factors 315) -> (3 3 5 7)
p35(N) -> p35prime_factors(N, 2, []).

p35prime_factors(1, _C, PF) -> lists:reverse(PF);
p35prime_factors(N, 2, PF) ->
case (N rem 2) =:= 0 of
true -> p35prime_factors(N div 2, 2, [2 | PF]);
false -> p35prime_factors(N, 3, PF)
end;
p35prime_factors(N, C, PF) ->
case (N rem C) =:= 0 of
true -> p35prime_factors(N div C, C, [C | PF]);
false -> p35prime_factors(N, C+2, PF)
end.

%% Determine the prime factors of a given positive integer (2).
%% Construct a list containing the prime factors and their multiplicity.
%% Example: (prime-factors-mult 315) -> ((3 2) (5 1) (7 1))
%% Hint: The problem is similar to problem P13.
p36(N) -> p10(p35(N)).

%% Calculate Euler’s totient function phi(m) (improved).
%% See problem P34 for the definition of Euler’s totient function.
%% If the list of the prime factors of a number m is known in the form of problem P36 then the function phi(m) can be efficiently calculated as follows:
%% Let ((p1 m1) (p2 m2) (p3 m3) …) be the list of prime factors (and their multiplicities) of a given number m. Then phi(m) can be calculated with the following formula:
%% phi(m) = (p1 - 1) * [p1 ** (m1 - 1)] * (p2 - 1) * [p2 ** (m2 - 1)] * (p3 - 1) * [p3 ** (m3 - 1)] + …
%% Note that a ** b stands for the b’th power of a.
p37(M) -> p37phi(p36(M), 1).

p37phi([], Phi) -> Phi;
p37phi([[M, P] | L], Phi) -> p37phi(L, Phi * (P - 1) * round(math:pow( P, M-1 )) ).

%% Compare the two methods of calculating Euler’s totient function.
%% Use the solutions of problems P34 and P37 to compare the algorithms. Take the number of logical inferences as a measure for efficiency. Try to calculate phi(10090) as an example.
p38(M) ->
{T34, V34} = timer:tc(lp, p34, [M]),
{T37, V37} = timer:tc(lp, p37, [M]),
io:fwrite(”p34 took ~p micro seconds and returned ~p.~n”, [T34, V34]),
io:fwrite(”p37 took ~p micro seconds and returned ~p.~n”, [T37, V37]).

%% A list of prime numbers.
%% Given a range of integers by its lower and upper limit, construct a list of all prime numbers in that range.
p39(High) -> p39(1, High).
p39(Low, High) when Low > High -> p39(High, Low);
p39(Low, High) -> [X || X < - p39primes(lists:seq(2, High) , [1]), X >= Low, X =< High].

p39primes([], Primes) -> lists:reverse(Primes);
p39primes([1 | Sieve], Primes) ->      p39primes(Sieve, [1 | Primes]); % pops 1 off the sieve
p39primes([2 | Sieve], Primes) ->      p39primes([X || X < - Sieve, (X rem 2) > 0], [2 | Primes]); % pops 2 off the sieve and removes all multiples of 2
p39primes([Curr | Sieve], Primes) -> p39primes([X || X < - Sieve, (X rem Curr) > 0], [Curr | Primes]). % pops the next value off the sieve and removes all multiples

%% Goldbach’s conjecture.
%% Goldbach’s conjecture says that every positive even number greater than 2 is the sum of two prime numbers.
%% Example: 28 = 5 + 23. It is one of the most famous facts in number theory that has not been proved to be correct in the general case.
%% It has been numerically confirmed up to very large numbers (much larger than we can go with our Prolog system).
%% Write a predicate to find the two prime numbers that sum up to a given even integer.
%% Example: (goldbach 28) -> (5 23)
p40(N) -> p40goldbach(N, p39(N), []).

p40goldbach(0, _Primes, Result) when length(Result) =:= 2 -> lists:reverse(Result);
p40goldbach(N, _Primes, Result) when length(Result) =:= 2, N =/= 0 -> false;
p40goldbach(_N, [], _Result) -> false;

p40goldbach(N, [P | Primes], Result) ->
Sol = p40goldbach(N-P, Primes, [P | Result]),
case is_list(Sol) of
true -> Sol;
_else -> p40goldbach(N, Primes, Result)
end.

E-99: 21-30

Sunday, January 7th, 2007

The next installment of the 99 Lisp problems. 27 and 28 are incomplete as I have not sat down to actually work through them yet. More research is needed to do multinomial coefficients. This batch of problems posed some challenge and required a bit of research and dusting off math skills, as well as getting familiar with Erlang’s List Comprehension syntax. For your viewing pleasure:

%% Insert an element at a given position into a list.
%% Example: (insert-at ‘alfa ‘(a b c d) 2) -> (A ALFA B C D)
p21(Elem, L, Pos) -> p21insert(Elem, L, Pos, []).

p21insert(Elem, L, 1, NewL) -> lists:reverse([Elem | NewL]) ++ L;
p21insert(Elem, [H | L], Pos, NewL) -> p21insert(Elem, L, Pos-1, [H | NewL]).

%% Create a list containing all integers within a given range.
%% If first argument is smaller than second, produce a list in decreasing order.
%% Example: (range 4 9) -> (4 5 6 7 8 9)
p22(Start, End) when Start < End -> p22range_asc(End-Start, [Start]);
p22(Start, End) -> p22range_desc(Start-End, [Start]).

p22range_asc(0, L) -> lists:reverse(L);
p22range_asc(Count, [H | L]) -> p22range_asc(Count-1, [H+1, H | L]).

p22range_desc(0, L) -> lists:reverse(L);
p22range_desc(Count, [H | L]) -> p22range_desc(Count-1, [H-1, H | L]).

%% Extract a given number of randomly selected elements from a list.
%% The selected items shall be returned in a list.
%% Example: (rnd-select ‘(a b c d e f g h) 3) -> (E D A)
%% Hint: Use the built-in random number generator and the result of problem P20.
p23(L, Count) when length(L) < Count -> p23rnd_select(L, length(L), []);
p23(L, Count) -> p23rnd_select(L, Count, []).

p23rnd_select(_L, 0, RandL) -> RandL;
p23rnd_select(L, Count, RandL) ->
Rnd = random:uniform(length(L)),
V = lists:nth(Rnd, L),
p23rnd_select(p20(L, Rnd), Count-1, [V | RandL]).

%% Lotto: Draw N different random numbers from the set 1..M.
%% The selected numbers shall be returned in a list.
%% Example: (lotto-select 6 49) -> (23 1 17 33 21 37)
%% Hint: Combine the solutions of problems P22 and P23.
p24(Num, High) -> p23(p22(1, High), Num).

%% Generate a random permutation of the elements of a list.
%% Example: (rnd-permu ‘(a b c d e f)) -> (B A D C E F)
%% Hint: Use the solution of problem P23.
p25(L) -> p23(L, length(L)).

%% Generate the combinations of K distinct objects chosen from the N elements of a list
%% In how many ways can a committee of 3 be chosen from a group of 12 people? We all know that there are C(12,3) = 220 possibilities (C(N,K) denotes the well-known binomial coefficients).
%% For pure mathematicians, this result may be great. But we want to really generate all the possibilities in a list.
%% Example: (combination 3 ‘(a b c d e f)) -> ((A B C) (A B D) (A B E) … )

% Not the most efficient way, but generate the power set for the list and remove all but the sub-lists of length K
powerset([]) ->
[[]];
powerset([H | T]) ->
SubPS = powerset(T),
lists:append(SubPS, lists:map(fun(E) -> [H | E] end, SubPS)).

p26(K, L) -> [X || X < - powerset(L), length(X) == K].

%% Group the elements of a set into disjoint subsets.
%% a) In how many ways can a group of 9 people work in 3 disjoint subgroups of 2, 3 and 4 persons? Write a function that generates all the possibilities and returns them in a list.
%% Example: (group3 ‘(aldo beat carla david evi flip gary hugo ida)) -> ( ( (ALDO BEAT) (CARLA DAVID EVI) (FLIP GARY HUGO IDA) ) … )
p27([]) -> [].

%% b) Generalize the above predicate in a way that we can specify a list of group sizes and the predicate will return a list of groups.
%% Example: (group ‘(aldo beat carla david evi flip gary hugo ida) ‘(2 2 5)) -> ( ( (ALDO BEAT) (CARLA DAVID) (EVI FLIP GARY HUGO IDA) ) … )
%% Note that we do not want permutations of the group members; i.e. ((ALDO BEAT) …) is the same solution as ((BEAT ALDO) …).
%% However, we make a difference between ((ALDO BEAT) (CARLA DAVID) …) and ((CARLA DAVID) (ALDO BEAT) …).
%% You may find more about this combinatorial problem in a good book on discrete mathematics under the term “multinomial coefficients”.
p28() -> [].

%% Sorting a list of lists according to length of sublists
%% a) We suppose that a list contains elements that are lists themselves. The objective is to sort the elements of this list according to their length. E.g. short lists first, longer lists later, or vice versa.
%% Example: (lsort ‘((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o))) -> ((O) (D E) (D E) (M N) (A B C) (F G H) (I J K L))
p29(L) -> p29lsort(L).

%% code taken from Erlang Programming Examples -> List Comprehensions -> QuickSort and adapted
p29lsort([]) -> [];
p29lsort([Pivot | L]) ->
p29lsort([ X || X < - L, length(X) < length(Pivot)]) ++ [Pivot | p29lsort([ X || X <- L, length(X) >= length(Pivot)])].

%% b) Again, we suppose that a list contains elements that are lists themselves. But this time the objective is to sort the elements of this list according to their length frequency;
%% i.e., in the default, where sorting is done ascendingly, lists with rare lengths are placed first, others with a more frequent length come later.
%% Example: (lfsort ‘((a b c) (d e) (f g h) (d e) (i j k l) (m n) (o))) -> ((i j k l) (o) (a b c) (f g h) (d e) (d e) (m n))
%% Note that in the above example, the first two lists in the result have length 4 and 1, both lengths appear just once. The third and forth list have length 3 which appears twice (there are two list of this length).
%% And finally, the last three lists have length 2. This is the most frequent length.
p30(L) -> p30lfsort(L).

p30lfsort([]) -> [];
p30lfsort([H | L]) ->
Grouped = p30group(L, [[H]]),
Sorted = p29(Grouped),
p30ungroup(Sorted, []).

p30group([], Grouped) -> Grouped;
p30group([H | L], Grouped) -> p30group(L, p30insert(H, Grouped, [])).

%% insert Elem in to a group with the same length, or create a new group for the length of Elem
p30insert(Elem, [], PrevGroups) ->
lists:reverse([[Elem] | PrevGroups]);
p30insert(Elem, [ [GroupH | GroupT] | RestGrouped], PrevGroups) when length(GroupH) == length(Elem) ->
PrevGroups ++ [ [GroupH, Elem | GroupT] | RestGrouped];
p30insert(Elem, [ [GroupH | GroupT] | RestGrouped], PrevGroups) ->
p30insert(Elem, RestGrouped, [[GroupH | GroupT] | PrevGroups]).

%% remove length-specific groupings
p30ungroup([], Ungrouped) -> lists:reverse(Ungrouped);
p30ungroup([H | L], Ungrouped) when not is_list(H) -> [[H | L] | Ungrouped];
p30ungroup([Group | L], Ungrouped) -> p30ungroup(Group, []) ++ p30ungroup(L, Ungrouped).