Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
=begin
def triple_sequence(start, length)
result = [start]
n = 0
while n < length - 1
result << result [n] * 3
n += 1
end
return result
end
=end
def fibonacci(length)
n = 0
result = [1, 1]
if length == 0
return []
elsif length == 1
return [1]
end
while result.length < length
last = result[-1]
sec_last = result[-2]
result << last + sec_last
end
result.shift
return result
end
print triple_sequence(2, 4) # => [2, 6, 18, 54]
puts
print triple_sequence(4, 5) # => [4, 12, 36, 108, 324]
puts
=begin
def greatest_factor_array(arr)
result = []
arr.each do |element|
if element % 2 == 0
result << divides(element)[-2]
else
result << element
end
end
return result
end
def divides(num)
n = num
dividers_array = []
while n > 0
if num % n == 0
dividers_array << n
end
n -= 1
end
return dividers_array.sort!
end
=end
def greatest_factor_array(array)
result = array.map do |i|
if i % 2 == 0
greatest_factor(i)
else
i
end
end
return result
end
def greatest_factor(num)
(1...num).reverse_each do |n|
if num % n == 0
return n
end
end
end
#puts greates_factor(16)
print greatest_factor_array([16, 7, 9, 14]) # => [8, 7, 9, 7]
puts
print greatest_factor_array([30, 3, 24, 21, 10]) # => [15, 3, 12, 21, 5]
puts