Confident Ruby

Leave a comment
Programming

I just finished reading Confident Ruby by Avdi Grimm.

I’ve been writing a gem that contains a class that initializes with a very specific argument — an array of twelve strings, each of them having their own specific requirements. It’s the kind of stuff that is very easy to get wrong, as well. I had quickly perused the book before, so I had an idea of the subject matter. I wanted to handle edge cases and incorrect parameters quickly and cleanly, one of many things addressed by the book. It seemed like the perfect time to read it in depth.

I’m glad I did. The code in that class is much shorter and clearer than it would have been otherwise. The book does a much better job than most at addressing the “why” and not just the “how”. Overall, I’d highly recommend it.

Project Euler #48 in Racket

Leave a comment
Programming

Problem #48 at Project Euler is as follows:

The series, 11 + 22 + 33 + … + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + … + 10001000.

My solution in Racket:

At this point, using Racket almost feels like cheating. To be fair, this just calculates the entire number; I just looked at the last ten digits myself. That would be fairly trivial to do, though.

Project Euler #30 in Racket

Leave a comment
Programming

Problem #30 at Project Euler is as follows:


Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.


My solution in Racket:

I desperately wanted to name that second function power-of-love , but that wouldn’t have made any sense. Someday it will, but that day is not today.

Anyway, the heavy lifting is in that last for/sum . It checks each number between 2 and 355,000 to see which ones are the sum of the fifth powers of their digits, and then adds them together. 355,000 isn’t arbitrary, but I’ll leave the reasoning why as an exercise for the reader.

Project Euler #25 in Ruby

Leave a comment
Programming

Problem 25 at Project Euler is as follows:

The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1.

Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144

The 12th term, F12, is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?

My solution in Ruby:

There’s probably a cleaner way to do this in Ruby, but this works. I start with an array and I keep adding numbers from the Fibonacci sequence until the most recent addition is 1000 digits long. The answer is the size of the resulting array.

Project Euler #16 in Ruby

Leave a comment
Programming

Problem #16 at Project Euler is as follows:

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 21000?

My solution in Ruby:

It’s fairly straightforward, as are most of my Project Euler solutions (or at least I hope they’re straightforward). Find 2 to the 1000th, turn that number into an array of its integers, then add them all up. I haven’t attempted this in Racket because it would be very much the same solution, but I think I will soon anyway, just for the practice.

Project Euler #8 in Racket

Leave a comment
Programming

Problem #8 at Project Euler was as follows:

The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

(A giant, 1000 digit number)

Find the five adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

In the month since I solved this problem, they have altered it. They are now asking for the greatest product of thirteen consecutive digits. I don’t know why anyone would bother changing it after 10 years, but whatever. I suppose they are just trying to remind us that things change, and that the the slow, inexorable march of time will eventually claim us all.

So on that note, my solution!

The last line puts everything together. I turn the integer into a list, I make a new list of every product of five consecutive digits, I sort that list of products in descending order, and I take the first one. Done and done.

Project Euler #5 in Racket

Leave a comment
Programming

Problem 5 is as follows:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

My answer in Racket:

I’m sure there is some elegant, concise way to use the power of math to find the solution to this question. Or, you could just check by brute force the first 12 million multiples of 20. Guess which one I did.

iOS Development

Leave a comment
Programming

Between the recent Rubymotion #inspect conference and WWDC, I’m finding myself playing with iOS development more and more. I’ll continue to work on my rails application, but not to the exclusion of iOS. Plus, who knows? Maybe I’ll become a renown Swift expert. You’ll be able to say you knew of me before the riches and fame went to my head. Unless, of course, you’re reading this in the future, and this post is on the giant sign by the entrance to the museum dedicated to me, in which case: Welcome to the Museum!

Rubymotion #inspect 2014

Leave a comment
Misc

I’m going to a RubyMotion conference in San Francisco tomorrow and the day after. I haven’t really been focusing much on RubyMotion lately, but the conference is local to me this year and I’d like to start working with iOS again, so it seemed like a great opportunity.

This will also be the first tech conference I’ve ever attended. I’m not sure what the dress code is for this sort of thing. I own a tux that’s just been taking up space in the closet, so I’ll probably wear that.

Project Euler #6 in Racket

Leave a comment
Programming

Problem #6 at projecteuler.net is as follows:

The sum of the squares of the first ten natural numbers is 12 + 22 + … + 102 = 385

The square of the sum of the first ten natural numbers is (1 + 2 + … + 10)2 = 552 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640. Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

My solution in Racket:

This was pretty trivial. Line 4 is primarily responsible for creating the sum of the first 100 natural numbers with build-list  then adding them together. The next line also uses build-list  to create a list of 100 values, but the function passed to it returns the square of each number instead of the number itself like values  does. After those two lists are built, the rest falls into place, and it’s all over but the crying.