Today I started working on the problems on the Project Euler website. Which I first heard of the other day. Since I want to learn Ruby, thought it would be the best to approach the problems using it. I’m gonna be posting regularly about my progress and, naturally, my subsequent progress with ruby. I also created a GitHub repository to share the source.

The Problem (#1)

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum >of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Being a new comer to ruby, my first approach was a bit odd, considering the ruby style of doing things.

count = 0 # Counter variable
result = 0 # A variable to hold the result

1000.times do # Call this block a 1000 times
result += count if count % 3 == 0 # Add count if it's multiple of 3
        || count % 5 == 0 # or or 5
count += 1  # increase the counter
end

puts result # print the result


This code outputs the correct answer, but I don’t think it looks very nice. Besides, it reminded me of Pascal. I checked some solutions from other people using ruby and one of them got my attention.

p (1...1000).select {|i| i % 3 == 0 II i % 5 == 0 }.inject(0) {|r, i| r + i}

# which is the same as:

p (1...1000).reject {|i| i % 3 != 0 && i % 5 != 0}.inject {|r, i| r + i}


Comparing these peaces of code to my first approach we see a whole lot of differences. Lets not consider the size of the snippets, which is obviously different. Lets try to see how the code works, or reads.

The first peace of code counts (loops) through a block from 1 to 1000, verifies the counter (current element) matches the criteria (is it multiple of 3 or 5?), in the case it does it’s added to the the variable holding the result.

With the second approach we see a significant reduction of size, but it’s also more appealing. We can even read it out laud.

"In a range of 1 to 999 select those values that are multiples of 3 or 5 and then inject them into a block that adds them”

Though I could read and interpret what the code was doing I had to investigate about ranges, the select and inject methods. That is how I arrived at the opposed version of the previous snippet, which does exactly the same except that it excludes (reject) the values that does not match the criteria.

Ruby is a very powerful language, It’s simple and smart. With this example I got a glance of its versatility. I’ll keep you posted as I progress through the problems.

Cheers,

Sam Witwicky (Shia LaBeouf) and Carlty (Rosie Huntington-Whiteley) his new girlfriend

I enjoyed Transformers. In fact, I didn’t even notice the movie was bad until later. I was so excited watching Optimus and Ironhide that I didn’t mind when Prime said he found Sam Witwicky (Shia LaBeouf) through eBay. I forgot most of Revenge of the Fallen; barely remember a Decepticon climbing the great pyramid which was just awful. At least had Megan Fox.

Transformers: Dark of the Moon is very different. Is not a movie, I can say. Is a carnival, a celebration of stupidity and incoherence. A blatant mockery on the moviegoers. Sincerely, the most exasperating 150 minutes, didn’t make it to the last scenes. But I guess Buzz Aldrin did not know.

Just like I didn’t know either if there was an actual plot in the film. Apparently Michael Bay and Ehren Kruger had an idea involving Apollo 11, Chernobyl and the Trump International Hotel and Tower. They got $195 millions dollars and went through with it. I’m sure they didn’t even ask an astrophysics before thinking of teleporting a planet into our exosphere. Absolute madness, some sort of vulgar comedy mixed up with a hottie (Rosie Huntington-Whiteley), a lot of special effects and useless characters without persona, that is what this movie is made of.

A little bit of light came from Bruce Brazos (John Malkovich), Sam’s boss who was genuinely funny. Sad his appearance was kind of short. John Turturro was also entertaining during some point but took big part into the senseless madness as well.

Definitely, we’ve seen the darker side of transformers, not the moon’s. These people only cared about their $43,225,329 and this is the film they understood they had to make to achieve them. Congratulations!

I know you may think I did not speak about the movie, but trust me is not necessary. If you want to go watch it, go. I did. I just don’t think your reaction will be any different than mine if your passionate about movies.

Always remember, you can actually loose your faith in Michael Bay.


Today I got my invitation for Google+ and felt so good. I’m not a social network person. I go to Facebook check the updates and bounce. But I spent more time on Google than I do on any other site. I use Google Chrome, Gmail, Buzz, Calendar, Gtalk, Google Apps and Youtube on a daily basis and even though I use Facebook, I still upload my pictures to Picasa. I am totally Googlized.

What I’m trying to say is I have plain faith that this social network will come handy to centralize all my Google experience. But I’m still sure they may have some more things in mind.

I’ll keep you posted as I explore and expect a review as soon as I can.

Cheers,

Archive | Subscribe | Ask me anything 1/4 Older Theme: Clean Detail by PinchHost