I would prefer to use

  total += num2 if num2.even?

when I am testing for even-ness of a number.

Hope this helps,

Mike

On Nov 6, 2013, at 11:24 AM, Rafael M. <lists / ruby-forum.com> wrote:

> I did a test in a job interview and one of the questions was something
> like
> make an algorithm that prints the sum of the first 4 million even
> numbers
> of fibonacci sequence.
> 
> in the test I wrote pseudo code, but now I'm trying to test my logic
> with ruby:
> 
> num1 = 1
> num2 = 1
> next_number = 0
> total = 0
> 
> 1.upto(4_000_000) do
>  next_number = num1 + num2
>  num1 = num2
>  num2 = next_number
>  total += num2 if((num2 % 2) == 0)
> end
> 
> puts total
> 
> is there any way to have the total value?
> is this a good code or there is a clever way to write it?
> 
> -- 
> Posted via http://www.ruby-forum.com/.

-- 

Mike Stok <mike / stok.ca>
http://www.stok.ca/~mike/

The "`Stok' disclaimers" apply.