Assignment 2: The Facets of Ruby, Part I
ProblemsPage 1 of 5

1. Password Lost

You: “Phew. That was a lot of Ruby.”

Al: “So yeah, XXX, I'd like you to work on something.

Password Protected?

Al continues. “So we realized that the Jel who wrote the code for spaceship Houki Boshi left his laptop computer. Here it is.

“We believe that there might be some source code left on his computer. However, his computer is password protected.

But you notice something: “But hey, there's a password hint!”

Password Hint

The password hint looks cryptic:

The sequence is:

h is the first, fifth, sixteenth, ... letter in this sentence, not counting spaces, dashes or commas. (The word "and" is never used.)

The sequence therefore is 1, 5, 16, 25, 36...

The password is the 7716 th Prime in this sequence.

Al: Does this make sense to you?

You: Hmm...oh, there's something else after the hint:

(Note to self) This question is stolen from 15-251, Spring 2009. If *you* actually solved this question, feel free to re-use your code (any language is fine).

But if you didn't do anything and had your teammates solve this, you might as well try it by yourself now :P

Email hw AT cmurpg.com the answer and the zip of the code to complete this assignment.

You: Okay, this part doesn't make any sense...but I think I know how to solve the original question.

Al: Great! This is why we hired you!

Cracking the Code

The sequence is:

h is the first, fifth, sixteenth, ... letter in this sentence, not counting spaces, dashes or commas. (The word "and" is never used.)

You: Here's probably how it works. First, this is the original sentence.

Because we don't count commas or spaces, let's remove them.

Now, let's see where h's are located.

And here's the index of each h's, assuming it begins from 1.

So far, we have “first,” “fifth,” and “sixteenth.” And the last h appears at 25th position. So therefore, we add “twentyfifth” to the sequence.

Now, another h appears again.

And the position is 36th.

So we add “thirtysixth” to the sentence.

We continue this process and keep track of indices of h. Then we get sequence 1, 5, 16, 25, 36,... etc. The next one looks like 38.

The password is the 7716 th Prime in this sequence.

So we just need to find the 7716th prime in the sequence. The first prime seems to be 5.

Using Ruby

Al: Excellent! Now, how can we find that prime number?

Oha: XXX, since you learned some Ruby, why don't you write a Ruby code to solve this problem? I'll help you out.

You: Sure!

Next Page

Using Ruby, this problem can easily be solved within 30 minutes. Let's see how. Next Page

Assignment 2:
The Facets of Ruby, Part I

Problems