mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-25 21:58:57 +01:00
3n plus 1 feature implementation
This commit is contained in:
parent
29a069a3bf
commit
d7b1bc0b73
2 changed files with 43 additions and 0 deletions
|
@ -79,6 +79,7 @@
|
|||
* [Fibonacci](https://github.com/TheAlgorithms/Ruby/blob/master/dynamic_programming/fibonacci.rb)
|
||||
|
||||
## Maths
|
||||
* [3N Plus 1 or Collatz Conjecture](https://github.com/TheAlgorithms/Ruby/blob/master/maths/3nPlus1.rb)
|
||||
* [Abs Max](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_max.rb)
|
||||
* [Abs Min](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs_min.rb)
|
||||
* [Abs](https://github.com/TheAlgorithms/Ruby/blob/master/maths/abs.rb)
|
||||
|
|
42
maths/3nPlus1.rb
Normal file
42
maths/3nPlus1.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
# A ruby program for 3N plus 1
|
||||
# 3N plus 1 is also called as Collatz Conjecture
|
||||
#
|
||||
# f(n) => n / 2 (if n = 0 (mod 2))
|
||||
# f(n) => (3n + 1) (if n = 1 (mod 2))
|
||||
#
|
||||
# Wiki: https://en.wikipedia.org/wiki/Collatz_conjecture
|
||||
|
||||
def collatz_conjecture(number)
|
||||
n = number
|
||||
nums = Array.new
|
||||
nums.push number
|
||||
while number > 1
|
||||
if number % 2 == 0
|
||||
number /= 2
|
||||
nums.push number
|
||||
else
|
||||
number = 3 * number + 1
|
||||
nums.push number
|
||||
end
|
||||
end
|
||||
"The 3N + 1 series of #{n} is #{nums.to_s}."
|
||||
rescue
|
||||
"Error: Please provide number only!"
|
||||
end
|
||||
|
||||
# Valid inputs
|
||||
puts collatz_conjecture(12)
|
||||
# The 3N + 1 series of 12 is [12, 6, 3, 10, 5, 16, 8, 4, 2, 1].
|
||||
|
||||
puts collatz_conjecture(6)
|
||||
# The 3N + 1 series of 6 is [6, 3, 10, 5, 16, 8, 4, 2, 1].
|
||||
|
||||
puts collatz_conjecture(100)
|
||||
# The 3N + 1 series of 100 is [100, 50, 25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1].
|
||||
|
||||
# Invalid inputs
|
||||
puts collatz_conjecture("12")
|
||||
# Error: Please provide number only!
|
||||
|
||||
puts collatz_conjecture("a")
|
||||
# Error: Please provide number only!
|
Loading…
Reference in a new issue