Forum: Ruby Is there any way to get this result?

99dc80d1918a1b49d6f44d83149ce0a2?d=identicon&s=25 Rafael M. (rafael_m)
on 2013-11-06 17:24
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?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-11-06 18:18
(Received via mailing list)
On Wed, Nov 6, 2013 at 5:24 PM, Rafael M. <lists@ruby-forum.com> wrote:
> num2 = 1
> 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?

I think the code is OK - except your looping condition. It does not
match the problem description.

Kind regards

robert
20753db11b9ffec13351e7fc2ddfbf7e?d=identicon&s=25 Ricky N. (ricky_n)
on 2013-11-06 18:49
(Received via mailing list)
I don't have a working copy of ruby on me, but I feel like a more
Rubyish
way of implementing it would be the following. The hacky bit with the
hash
is to get a memoized collection of Fibonacci numbers (source:
http://stackoverflow.com/questions/6418524/fibonac...). I had a
pretty awesome mind blown feeling when I saw that answer the first time.

n = 4,000,000
fib = Hash.new{ |h,k| h[k] = k <= 1 ? k : h[k-1] + h[k-2] }
fib.values_at(*(0..n).step(2)).inject(:+)




On Wed, Nov 6, 2013 at 11:17 AM, Robert Klemme
20753db11b9ffec13351e7fc2ddfbf7e?d=identicon&s=25 Ricky N. (ricky_n)
on 2013-11-06 18:56
(Received via mailing list)
Oh bah, I did it for every other value of the fib sequence instead of
the
even values. Take out the step and do a more complex inject.
99dc80d1918a1b49d6f44d83149ce0a2?d=identicon&s=25 Rafael M. (rafael_m)
on 2013-11-06 19:17
Robert Klemme wrote in post #1126613:
> On Wed, Nov 6, 2013 at 5:24 PM, Rafael M. <lists@ruby-forum.com> wrote:
>> num2 = 1
>> 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?
>
> I think the code is OK - except your looping condition. It does not
> match the problem description.
>
> Kind regards
>
> robert

Yes I expressed myself bad
the problem was really to loop through 4 million numbers of fibonacci
and if the current number was even you should add to the total,
the interviewer checked my pseudo code and found it correct

thanks for the replies
2ffac40f8a985a2b2749244b8a1c4161?d=identicon&s=25 Mike Stok (Guest)
on 2013-11-07 12:48
(Received via mailing list)
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:

> num2 = 1
> 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.
Ddbfebb47432f6599da361df6a135c7c?d=identicon&s=25 Adam Shelly (Guest)
on 2013-11-07 13:22
(Received via mailing list)
(2..n).step(3) should get you all the even fib values.

-Adam
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.