This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
has_many :notes | |
end | |
class UserSerializer < ActiveModel::Serializer | |
attributes :id, :name | |
has_many :notes | |
end | |
``` | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def nth_fibonacci(number) | |
return 1 if [0, 1].include? number | |
nth_fibonacci(number - 2) + nth_fibonacci(number - 1) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gcd(x, y) | |
loop do | |
r = x % y | |
return y if r.zero? | |
x, y = y, r | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gcd(nth_fibonacci(n), nth_fibonacci(n+1)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> gcd(nth_fibonacci(32), nth_fibonacci(33)) | |
=> 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def array_of_fibonaccis(amount) | |
fibonacci_numbers = [1, 1] | |
return [1] if amount < 2 | |
(amount - 2).times { (fibonacci_numbers << fibonacci_numbers[-2..-1].inject(&:+)) } | |
fibonacci_numbers | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def neighbour(n, m) | |
return unless (n - m).abs == 1 && n < m | |
array_of_fibonaccis(m + 1)[n..m] | |
end |
OlderNewer