Opposite of the digital root!

19

2

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!

FantaC

Posted 2017-05-29T00:39:57.520

Reputation: 1 425

1does the number itself count as a two digit number? (05)? – Destructible Lemon – 2017-05-29T00:44:58.610

2Inverse challenge – FryAmTheEggman – 2017-05-29T00:48:32.733

5What 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. – FryAmTheEggman – 2017-05-29T00:50:03.303

1Your 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. – FryAmTheEggman – 2017-05-29T02:48:44.037

@FryAmTheEggman I just updated the post – FantaC – 2017-05-29T03:01:36.097

2is the opposite of a digital root an analog root? – tuskiomi – 2017-05-29T03:32:35.857

Why should 0 return 0, that is not a 2 digits number (the same for all other 9 digits) – edc65 – 2017-05-29T06:52:00.650

@edc65 You're right, I'm not sure exactly what I was thinking. I will modify the req. – FantaC – 2017-05-29T13:01:15.190

Must 1 and 01 both be included (for input 1)? – user202729 – 2018-01-13T06:31:00.283

@user202729 no, you get to choose – FantaC – 2018-01-13T14:33:41.637

Answers

5

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

Blue

Posted 2017-05-29T00:39:57.520

Reputation: 26 661

9

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)));
}

Arnauld

Posted 2017-05-29T00:39:57.520

Reputation: 111 334

3A downvote without any comment doesn't help much to improve the answer... – Arnauld – 2017-05-29T07:27:42.727

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. – David Mulder – 2017-05-29T20:09:02.090

@DavidMulder Thanks for the suggestion. That's actually the way I answer most of the time. Updated. – Arnauld – 2017-05-29T20:24:47.620

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. – Craig Ayre – 2018-01-12T11:03:16.160

@CraigAyre I'm not sure where this + came from... Updated. Thanks! – Arnauld – 2018-01-12T11:25:54.780

8

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

kalsowerus

Posted 2017-05-29T00:39:57.520

Reputation: 1 894

I think you can replace the infinite loop by SOSO, since the number will never be bigger than 99. – Adnan – 2017-05-29T10:32:50.687

@Adnan Don't think, I assure you he can. – Erik the Outgolfer – 2017-05-29T10:46:13.553

@Adnan Thanks, modified it. – kalsowerus – 2017-05-29T10:46:50.387

1тL< isn't really shorter than plain 99Ý. ;) – Erik the Outgolfer – 2017-05-29T10:48:31.553

1@EriktheOutgolfer Well, I was probably trying to hard to save a byte there ;D – kalsowerus – 2017-05-29T10:54:53.617

2Main "link"? Since when 05AB1E has links? It's not Jelly. – Andrew Savinykh – 2017-05-30T03:11:03.580

7

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.

Ørjan Johansen

Posted 2017-05-29T00:39:57.520

Reputation: 6 914

7

Jelly, 8 bytes

11Ḷ×9+ȧ@

Try it online!

Different algorithm than my other answer.

Erik the Outgolfer

Posted 2017-05-29T00:39:57.520

Reputation: 38 134

7

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.

Post Rock Garf Hunter

Posted 2017-05-29T00:39:57.520

Reputation: 55 382

The 46 doesn’t preserve the original number :( – Jo King – 2018-01-15T02:28:53.977

@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. – Post Rock Garf Hunter – 2018-01-15T02:34:11.220

Nice Job! You earned the bounty. – FantaC – 2018-01-15T19:45:49.777

5

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

Uriel

Posted 2017-05-29T00:39:57.520

Reputation: 11 708

5

Python 2, 29 bytes

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

Try it online!

ovs

Posted 2017-05-29T00:39:57.520

Reputation: 21 408

5

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

Fatalize

Posted 2017-05-29T00:39:57.520

Reputation: 32 976

5

Bash, 31 27 bytes

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

Try it online!

previous

eval echo {$1..$(($1?99:0))..9}

marcosm

Posted 2017-05-29T00:39:57.520

Reputation: 986

how does one find the man pages / bash help / ? about "{x..y..z}" ? what is it called? – Olivier Dulac – 2017-05-30T11:18:21.443

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. – Olivier Dulac – 2017-05-30T11:22:02.717

5

Brain-Flak, 48 bytes

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

Try it online!

I may add an explanation later.

MegaTom

Posted 2017-05-29T00:39:57.520

Reputation: 3 787

Brilliant! I tried combining the +9 with the length to make a counter, but I never thought of doing it the other way around – Jo King – 2018-01-13T03:10:34.407

1It's not over yet :P – Post Rock Garf Hunter – 2018-01-14T13:11:34.923

4

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)

Mego

Posted 2017-05-29T00:39:57.520

Reputation: 32 998

@FryAmTheEggman Fixed. – Mego – 2017-05-29T07:06:42.433

