It's important to remember that the % operator happens before +, this solves the edge cases around numbers divisible by 9 without needing extra if statements/ternaries.
For instance,
18 -> 1 + 8 = 9
however,
18 % 9 = 0
which does not fit the problem specification, so instead we do
Here's my solution in Python. Not bad considering I gave it only 5 minutes. I could have gone with reduce and lambda, but it's not really pythonic, not fast and the reading would be terrible. I like the short circuit behavior.
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
Anytime there's this sort of array summarizing is a good time for reduce
Here's a wee JS implementation of the recursive method (didn't know about the modulus one, pretty neat)
So, for numbers greater than ten, convert the number to a string (''+n), split the string into its digits ([... ''+n]), and accumulate the sum of those digits ([... ''+n].reduce((acc, char)=>acc + ~~char, 0). Then recuse another level just in case the sum is multi-digit too.
Note that squiggigle operator~~ (my term, not sure if it's got a name already) reliably converts a value to an signed 32-bit int in a bit less space than doing it properly, it's pretty much the same as the bang bang op !!, just with bitwise negation. It's used here since char is a string, ~~char get's the string's parsed number.
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
n % 9 is such an interesting property! But since I had no knowledge of it, I went with a naive solution in KOTLIN which I cracked within a few minutes:
fun digitalRoot(number: Int): Int {
return if(number < 10) {
number
} else {
val digits = number.toString().split("")
var sum = 0
digits.forEach {
val digit = it.toIntOrNull()
if(digit != null) { sum += digit}
}
digitalRoot(sum)
}
}
Property of the digital root is that the result would be
n % 9andn % 9 == 0it will be 9.Here is the Python code snippets with nest while loop:
Python 3 using direct calculation. Defined as function with test cases in the tio link.
Try it online!
why
1 + (n - 1)?It's important to remember that the % operator happens before +, this solves the edge cases around numbers divisible by 9 without needing extra if statements/ternaries.
For instance,
however,
which does not fit the problem specification, so instead we do
I tested it with just n and it works fine.
Here's my solution in Python. Not bad considering I gave it only 5 minutes. I could have gone with reduce and lambda, but it's not really pythonic, not fast and the reading would be terrible. I like the short circuit behavior.
Haskell
I've always liked this problem because of the n%9 trick, it's such a neat mathematical property.
I thought I'd do the obligatory n%9 solution:
As well as a one line recursive solution that doesn't use the n%9 trick:
Anytime there's this sort of array summarizing is a good time for
reduceHere's a wee JS implementation of the recursive method (didn't know about the modulus one, pretty neat)
So, for numbers greater than ten, convert the number to a string (
''+n), split the string into its digits ([... ''+n]), and accumulate the sum of those digits ([... ''+n].reduce((acc, char)=>acc + ~~char, 0). Then recuse another level just in case the sum is multi-digit too.Note that squiggigle operator
~~(my term, not sure if it's got a name already) reliably converts a value to an signed 32-bit int in a bit less space than doing it properly, it's pretty much the same as the bang bang op!!, just with bitwise negation. It's used here sincecharis a string,~~charget's the string's parsed number.Here's my take on the modulus one, keeping with the nsfw JS syntax theme
Python 3 (indent 3 is not reflecting in comment)
ip_val = input("Enter the number?")
if ip_val.isdigit():
while len(ip_val) > 1:
res = 0
for e in ip_val:
res += int(e)
ip_val = str(res)
print(ip_val)
n % 9is such an interesting property! But since I had no knowledge of it, I went with a naive solution in KOTLIN which I cracked within a few minutes:Here's my solution:
Simple python answer:
