19
1
This is different from My Word can beat up your Word as it is less complex and only requires you to calculate it, and not compare them.
To find the digital root, take all of the digits of a number, add them, and repeat until you get a one-digit number. For example, if the number was 12345
, you would add 1
, 2
, 3
, 4
, and 5
, getting 15
. You would then add 1
and 5
, giving you 6
.
Your task
Given an integer N (0 <= N <= 10000) through STDIN, print the digital root of N.
Test cases
1 -> 1
45 -> 9
341 -> 8
6801 -> 6
59613 -> 6
495106 -> 7
Remember, this is code-golf, so the code with the smallest number of bytes wins.
Can we take the input as a string? – Daniel – 2016-10-27T14:02:57.220
@Dopapp Yes.... – Oliver Ni – 2016-10-27T14:05:06.707
This is called a digital root.
– James – 2016-10-27T14:05:10.107Related. – Oliver Ni – 2016-10-27T14:07:03.320
Related. – Oliver Ni – 2016-10-27T14:07:11.527
1
Maybe a subtask of this challenge.
– nimi – 2016-10-27T14:17:38.5403Very closely related to this challenge ... maybe close enough for a dupe. – AdmBorkBork – 2016-10-27T14:22:01.520
@nimi That's why I had the feeling I had done something like this before.. – Kevin Cruijssen – 2016-10-27T14:24:49.930
8Please be more precise when saying
number
. In particular. must input0
be supported? – Ton Hospel – 2016-10-27T14:28:40.7202@TimmyD I think that this one is the much cleaner challenge without adding letter to integer conversion, computing the function for two values and including the literal
STALEMATE
. It might be better to close the other one as a dupe of this. – Martin Ender – 2016-10-27T14:42:37.3901
@MartinEnder Perhaps. Points out another reason why using the Sandbox is encouraged.
– AdmBorkBork – 2016-10-27T14:47:13.8503@MartinEnder I retracted my close vote, I think it's unfair to close a good challenge as a dupe of another more complex challenge. – Erik the Outgolfer – 2016-10-27T14:48:47.743
You should add 10 as a test case. My algorithm was working fine for all your test cases, but was wrong for 10! – sergiol – 2018-04-10T14:12:33.103