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.
Tutorialspoint

Execute Ruby Online

=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

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.