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?
I think the code is OK - except your looping condition. It does not
match the problem description.
Kind regards
robert
···
On Wed, Nov 6, 2013 at 5:24 PM, 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?
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?
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/fibonacci-one-liner\). 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 <shortcutter@googlemail.com>wrote:
On Wed, Nov 6, 2013 at 5:24 PM, 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?
I think the code is OK - except your looping condition. It does not
match the problem description.
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.
···
On Wed, Nov 6, 2013 at 11:49 AM, Ricky Ng <dummey@gmail.com> wrote:
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/fibonacci-one-liner\). 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 <shortcutter@googlemail.com > > wrote:
On Wed, Nov 6, 2013 at 5:24 PM, 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?
I think the code is OK - except your looping condition. It does not
match the problem description.
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
(2..n).step(3) should get you all the even fib values.
-Adam
···
On Nov 6, 2013 12:55 PM, "Ricky Ng" <dummey@gmail.com> wrote:
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.
On Wed, Nov 6, 2013 at 11:49 AM, Ricky Ng <dummey@gmail.com> wrote:
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/fibonacci-one-liner\). 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 < >> shortcutter@googlemail.com> wrote:
On Wed, Nov 6, 2013 at 5:24 PM, 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?
I think the code is OK - except your looping condition. It does not
match the problem description.