4

Mathematica, 25 bytes

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

works for 0

J42161217

Posted 2017-05-29T00:39:57.520

Reputation: 15 931

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). – JungHwan Min – 2017-05-29T02:42:32.003

I see what you mean. Do you inspect only "my" codes? cause many codes here don't work for 0... – J42161217 – 2017-05-29T02:49:16.843

1Welp, 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. – JungHwan Min – 2017-05-29T02:53:31.090

all fixed and working – J42161217 – 2017-05-29T03:02:27.023

What? No builtin? – OldBunny2800 – 2017-05-29T16:17:26.293

4

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

Jörg Hülsermann

Posted 2017-05-29T00:39:57.520

Reputation: 13 026

4

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

fireflame241

Posted 2017-05-29T00:39:57.520

Reputation: 7 021

1When I use 1 as an argument it also gives out 100, which is not two digit – FantaC – 2017-05-29T02:54:37.353

This doesn't split the 0 and 9 cases. – Ørjan Johansen – 2017-05-29T07:51:41.640

This still doesn't work for 0

– caird coinheringaahing – 2018-04-06T15:44:12.150

4

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

Jo King

Posted 2017-05-29T00:39:57.520

Reputation: 38 234

1Good job! Welcome to Brain-Flak. – MegaTom – 2018-01-12T21:29:12.213

3

Python, 48 51 bytes

3 bytes saved thanks to @WheatWizard

lambda n:[x for x in range(100)if~-n==~-x%9or x==n]

Uriel

Posted 2017-05-29T00:39:57.520

Reputation: 11 708

1try ~-x instead of (x-1) – Post Rock Garf Hunter – 2017-05-29T01:51:49.740

1still with @WheatWizard's tip, remove the space in if ~-x%9 – Felipe Nardi Batista – 2017-05-29T14:28:44.153

Now you can do ~-n==~-x%9or x==n to save a byte – Post Rock Garf Hunter – 2017-05-29T14:39:27.197

This gives a 0 for 9 – FantaC – 2018-01-12T00:49:56.590

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) – Allen Fisher – 2018-03-03T03:30:23.857

3

Jelly, 12 bytes

ȷ2ḶDS$ÐL⁼¥Ðf

Try it online!

Erik the Outgolfer

Posted 2017-05-29T00:39:57.520

Reputation: 38 134

3

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).

Christoph

Posted 2017-05-29T00:39:57.520

Reputation: 1 489

2

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.

Nitrodon

Posted 2017-05-29T00:39:57.520

Reputation: 9 181

2

Pyke, 6 bytes (old version)

Working commit

TXU#sq

Explanation:

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

Blue

Posted 2017-05-29T00:39:57.520

Reputation: 26 661

2

Ruby, 25 bytes

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

Try it online!

G B

Posted 2017-05-29T00:39:57.520

Reputation: 11 099

2

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

Neil

Posted 2017-05-29T00:39:57.520

Reputation: 95 035

11 bytes? – ASCII-only – 2018-02-28T12:07:11.183

i think i just found an alternative that prints 0, here

– ASCII-only – 2018-04-06T10:45:28.673

2

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!

gastropner

Posted 2017-05-29T00:39:57.520

Reputation: 3 264

You can save 2 bytes by putting the printf inside the loop header: Try it online!

– DLosc – 2018-01-15T20:39:54.437

@DLosc myeah, but then it starts one number too late. – gastropner – 2018-01-15T21:31:46.617

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. – DLosc – 2018-01-16T03:55:14.140

1

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!

gggg

Posted 2017-05-29T00:39:57.520

Reputation: 1 715

1

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

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

Try it online!

Xcali

Posted 2017-05-29T00:39:57.520

Reputation: 7 671

1

Clojure, 33 bytes

(fn[n](if(> n 0)(range n 100 9)))

user84207

Posted 2017-05-29T00:39:57.520

Reputation: 171

1

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

Alonoaky

Posted 2017-05-29T00:39:57.520

Reputation: 51

1

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(....

– Steadybox – 2018-01-14T23:13:00.793

0

Perl 5, 62 bytes

sub x{$f=$_[0];@x=("0$f",$f);push@x,map$f+$_*10,(1..9)if$f;@x}

There's bound to be a shorter way

Tom Tanner

Posted 2017-05-29T00:39:57.520

Reputation: 251

0

Gol><>, 12 bytes

I:ZhbF:N9+|;

Try it online!

How it works

I:ZhbF:N9+|;

I             Take input as number
 :            Duplicate
  Z           Pop and skip one if nonzero
   h          If zero, print the top as number and halt
              Otherwise...
    bF....|   Repeat these commands 11 times
      :N      Print top as number, with newline
        9+    Add 9
           ;  Halt

Bubbler

Posted 2017-05-29T00:39:57.520

Reputation: 16 616