December 2009
6 posts
4 tags
Insert and Delete for Javascript Arrays
Javascript is an awesome language but lacks some useful API’s such as for Arrays. Insert and delete at a particular position are two very basic array operations that can be done by using the splice method. I didn’t even know what splice meant till I bumped across Javascript Arrays. Now Array can be easily extended to do that using prototype, but a better home for such methods and...
Dec 28th
1 note
3 tags
Project Euler Problem 24
Used the algorithm on Wikipedia at http://en.wikipedia.org/wiki/Permutation#Lexicographical_order_generation. It’s a generic method that takes any arbitrary list and the desired permutation number k. Problem Statement 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...
Dec 24th
3 tags
Project Euler Problem 23
This problem required generating all “abundant” numbers below the specified limit, and then canceling out those that could not be reached by adding up any two abundant numbers from the generated list. A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28,...
Dec 24th
1 note
4 tags
One liner for Project Euler problem 20
I was needlessly worrying about about the capabilities of Bignum in being able to handle this factorial which has 158 digits in its result. Wasn’t an issue at all though. Ruby certainly brings conciseness to the code. It maybe possible in a lot lesser characters in J or other functional languages, but this is good enough for me for the time being. The problem statement is Find the sum of...
Dec 23rd
3 tags
Project Euler Problem 17
This is a good way to learn Ruby by solving some problems from Project Euler. Here’s my solution to problem 17. Link to the problem - http://projecteuler.net/index.php?section=problems&id=17 and the problem text and solution are below: If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the...
Dec 23rd
3 notes
Dec 6th
1 note