On 7/17/07, Kaldrenon <kaldrenon / gmail.com> wrote:
> Hi all.
>
> I recently got into challenging myself with some of the tasks over at
> Project Euler (www.projecteuler.net), and while working on the
> solution for one of them (# 14) I came across a Ruby problem. Now, I'm
> pretty sure that this is just a dumbness on my part, but I don't know
> how to fix this problem because I see no differences between my code
> and the examples at http://www.whytheluckystiff.net/ruby/pickaxe/ for
> declaring and calling methods.
>
> Here's the code (it's tiny so I'll copy the whole thing):
>
> class Test
>   def series(n,x)
>     if (n == 1)
>       return x
>     elsif (n % 2 == 0 )
>       return sequence(n/2, x + 1)
>     else
>       return sequence(3*n + 1, x + 1)
>     end
>   end
>
>   max_num = 1
>   max_len = 1
>   for i in 2...1_000_000 do
>     my_len = series(i,1)
>     if (my_len > max_len)
>       max_len = my_len
>       max_num = i
>     end
>   end
>   print max_num , ": chain of length  " , max_len
> end
>
> The error I'm getting is this:
> undefined method `series' for Test:Class (NoMethodError)
>         from euler14.rb:14:in `each'
>
> I see no clear reason why I'm getting this error. Can you un-dumb me?
>
> Thanks in advance,
> Andrew

Andrew,

You've created an instance method, but you're calling a class method.

Try defining a class method with:

def self.series(n,x)

instead.

Also, did you notice that you're returning sequence(), rather than series()? :)

-Alex