mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-25 21:58:57 +01:00
Merge pull request #150 from msaf9/master
This commit is contained in:
commit
3f8a50a2e7
2 changed files with 32 additions and 0 deletions
|
@ -98,6 +98,7 @@
|
|||
* [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/maths/fibonacci.rb)
|
||||
* [Find Max](https://github.com/TheAlgorithms/Ruby/blob/master/maths/find_max.rb)
|
||||
* [Find Min](https://github.com/TheAlgorithms/Ruby/blob/master/maths/find_min.rb)
|
||||
* [Lucas Series](https://github.com/TheAlgorithms/Ruby/blob/master/maths/lucas_series.rb)
|
||||
* [Number Of Digits](https://github.com/TheAlgorithms/Ruby/blob/master/maths/number_of_digits.rb)
|
||||
* [Power Of Two](https://github.com/TheAlgorithms/Ruby/blob/master/maths/power_of_two.rb)
|
||||
* [Prime Number](https://github.com/TheAlgorithms/Ruby/blob/master/maths/prime_number.rb)
|
||||
|
|
31
maths/lucas_series.rb
Normal file
31
maths/lucas_series.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
# 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
|
Loading…
Reference in a new issue