mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-26 21:58:56 +01:00
31 lines
639 B
Ruby
31 lines
639 B
Ruby
# A ruby program for Lucas series
|
|
#
|
|
# The Lucas numbers, commonly denoted L(n) form a sequence,
|
|
# called the Lucas series, such that each number is the sum
|
|
# of the two preceding ones, starting from 2 and 1. That is,
|
|
#
|
|
# L(0) = 2, L(1) = 1
|
|
# L(n) = L(n - 1) + L(n - 2), for n > 1.
|
|
#
|
|
# Given n, calculate L(n).
|
|
# Example: 2 1 3 4 7 11 18...
|
|
# Resource: https://en.wikipedia.org/wiki/Lucas_number
|
|
|
|
def lucas(number)
|
|
golden_ratio = (1 + 5**0.5) / 2
|
|
((golden_ratio**number).round()).to_i
|
|
rescue
|
|
"Error: Provide number only!"
|
|
end
|
|
|
|
puts lucas(4)
|
|
# 7
|
|
|
|
puts lucas(3)
|
|
# 4
|
|
|
|
puts lucas("3")
|
|
# Error: Provide number only!
|
|
|
|
puts lucas(2)
|
|
# 3
|