Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Digital-root/Crystal/digital-root-1.crystal
Normal file
13
Task/Digital-root/Crystal/digital-root-1.crystal
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
def digital_root(n : Int, base = 10) : Int
|
||||
max_single_digit = base - 1
|
||||
n = n.abs
|
||||
if n > max_single_digit
|
||||
n = 1 + (n - 1) % max_single_digit
|
||||
end
|
||||
n
|
||||
end
|
||||
|
||||
puts digital_root 627615
|
||||
puts digital_root 39390
|
||||
puts digital_root 588225
|
||||
puts digital_root 7, base: 3
|
||||
18
Task/Digital-root/Crystal/digital-root-2.crystal
Normal file
18
Task/Digital-root/Crystal/digital-root-2.crystal
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
|
||||
18
Task/Digital-root/Crystal/digital-root-3.crystal
Normal file
18
Task/Digital-root/Crystal/digital-root-3.crystal
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
def digital_root_with_persistence_to_s(n : Int) : {Int32, Int32}
|
||||
n = n.abs
|
||||
persistence = 0
|
||||
|
||||
until n <= 9
|
||||
persistence += 1
|
||||
|
||||
digit_sum = n.to_s.chars.sum &.to_i
|
||||
|
||||
n = digit_sum
|
||||
end
|
||||
|
||||
{n, persistence}
|
||||
end
|
||||
|
||||
puts digital_root_with_persistence_to_s 627615
|
||||
puts digital_root_with_persistence_to_s 39390
|
||||
puts digital_root_with_persistence_to_s 588225
|
||||
Loading…
Add table
Add a link
Reference in a new issue