TheAlgorithms-Ruby/maths/lucas_series.rb

32 lines
645 B
Ruby
Raw Normal View History

2021-05-15 16:43:21 +02:00
# 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
2021-09-03 22:24:58 +02:00
(golden_ratio**number).round.to_i
rescue StandardError
'Error: Provide number only!'
2021-05-15 16:43:21 +02:00
end
puts lucas(4)
# 7
puts lucas(3)
# 4
2021-09-03 22:24:58 +02:00
puts lucas('3')
2021-05-15 16:43:21 +02:00
# Error: Provide number only!
puts lucas(2)
# 3