Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Digital-root/Ruby/digital-root.rb
Normal file
24
Task/Digital-root/Ruby/digital-root.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
class String
|
||||
def digroot_persistence(base=10)
|
||||
num = self.to_i(base)
|
||||
persistence = 0
|
||||
until num < base do
|
||||
num = num.digits(base).sum
|
||||
persistence += 1
|
||||
end
|
||||
[num.to_s(base), persistence]
|
||||
end
|
||||
end
|
||||
|
||||
puts "--- Examples in 10-Base ---"
|
||||
%w(627615 39390 588225 393900588225).each do |str|
|
||||
puts "%12s has a digital root of %s and a persistence of %s." % [str, *str.digroot_persistence]
|
||||
end
|
||||
puts "\n--- Examples in other Base ---"
|
||||
format = "%s base %s has a digital root of %s and a persistence of %s."
|
||||
[["101101110110110010011011111110011000001", 2],
|
||||
[ "5BB64DFCC1", 16],
|
||||
["5", 8],
|
||||
["50YE8N29", 36]].each do |(str, base)|
|
||||
puts format % [str, base, *str.digroot_persistence(base)]
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue