19
\$\begingroup\$

Also known as the [analog root]

(Opposite of the digital root!) ;)

The digital root of a number is the continuous summation of its digits until it is a single digit, for example, the digital root of 89456 is calculated like this:

8 + 9 + 4 + 5 + 6 = 32

3 + 2 = 5

The digital root of 89456 is 5.

Given a digit as input via STDIN, print/return all of the possible two digit numbers that have that digital root. If you need it to, it can include itself, e.g. 05

These are all of the possible inputs and outputs:

(You get to choose whether or not to include the leading zero for the digit itself)

I/O

0 => 0 or 00 or nothing

1 => 01 and/or 1, 10, 19, 28, 37, 46, 55, 64, 73, 82, 91 - Make sure that 1 does not return 100

2 => 02 and/or 2, 11, 20, 29, 38, 47, 56, 65, 74, 83, 92

3 => 03 and/or 3, 12, 21, 30, 39, 48, 57, 66, 75, 84, 93

4 => 04 and/or 4, 13, 22, 31, 40, 49, 58, 67, 76, 85, 94

5 => 05 and/or 5, 14, 23, 32, 41, 50 ,59, 68, 77, 86, 95

6 => 06 and/or 6, 15, 24, 33, 42, 51, 60, 69, 78, 87, 96

7 => 07 and/or 7, 16, 25, 34, 43, 52, 61, 70, 79, 88, 97

8 => 08 and/or 8, 17, 26, 35, 44, 53, 62, 71, 80, 89, 98

9 => 09 and/or 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99

No standard loopholes, and it's , so the shortest answer in bytes wins.

Congrats to Heeby Jeeby Man on his amazing 46 byte brain-flak answer!

\$\endgroup\$
10
  • 1
    \$\begingroup\$ does the number itself count as a two digit number? (05)? \$\endgroup\$ May 29, 2017 at 0:44
  • 2
    \$\begingroup\$ Inverse challenge \$\endgroup\$ May 29, 2017 at 0:48
  • 5
    \$\begingroup\$ What should the output be for 0? And again, in a case like this where there are only 10 possible inputs, it would be of great benefit to supply the outputs in your challenge. \$\endgroup\$ May 29, 2017 at 0:50
  • 1
    \$\begingroup\$ Your decision on how to handle zero invalidates many of the answers that were posted. It would be considerate to let the participants know you have made a decision. \$\endgroup\$ May 29, 2017 at 2:48
  • 2
    \$\begingroup\$ is the opposite of a digital root an analog root? \$\endgroup\$
    – tuskiomi
    May 29, 2017 at 3:32

33 Answers 33

10
\$\begingroup\$

JavaScript (ES6), 27 31 30 bytes

Returns 0 for 0 or an array of solutions otherwise.

n=>n&&[...1e9+''].map(_=>n+=9)

Demo

let f =

n=>n&&[...1e9+''].map(_=>n+=9)

for(n = 0; n < 10; n++) {
  console.log(n, JSON.stringify(f(n)));
}

\$\endgroup\$
5
  • 3
    \$\begingroup\$ A downvote without any comment doesn't help much to improve the answer... \$\endgroup\$
    – Arnauld
    May 29, 2017 at 7:27
  • \$\begingroup\$ To some it might be unclear which part is the actual codegolfed function, and which part is a demonstration. It might be a good idea to just put the function itself straight below the JavaScript line. \$\endgroup\$ May 29, 2017 at 20:09
  • \$\begingroup\$ @DavidMulder Thanks for the suggestion. That's actually the way I answer most of the time. Updated. \$\endgroup\$
    – Arnauld
    May 29, 2017 at 20:24
  • \$\begingroup\$ Nice solution! Sorry for dragging up an old solution but could you drop the + to save another byte? Though it would not work with stdin string that way I suppose. \$\endgroup\$
    – Craig Ayre
    Jan 12, 2018 at 11:03
  • \$\begingroup\$ @CraigAyre I'm not sure where this + came from... Updated. Thanks! \$\endgroup\$
    – Arnauld
    Jan 12, 2018 at 11:25
8
\$\begingroup\$

05AB1E, 13 12 9 bytes

-3 bytes thanks to Adnan

тL<ʒSOSOQ

Try it online!

Explanation

тL<ʒSOSOQ   Main link. Argument n
тL<         List from 1 to 100, then decrement to get 0 to 99
   ʒ        Filter
    SOSO    Sum of all chars, twice
        Q   Compare to input
\$\endgroup\$
6
  • \$\begingroup\$ I think you can replace the infinite loop by SOSO, since the number will never be bigger than 99. \$\endgroup\$
    – Adnan
    May 29, 2017 at 10:32
  • \$\begingroup\$ @Adnan Don't think, I assure you he can. \$\endgroup\$ May 29, 2017 at 10:46
  • 1
    \$\begingroup\$ тL< isn't really shorter than plain 99Ý. ;) \$\endgroup\$ May 29, 2017 at 10:48
  • 1
    \$\begingroup\$ @EriktheOutgolfer Well, I was probably trying to hard to save a byte there ;D \$\endgroup\$
    – kalsowerus
    May 29, 2017 at 10:54
  • 2
    \$\begingroup\$ Main "link"? Since when 05AB1E has links? It's not Jelly. \$\endgroup\$ May 30, 2017 at 3:11
7
\$\begingroup\$

Jelly, 8 bytes

11Ḷ×9+ȧ@

Try it online!

Different algorithm than my other answer.

\$\endgroup\$
7
+50
\$\begingroup\$

Brain-Flak, 46 bytes

{((()()()()()){}){(({}[()])<(({}{}[]))>)}}{}{}

Try it online!

Explanation

This answer uses an idea from Megatom's answer namely using the stack height as the difference between the loop counter and the increment. Like previous answers this answer has a large outer loop to catch all the zeros. Inside the loop we push 10 to act as a counter, we then start another nested loop. In this loop we decrement the counter by 1

({}[()])

Then we pop the top two items, which are the counter and the last item we calculated. We add these to the stack height in order to counter balance the decrementation, we then push this twice, once for output and once so that it can be consumed to calculate the next result. Pushing things twice means we accidentally push an additional value which needs to be removed at the end of execution.

The reason this just barely beats Megatom is Megatom's answer is forced to get its stack heights while the last result is still on the stack. This means they are forced to use a rather expensive [()] to decrease the total by one. By moving the duplicate to the end of the loop I am able to avoid having to use [()] at the cost of an additional {} at the very end of the program. If Megatom were to use this strategy his answer would look like:

{<>((()()()()()){}){((({}[()])<>{}[]))<>}}<>{}

also 46 bytes.

Brain-Flak, 52 bytes

{((()()()()()){}){({}[()]<(({})((()()())){}{})>)}}{}

Try it online!

Explanation

The main outerloop makes a special case for input of zero. If zero is input we jump over the entire loop, pop zero and then output nothing. Otherwise we enter the loop. Here we push loop 10 times each time adding 9 to the top of the stack, keeping old values. Since 9 preserves digital sums this will get us the next value. Once the loop has expired we use the zero it generated to exit the loop which is then popped by the {} at the end.

Brain-Flak, 56 bytes

{([(((()()())){}{})]){({}()<(({})<(({}{}))>)>)}}{}({}{})

Try it online!

Explanation

This version works very similarly to the last one, except we loop 9 times instead of 10 leaving out the original value. In order to do this we have to rearrange the way we handle memory a bit. All of the bytes we might have saved using this method get put into cleanup.

\$\endgroup\$
3
  • \$\begingroup\$ The 46 doesn’t preserve the original number :( \$\endgroup\$
    – Jo King
    Jan 15, 2018 at 2:28
  • \$\begingroup\$ @JoKing Yeah, it just does the 2 digit cases. Which I think is kind of the intention of the question, so that makes me pretty happy. \$\endgroup\$
    – Wheat Wizard
    Jan 15, 2018 at 2:34
  • \$\begingroup\$ Nice Job! You earned the bounty. \$\endgroup\$
    – qqq
    Jan 15, 2018 at 19:45
6
\$\begingroup\$

Haskell, 21 bytes

f takes an integer and returns a list of integers.

f d=[d,d+9..99^0^0^d]

Try it online!

  • Starts with the digit d and generates the range with every 9th number up to a bound of 99, except for the tricky case of 0.
  • To stop early for 0, uses that the power 0^d==1 for 0 and ==0 for all other digits. Thus 99^0^0^d gives 1 for 0 but 99 for anything else.
\$\endgroup\$
5
\$\begingroup\$

Pyke, 6 bytes

ITV
9+

Try it here!

ITV\n9+ - if input: (don't print anything for 0 case)
 TV\n9+ -  repeat 10 times:
   \n   -    print ^
     9+ -   ^ += 9
\$\endgroup\$
5
\$\begingroup\$

Python 2, 29 bytes

lambda n:n and range(n,100,9)

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Brachylog, 12 bytes

0g|g{t+₉}ᵃ¹⁰

Try it online!

Explanation

0g               Input = 0, Output = [0]
  |              Or
   g{   }ᵃ¹⁰     Accumulate 10 times, starting with [Input]
     t+₉         Take the last element, add 9
\$\endgroup\$
5
\$\begingroup\$

Bash, 31 27 bytes

seq $1 9 $(($1?99:0))|xargs

Try it online!

previous

eval echo {$1..$(($1?99:0))..9}
\$\endgroup\$
2
  • \$\begingroup\$ how does one find the man pages / bash help / ? about "{x..y..z}" ? what is it called? \$\endgroup\$ May 30, 2017 at 11:18
  • \$\begingroup\$ found it: in man page, search [^.]\.\.[^.] : brace expansion: (...) A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer. (...) When the increment is supplied, it is used as the difference between each term. The default increment is 1 or -1 as appropriate. \$\endgroup\$ May 30, 2017 at 11:22
5
\$\begingroup\$

Dyalog APL, 15 bytes

{(×⍵)/+\⍵,10⍴9}

How?

⍵,10⍴9 - concatenate input with 10 9s (⍵ 9 9 9 9 9 9 9 9 9 9).

+\ - cumulative sum.

(×⍵)/ - expand signum times - where signum gives 1 for 1-9 and 0 for 0.

Try it online!

Dyalog APL, 24 bytes

{⍵/⍨⎕=(⍵≠0)×1+9|⍵-1}⍳100

Requires ⎕IO←0.

How?

                      ⍳100  ⍝ 0 .. 99
              1+9|⍵-1      ⍝ digit sum (⍵-1 mod 9 + 1)
        (⍵≠0)×             ⍝ edge case for 0
     ⎕=                    ⍝ equals to the input
 ⍵/⍨                       ⍝ compress with the range
\$\endgroup\$
5
\$\begingroup\$

Brain-Flak, 48 bytes

{<>((()()()()()){}){(({}[()])<>[][()]({}))<>}}<>

Try it online!

I may add an explanation later.

\$\endgroup\$
2
  • \$\begingroup\$ Brilliant! I tried combining the +9 with the length to make a counter, but I never thought of doing it the other way around \$\endgroup\$
    – Jo King
    Jan 13, 2018 at 3:10
  • 1
    \$\begingroup\$ It's not over yet :P \$\endgroup\$
    – Wheat Wizard
    Jan 14, 2018 at 13:11
4
\$\begingroup\$

Mathematica, 25 bytes

If[#==0,0,Range[#,99,9]]&

works for 0

\$\endgroup\$
5
  • \$\begingroup\$ Doesn't work for 0. This also wouldn't include numbers whose digits add up to a number greater than 9. (e.g. 9 wouldn't have 99 in the output). \$\endgroup\$ May 29, 2017 at 2:42
  • \$\begingroup\$ I see what you mean. Do you inspect only "my" codes? cause many codes here don't work for 0... \$\endgroup\$
    – ZaMoC
    May 29, 2017 at 2:49
  • 1
    \$\begingroup\$ Welp, I tend to focus on Mathematica code because that's the language I know best. Didn't mean to target you or anything. I apologize if it seemed like it. \$\endgroup\$ May 29, 2017 at 2:53
  • \$\begingroup\$ all fixed and working \$\endgroup\$
    – ZaMoC
    May 29, 2017 at 3:02
  • \$\begingroup\$ What? No builtin? \$\endgroup\$
    – AAM111
    May 29, 2017 at 16:17
4
\$\begingroup\$

Jelly, 12 bytes

⁵²Ḷµ,³%9EµÐf

Try it online!

How It Works

⁵²Ḷµ,³%9EµÐf
⁵             - literal 10
 ²            - square
  R           - lowered range: 0 to 99 inclusive.
   µ     µÐf  - filter based on:
    ,³          - element and input
      %9        - mod 9
        E       - are equal
\$\endgroup\$
3
  • 1
    \$\begingroup\$ When I use 1 as an argument it also gives out 100, which is not two digit \$\endgroup\$
    – qqq
    May 29, 2017 at 2:54
  • \$\begingroup\$ This doesn't split the 0 and 9 cases. \$\endgroup\$ May 29, 2017 at 7:51
  • \$\begingroup\$ This still doesn't work for 0 \$\endgroup\$ Apr 6, 2018 at 15:44
4
\$\begingroup\$

Actually, 18 bytes

╗2╤DR⌠╜-9@%Y⌡░╜;)I

Try it online!

Explanation:

╗2╤DR⌠╜-9@%Y⌡░╜;)I
╗                   save input to register 0
 2╤DR               range(1, 100)
     ⌠╜-9@%Y⌡░      elements in range where function returns truthy:
      ╜-              subtract from input
        9@%           mod 9
           Y          is equal to 0
              ╜;)   push a copy of the input on the top and the bottom of the stack
                 I  if input is truthy, return the filtered range, else return the input (special-cases 0)
\$\endgroup\$
1
  • \$\begingroup\$ @FryAmTheEggman Fixed. \$\endgroup\$
    – user45941
    May 29, 2017 at 7:06
4
\$\begingroup\$

PHP, 41 Bytes

prints underscore separated values

for(;100>$a=&$argn;$a+=$a?9:ERA)echo$a._;

ERA is the shortest constant in PHP with the value 131116. You can replace it with the boring alternative 100 or end the program with die

Online Version

\$\endgroup\$
4
\$\begingroup\$

Brain-Flak, 54 52 bytes

{<>((((()()())){}{})()){({}<(({})<>({}))><>[()])}}<>

Try it online!

My first foray with Brain-Flak, and I think I've done pretty well. Anyone with more experience have advice?

How It Works:

{ Don't do anything if input is 0
  <>((((()()())){}{})()) Switch to other stack and add 9 and 10
                         10 is the counter, 9 is to add to the current num
  { While counter
     (
       {} Pop the counter
       <(({})<>({}))> Get a copy of the 9, switch to the other stack and add it to a copy of the top of it. Use <...> to make it return 0
       <>[()] Switch to the other stack and decrement the counter
     ) 
  }
}<> Switch to the stack with the values on it
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Good job! Welcome to Brain-Flak. \$\endgroup\$
    – MegaTom
    Jan 12, 2018 at 21:29
3
\$\begingroup\$

Jelly, 12 bytes

ȷ2ḶDS$ÐL⁼¥Ðf

Try it online!

\$\endgroup\$
3
\$\begingroup\$

PHP, 35

print_r(range($argn,!!$argn*99,9));

Creates the range [$argn, 100) with a step of 9 as array and prints it. If the input is 0 it creates the range [0,0] => array(0).

\$\endgroup\$
3
\$\begingroup\$

Python, 48 51 bytes

3 bytes saved thanks to @WheatWizard

lambda n:[x for x in range(100)if~-n==~-x%9or x==n]
\$\endgroup\$
5
  • 1
    \$\begingroup\$ try ~-x instead of (x-1) \$\endgroup\$
    – Wheat Wizard
    May 29, 2017 at 1:51
  • 1
    \$\begingroup\$ still with @WheatWizard's tip, remove the space in if ~-x%9 \$\endgroup\$ May 29, 2017 at 14:28
  • \$\begingroup\$ Now you can do ~-n==~-x%9or x==n to save a byte \$\endgroup\$
    – Wheat Wizard
    May 29, 2017 at 14:39
  • \$\begingroup\$ This gives a 0 for 9 \$\endgroup\$
    – qqq
    Jan 12, 2018 at 0:49
  • \$\begingroup\$ I need to ask a silly question... I'd like to run this. How do I get this code to run? There are some constructs here I'm learning about (mainly the ~ operator) \$\endgroup\$ Mar 3, 2018 at 3:30
2
\$\begingroup\$

R, 23 bytes

pryr::f(x+0:(10*!!x)*9)

Try it online!

The TIO link uses function(x) instead of pryr::f, since TIO doesn't have the pryr package installed.

\$\endgroup\$
2
\$\begingroup\$

Pyke, 6 bytes (old version)

Working commit

TXU#sq

Explanation:

TX     -   10**2
  U    -  range(^)
   #   - filter(^)
    s  -   digital_root(^)
     q -  ^==input
\$\endgroup\$
2
\$\begingroup\$

Ruby, 25 bytes

->a{[*a.step(99,9)]*a|[]}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

C (gcc), 55 bytes

f() need not actually be called with any argument; the n is just there instead of outside the function to save a byte.

f(n){for(scanf("%d",&n);n&&n<100;n+=9)printf("%d ",n);}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ You can save 2 bytes by putting the printf inside the loop header: Try it online! \$\endgroup\$
    – DLosc
    Jan 15, 2018 at 20:39
  • \$\begingroup\$ @DLosc myeah, but then it starts one number too late. \$\endgroup\$
    – gastropner
    Jan 15, 2018 at 21:31
  • \$\begingroup\$ The wording could be clearer, but the question does allow starting at (e.g.) 10 instead of 1: "... all of the possible two digit numbers that have that digital root. If you need it to, it can include [the single-digit number] itself, e.g. 05." In other words, including the single-digit number in the output is permitted but not required. \$\endgroup\$
    – DLosc
    Jan 16, 2018 at 3:55
2
\$\begingroup\$

Charcoal, 14 11 bytes

I∧N⁺Iθ×⁹…¹¹

Try it online! Link is to verbose version of code. Edit: Saved 2 bytes by not printing anything for zero input and 1 byte by using vectorising operations 3 bytes thanks to @ASCII-only. Explanation:

         ¹¹ Literal 11
        …   Range
       ⁹    Literal 9
      ×     Vector multiply
     θ      (First) input
    I       Cast to number
   ⁺        Vector add
  N         Input digit as a number
 ∧          Logical AND
I           Cast to string
            Implicitly print on separate lines
\$\endgroup\$
2
  • \$\begingroup\$ 11 bytes? \$\endgroup\$
    – ASCII-only
    Feb 28, 2018 at 12:07
  • \$\begingroup\$ i think i just found an alternative that prints 0, here \$\endgroup\$
    – ASCII-only
    Apr 6, 2018 at 10:45
2
\$\begingroup\$

Pyt, 11 bytes

?56+ř⁻9*+:;

Try it online!

?               implicit input; if input is truthy:
 56+            push 11
    ř⁻          řangify; decrement
      9*        multiply by 9
        +       add input
         :;     otherwise, do nothing; implicit print
\$\endgroup\$
2
\$\begingroup\$

Vyxal H, 6 bytes

ʁ'∑∑?=

Try it Online!

Port of kalsowerus's 05AB1E answer.

A port of Kip the Malamute's Pyt answer would be 8 bytes:

:[11ʁ9*+

Try it Online!

Explanation

ʁ'∑∑?=  # H flag pushes 100
ʁ'      # Filter range(100) by:
  ∑∑    #  Digit sum twice
    ?=  #  Equals the input
:[11ʁ9*+  # Implicit input
:         # Duplicate
 [        # If input is not 0:
  11ʁ     #  Push range(11)
     9*   #  Multiply by 9
       +  #  Add to input
          # (Otherwise, do nothing)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ "Make sure that 1 does not return 100" \$\endgroup\$ Mar 2, 2023 at 11:45
  • \$\begingroup\$ @DominicvanEssen fixed. That was just Vyxal implicitly converting 100 to a 1-based range instead of a 0-based range. \$\endgroup\$
    – The Thonnu
    Mar 2, 2023 at 11:58
1
\$\begingroup\$

Julia 0.6, 18 bytes

I use a ternary to catch the 0 case, and a range n:9:99 to create the numbers. In julia a range is an AbstractVector and can be used in place of an actual Vector of numbers in most cases, but it will just print as 1:9:91 which doesn't satisfy the challenge, so I wrap it in [_;] to collect the contents into a Vector.

n->n>0?[n:9:99;]:0

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Perl 5, 25 + 1 (-n) = 26 bytes

say;!$_||($_+=9)>99||redo

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Clojure, 33 bytes

(fn[n](if(> n 0)(range n 100 9)))
\$\endgroup\$
1
\$\begingroup\$

Clojure, 38 bytes

(defn f[n](if(pos? n)(range n 100 9)))

or as anonymous function which is 29 bytes

(#(if(pos? %)(range % 100 9))n)

Try it online!

thanks @steadybox

\$\endgroup\$
1
  • 1
    \$\begingroup\$ The output when n=9 is missing the last number, 99. Try it online! Also, you can save a byte by removing the space between f[n] and (if(.... \$\endgroup\$
    – Steadybox
    Jan 14, 2018 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.