Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63675 additions and 6796 deletions
18
Task/Digital-root/Crystal/digital-root-2.cr
Normal file
18
Task/Digital-root/Crystal/digital-root-2.cr
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
def digital_root_with_persistence(n : Int) : {Int32, Int32}
|
||||
n = n.abs
|
||||
persistence = 0
|
||||
|
||||
until n <= 9
|
||||
persistence += 1
|
||||
|
||||
digit_sum = (0..(Math.log10(n).floor.to_i)).sum { |i| (n % 10**(i + 1) - n % 10**i) // 10**i }
|
||||
|
||||
n = digit_sum
|
||||
end
|
||||
|
||||
{n, persistence}
|
||||
end
|
||||
|
||||
puts digital_root_with_persistence 627615
|
||||
puts digital_root_with_persistence 39390
|
||||
puts digital_root_with_persistence 588225
|
||||
Loading…
Add table
Add a link
Reference in a new issue