Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
999 --> 4 (because 999 = 729, 729 = 126, 126 = 12, and finally 1*2 = 2)
4 --> 0 (because 4 is already a one-digit number)
def persistence(n)
return 0 if n < 10
mult = n.digits.reduce(:*)
count = 1
until mult < 10 do
mult = mult.digits.reduce(:*)
count += 1
end
count
end
# From Ruby 3.0, RSpec is used under the hood.
# See https://rspec.info/
# Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
describe "Persistence" do
it "Basic tests" do
Test.assert_equals(persistence(39),3)
Test.assert_equals(persistence(4),0)
Test.assert_equals(persistence(25),2)
Test.assert_equals(persistence(999),4)
Test.assert_equals(persistence(444),3)
end
end