Am I a Self Number?

31

2

A self number (also called a Colombian or Devlali number) is a natural number, x, where the equation n + <digit sum of n> = x has no solutions for any natural number n. For example, 21 is not a self number, as n = 15 results in 15 + 1 + 5 = 21. On the other hand, 20 is a self number, as no n can be found which satisfies such an equality.

As this definition references the digit sum, it is base dependent. For the purposes of this challenge, we will only be considering base 10 self numbers, which are sequence A003052 in the OEIS. Binary (A010061) and base 100 (A283002) self numbers have also been calalogued.

The Challenge

Given a positive integer x as input, output a truthy value if x is a self number in base 10, and a falsey value otherwise. For clarification of truthy and falsey values, refer to this meta post on the subject.

You may write a full program or function, and input and output may be provided on any of the usual channels. Standard loopholes are, of course, banned.

This is , so the shorter your answer (in bytes) the better!

Test cases

Truthy:

1
3
5
7
9
20
31
86
154
525

Falsey:

2
4
6
8
10
15
21
50
100
500

Sandbox link

Leaderboards

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

# Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

# Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

# Perl, 43 + 2 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the leaderboard snippet:

# [><>](http://esolangs.org/wiki/Fish), 121 bytes

var QUESTION_ID=159881,OVERRIDE_USER=41020;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>

Sok

Posted 2018-03-22T08:33:07.683

Reputation: 5 592

There seems to be some discussion/disagreement of valid outputs, so I think I haven't been clear on what I was intending. I've added a sentence which should hopefully clear things up, referring to this meta post. Sorry if I've caused any confusion about this!

– Sok – 2018-03-22T14:23:24.643

Not to cause more confusion, but I think this discussion is relevant to why there was some confusion. Please consider this when making future challenges, as it can be difficult to post in languages that don't have if/else constructs if you use the old consensus.

– FryAmTheEggman – 2018-03-22T15:07:02.683

@FryAmTheEggman I hadn't realised the consensus had shifted, I feel like a numpty now :/ Still, I've already added one clarification now, it seems wrong to change it again. I'll just bear it in mind for the next challenge I post. Thanks – Sok – 2018-03-22T15:14:23.350

2I am not any kind of number! I am a free man! – David Richerby – 2018-03-22T20:01:53.893

1@DavidRicherby *sends white ball after you* – Sok – 2018-03-23T08:20:10.800

Answers

8

Octave, 37 bytes

@(n)sum(dec2base(t=1:n,10)'-48,1)+t-n

Port of my MATL answer.

Try it online!

Luis Mendo

Posted 2018-03-22T08:33:07.683

Reputation: 87 464

7

Java (JDK 10), 84 bytes

i->{for(int n=i;i-->1;)i|=((""+i).chars().map(x->x-48).sum()+i^n)-1>>-1;return~i<0;}

Try it online!

Explanation

i->{                                    // IntPredicate
  for(int n=i;i-->1;)                   //  for each number below n
    i|=(                                //   keep the sign of
      (""+i).chars().map(x->x-48).sum() //    sum of digits
      +i                                //    plus the number
      ^n                                //    xor n (hoping for a zero)
     )-1>>-1;                           //    changing that into a negative number if equals to zero
  return~i<0;                           //  return i>=0
}

Credits

Olivier Grégoire

Posted 2018-03-22T08:33:07.683

Reputation: 10 647

1-1 byte: n->{int i=n,r=0;for(;i-->0;)r=(""+i).chars().map(x->x-48).sum()+i==n?1:r;return r<1;} – Kevin Cruijssen – 2018-03-22T15:08:04.467

284 bytes: i->{for(int n=i;i-->1;)i|=((""+i).chars().map(x->x-48).sum()+i^n)-1>>-1;return~i<0;} – Nevay – 2018-03-22T18:52:37.333

5

05AB1E, 8 bytes

LD€SO+ÊW

Try it online! or as a Test suite

Explanation

L          # push range [1 ... input]
 D         # duplicate
  €S       # split each number into a list of digits
    O      # sum digit lists
     +     # add (n + digitSum(n))
      Ê    # check for inequality with input
       W   # min

Emigna

Posted 2018-03-22T08:33:07.683

Reputation: 50 798

5

C (gcc), 70 67 65 bytes

i,r,d,j;f(n){for(r=i=n;d=j=--i;r*=d!=n)for(;j;j/=10)d+=j%10;i=r;}

Try it online!

To shave off another 2 bytes, the truthy value returned is no longer 1, but the number itself.

gastropner

Posted 2018-03-22T08:33:07.683

Reputation: 3 264

5

Brachylog, 12 bytes

¬{⟦∋Iẹ+;I+?}

Try it online!

Explanation

¬{         }    Fails if succeeds, suceeds if fails:
  ⟦∋I             I ∈ [0, ..., Input]
    Iẹ+           The sum of the elements (i.e. digits) of I...
       ;I+?       ... with I itself results in the Input

Fatalize

Posted 2018-03-22T08:33:07.683

Reputation: 32 976

1

Is false. a truthy value by any objective method in a way that true. is not? I for one don't think so, and it looks like that would be supported by the meta consensus

– Sok – 2018-03-22T14:15:49.743

1@Sok I added 3 uninteresting bytes to adress your concerns. – Fatalize – 2018-03-22T14:25:08.517

4

Octave, 49 47 44 bytes

@(x)arrayfun(@(k)k+sum(num2str(k)-48)-x,1:x)

Try it online!

Explanation:

Trying to do the operation on a range is cumbersome and long, since num2str returns a string with spaces as separators if there are more than input number. Subtracting 48 would therefore give something like: 1 -16 -16 2 -16 -16 3 -16 -16 4 for an input range 1 ... 4. Getting rid of all the -16 takes a lot of bytes.

Therefore, we'll do this with a loop using arrayfun. For each of the numbers k = 1 .. x, where x is the input, we add k and its digit sum, and subtract x. This will return an array of with the result of that operation for each of the numbers in k. If any of the numbers in the array is a zero, the number is not a self number.

For inputs 20 and 21, the outputs are:

20:  -18, -16, -14, -12, -10, -8, -6, -4, -2, -9, -7, -5, -3, -1, 1, 3, 5, 7, 9, 2
21:  -19, -17, -15, -13, -11, -9, -7, -5, -3, -10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 1, 3

There are only non-zero elements for input 20, and at least one non-zero element for input 21. That means that 20 is a self number, and 21 is not.

Octave treats an array with at least one zero as false, as can be seen in the TIO-link.

Stewie Griffin

Posted 2018-03-22T08:33:07.683

Reputation: 43 471

4

MATL, 11 bytes

t:tFYA!Xs+-

The output is a non-empty array, which is truthy if all its entries are non-zero, and falsy if it contains one or more zeros.

Try it online! Or verify all test cases, including truthiness/falsihood test.

Explanation

Consider input n = 10 as an example.

t       % Implicit input, n. Duplicate
        % STACK: 10, 10
:       % Range
        % STACK: 10, [1 2 3 4 5 6 7 8 9 10]
t       % Duplicate
        % STACK: 10, [1 2 3 4 5 6 7 8 9 10], [1 2 3 4 5 6 7 8 9 10]
FYA!    % Convert to base 10 digits and transpose
        % STACK: 10, [1 2 3 4 5 6 7 8 9 10], [0 0 0 0 0 0 0 0 0 1
                                              1 2 3 4 5 6 7 8 9 0]
Xs      % Sum of each column
        % STACK: 10, [1 2 3 4 5 6 7 8 9 10], [1 2 3 4 5 6 7 8 9 1]
+       % Add, element-wise
        % STACK: 10, [2 4 6 8 10 12 14 16 18 11]
-       % Subtract, element-wise
        % STACK: [8 6 4 2 0 -2 -4 -6 -8 -1]
        % Implicit display

Luis Mendo

Posted 2018-03-22T08:33:07.683

Reputation: 87 464

3

Pari/GP, 32 bytes

n->!sum(i=1,n,i+sumdigits(i)==n)

Try it online!

alephalpha

Posted 2018-03-22T08:33:07.683

Reputation: 23 988

3

J, 28, 24, 22 21 bytes

-1 byte thanks to Conor O'Brien

-2 byts thanks to ngn

$@-.(+1#.,.&.":)"+@i.

Try it online!

Explanation:

i. a list 0 .. n-1

( )"+ for each item in the list

.,.&.": convert it to a list of digits,

1# find their sum

+ and add it to the item

$@-. exclude the list from the argument and find the shape

Galen Ivanov

Posted 2018-03-22T08:33:07.683

Reputation: 13 815

1I realize this is an old post, but "0 i. can be "+i. (-1 byte). – Conor O'Brien – 2019-07-23T02:22:44.690

1@ConorO'Brien Thank you! I think I didn't know this kind of golfs back then; now I use it (when I remember :) ) – Galen Ivanov – 2019-07-23T06:17:06.890

1-.@e. -> $@-. – ngn – 2019-07-23T06:53:01.990

@ngn Thank you, really nice! – Galen Ivanov – 2019-07-23T07:10:57.213

1@GalenIvanov also [:( )"+i. -> ( )"+@i. – ngn – 2019-07-23T07:13:13.060

@ngn Of course! – Galen Ivanov – 2019-07-23T07:17:43.517

3

APL (Dyalog), 14 bytes

~⊢∊⍳+(+/⍎¨∘⍕)¨∘⍳

Try it online!

How?

               ⍳   range
     (  ⍎¨∘⍕)      digits
     (+/    )      digit sums
   ⍳+              vectorized addition with the range
 ⊢∊                is the input included?
~                  negate

Uriel

Posted 2018-03-22T08:33:07.683

Reputation: 11 708

that's 16 bytes. to make them 15: +/⍎¨∘⍕ -> #+.⍎⍕ – ngn – 2019-07-23T06:45:33.443

3

Jelly, 6 bytes

ḟDS+Ɗ€

For input n, this returns [n] if n is a self number, [] if not.

Try it online!

How it works

ḟDS+Ɗ€  Main link. Argument: n

     €  Call the link to the left for each k in [1, ..., n].
    Ɗ     Drei; combine the three links to the left into a monadic chain.
 D          Decimal; map k to the array of its digits in base 10.
  S         Take the sum.
   +        Add k to the sum of the k's digits.
ḟ       Filterfalse; promote n to [n], then remove all elements that appear in the
        array to the right.
        This returns [n] if the array doesn't contain n, [] if it does.

Dennis

Posted 2018-03-22T08:33:07.683

Reputation: 196 637

What encoding packs these six characters into only six bytes? – WGroleau – 2018-03-23T01:51:20.580

1

Jelly uses a custom code page.

– Dennis – 2018-03-23T01:56:48.753

3

Haskell, 46 bytes

f x=and[x/=n+sum[read[d]|d<-show n]|n<-[1..x]]

Try it online!

user1472751

Posted 2018-03-22T08:33:07.683

Reputation: 1 511

2

Python 3, 60, 56, 55, 54 bytes

lambda x:{x}-{n+sum(map(int,str(n)))for n in range(x)}

Try it online!

-4 using all inverse instead of any
-1 by changing != to ^ by @jonathan-allan
-1 by using sets by @ovs

Rick Rongen

Posted 2018-03-22T08:33:07.683

Reputation: 223

2

Perl 6, 39 33 bytes

{!grep $_,map {$_+[+] .comb},^$_}

Try it out!

A bare block with implicit single parameter, called thus:

say {!grep $_,map {$_+[+] .comb},^$_}(500);
> False
say {!grep $_,map {$_+[+] .comb},^$_}(525);
> True

Since n + digits(n) >= n, we can just calculate the Colombian number for all the numbers up to our query value and see if any of them match. So this calculates the Colombian number for a given input:

{$_ + [+] .comb}

Which we apply to all the values up to our target:

(^$_).map({$_+[+] .comb})

But we only care whether any of them match, not what those values are, so as pointed out by @nwellenhof, we can grep:

grep $_, map {$_+[+] .comb}, ^$_

The rest is just coercion to bool and wrapping in a block.

39 bytes

{!((^$_).map({$_+[+] .comb}).any==$_)}

TIO test link provided by @Emigna

@nwellenhof pointed out that using grep would save 6 bytes!

Phil H

Posted 2018-03-22T08:33:07.683

Reputation: 1 376

1

Cool solution! You could add a TIO link for testing purposes.

– Emigna – 2018-03-22T09:58:39.167

1{!grep $_,map {$_+[+] .comb},^$_} for 33 bytes. – nwellnhof – 2018-03-22T13:47:46.507

29 bytes – Jo King – 2019-07-23T02:48:47.627

2

Python 2, 70 66 Bytes

lambda x:[i for i in range(x)if i+sum([int(j)for j in`i`])==x]==[]

EDIT: -4 thanks to @user56656

sonrad10

Posted 2018-03-22T08:33:07.683

Reputation: 535

1in python2 you can use \i`` instead of str(i) to save 4 bytes. – Post Rock Garf Hunter – 2018-03-22T12:48:05.320

@user56656 thanks, I didn't know about that – sonrad10 – 2018-03-22T13:26:11.640

1You can drop the [ and ] inside the sum – Mr. Xcoder – 2018-03-22T13:32:35.937

lambda x:[i for i in range(x)if i+sum(map(int,\i`))==x]==[]` – B. Eckles – 2018-03-22T19:31:06.960

lambda x:all(i+sum(map(int,\i`))-x for i in range(x))` – B. Eckles – 2018-03-22T20:05:14.030

lambda x:all(x^i+sum(map(int,\i`)) for i in range(x))` -- Borrowing from Python 3 answer above. Kinda disappointing. I didn't realize this whole time I've been reinventing what they did above. Just though i was improving this answer. xP EDIT: ...And just realized this last change didn't save anything anyway. xP – B. Eckles – 2018-03-22T20:12:56.163

2

Pyth, 8 bytes

!/m+sjdT

Test suite.

If swapping truthy / falsy values is allowed, then we can drop the ! and get 7 bytes instead. One of Sok's suggestions helped me golf 2 bytes.

Explanation

!/m+sjdT – Full program. Takes an input Q from STDIN, outputs either True or False.
  m      – Map over the range [0 ... Q) with a variable d.
     jdT – Convert d to base 10.
    s    – Sum.
   +     – And add the sum to d itself.
 /       – Count the occurrences of Q in the result.
!        – Negate. Implicitly output the result.

Mr. Xcoder

Posted 2018-03-22T08:33:07.683

Reputation: 39 774

I had .AmnQ+dsjdT, I had no idea about /. I've not used Pyth properly in a long time it seems! +1 – Sok – 2018-03-22T13:43:35.703

@Sok / basically counts the occurrences of an element in a list. I can also use }, which tests whether an object appears in a list, but I think that's the same byte count. – Mr. Xcoder – 2018-03-22T13:44:40.797

I think the S isn't required - input will be a positive integer, so having 0 in the mapping list won't be a problem? At least, it seems to work for the given test cases. – Sok – 2018-03-22T13:47:50.593

@Sok Great, you're right! I shaved off one more byte thanks to this. – Mr. Xcoder – 2018-03-22T13:50:16.343

How does +sjdT add sjdT to d? I've never seen anything like that – RK. – 2018-04-04T21:32:28.270

@RK. Pyth infers missing arguments based on context - in this case, + is missing a second argument while inside an m function block, so d is used. The m itself is missing a second argument too, but as its outside any function scope, Q is used. The full program is !/m+sjdTdQ. – Sok – 2018-06-27T10:27:45.263

@RK. Sok's comment explains that very well. Sorry for not replying to your comment, I must have overlooked it! – Mr. Xcoder – 2018-06-27T10:48:30.930

2

Japt -d!, 6 bytes

N¥U+ìx

Try it


Original, 8 bytes

Returns the input number for truthy or 0 for falsey. If only the empty array were falsey in JavaScript, this could be 7 bytes.

ÂNkUÇ+ìx

Try it


Explanation

             :Implicit input of integer U
   UÇ        :Generate the range [0,U) and pass each Z through a function
      ì      :  Digits of Z
       x     :  Reduce by addition
     +       :  Add to Z
  k          :Remove the elements in that array
 N           :From the array of inputs
            :Bitwise NOT NOT (~~), casts an empty array to 0 or a single element array to an integer 

Alternative

Ç+ìxÃe¦U

Try it

             :Implicit input of integer U
Ç            :Generate the range [0,U) and pass each Z through a function
  ì          :  Digits of Z
   x         :  Reduce by addition
 +           :  Add to Z
    Ã        :End function
     e       :Every
      ¦U     :  Does not equal U

Shaggy

Posted 2018-03-22T08:33:07.683

Reputation: 24 623

1

JavaScript (ES6), 52 51 bytes

Saved 1 byte thanks to @l4m2

Returns 0 or 1.

n=>(g=k=>k?eval([...k+'k'].join`+`)-n&&g(k-1):1)(n)

Try it online!

Arnauld

Posted 2018-03-22T08:33:07.683

Reputation: 111 334

n=>(g=k=>k?n-eval([...k+'k'].join`+`)&&g(k-1):1)(n) – l4m2 – 2018-03-22T09:59:46.530

1

Retina, 55 bytes

.+
*
Lv`_+
_+
$&$.&
^_+
$&¶$&
\d
*
Cms`^(_+)\b.*¶\1\b
0

Try it online! Link includes test cases. Explanation:

.+
*

Convert input x to unary.

Lv`_+

Create a range from x down to 1.

_+
$&$.&

Suffix the decimal value of each n to its unary value.

^_+
$&¶$&

Make a copy of x.

\d
*

Convert each decimal digit of n to unary, thus adding the digits to the existing copy of n.

Cms`^(_+)\b.*¶\1\b

Check whether x appears in any of the results.

0

Invert the result.

Neil

Posted 2018-03-22T08:33:07.683

Reputation: 95 035

1

Haskell, 63 58 bytes

s 0=0
s x=x`mod`10+s(x`div`10)
f n=all(\x->x+s x/=n)[1..n]

Try it online!

Cristian Lupascu

Posted 2018-03-22T08:33:07.683

Reputation: 8 369

1

Perl 5 -a, 34 bytes

#!/usr/bin/perl -a
say!grep{s/./$_+=$&/reg;/@F/}1..$_

Try it online!

Ton Hospel

Posted 2018-03-22T08:33:07.683

Reputation: 14 114

1

Ruby, 38 bytes

->x{(1..x).all?{|n|n+n.digits.sum!=x}}

Try it online!

Kirill L.

Posted 2018-03-22T08:33:07.683

Reputation: 6 693

1

Attache, 31 20 bytes

${0={Sum@_+_=x}~1:x}

Try it online!

{Sum@_+_=x} performs the check, ~1:x counts how many occurrences of that check are in the range from 1 to x, and 0= asserts that there are no such elements.


Attache, 30 bytes

None##`=#{_+Sum=>Digits!_}@1&`:

Try it online!

Explanation

This is a composition (##) of two functions:

  • None
  • `=#{_+Sum=>Digits!_}@1&`:

This means that whatever the second function returns, None will ensure it contains no truthy values

The second function is a fork (#) of two functions:

  • `=
  • {_+Sum=>Digits!_}@1&`:

This means that, given an input n, this fork checks for equality with n and each member of this second function.

This second function is also a composition (@) of two functions, being {_+Sum=>Digits!_}, which sums the digits of the input and adds it to the input and applies this function to each member of the range from 1 to n (1&`:).

In summary, this checks that no (None) element added to its digits ({_+Sum=>Digits!_}) from 1 to n (1&`:) is equal to (`=) n.

Conor O'Brien

Posted 2018-03-22T08:33:07.683

Reputation: 36 228

1

Retina, 24 bytes

.+
*

$.`¶$`
\d
*
D`
.\z

Try it online!

Could be 18 bytes with input given in unary, but the other Retina answer uses decimal as well, so I figured using decimal would make for a fairer comparison.

Explanation

.+
*

Convert input to unary, using _ as the unary digit.


$.`¶$`

At each position in the string (i.e. beginning, end, and between every pair of characters), insert: $.`, the length of the prefix (or the zero-indexed position of the match), , a linefeed, $`, the prefix itself (i.e. a unary representation of the zero-indexed position). E.g. if the input was 3 and we'd have the unary representation ___, then this would result in:

0
_1
__2
___3
___

In other words, we get one line for each number from 0 to the input (inclusive), which holds both a unary and a decimal representation of that number.

\d
*

We convert each digit to unary, which effectively computes the digit sum on each line and adds it to the number itself (all in unary).

D`

Deduplicate lines. This doesn't actually delete duplicate lines but just clears them to empty lines. So if any number from 0 to the input plus its digit sum is equal to the input, the last line will get cleared. Otherwise, the last line remains in place.

.\z

Check whether there's still a character on the last line. We can't use $, because that also matches in front of a trailing linefeed (which is exactly where we don't want to look).

Martin Ender

Posted 2018-03-22T08:33:07.683

Reputation: 184 808

1

Bash + GNU Coreutils, 91 Bytes

Returns truthy or falsy.

e=1;for((i=1;i<=$1;i++));{ [ $(bc<<<`grep -o . <<<$i|tr '\n' +`$i) -eq $1 ]&&e=0; };exit $e

rexkogitans

Posted 2018-03-22T08:33:07.683

Reputation: 589

1

Kotlin, 48 bytes

x->(1..x).all{v->"$v".sumBy{it.toInt()-48}+v!=x}

Try it online!

Explanation:

(1..x)      // Range of numbers from 1 to x
.all{v->    // Function expression that checks if ALL values evaluate to tru
"$v"        // Convert the value into a string
.sumBy{it.toInt()-48} // For each character, get the ASCII char value, subtract 48 in order to get it back to the numerical value, and then sum it up
+v          // Add the original number
!=x}        // Ensure that it does not equal the input

Makotosan

Posted 2018-03-22T08:33:07.683

Reputation: 503

1

Add++, 27 bytes

D,g,@,BDB+A+BK=
L,RbUABkºg!

Try it online!

How it works

D,g,@,		; Create a monadic function 'g'
		; Example argument:		[15]
	BD	; Digits;		STACK = [[1 5]]
	B+	; Sum;			STACK = [6]
	A+	; Add argument;		STACK = [21]
	BK=	; Equals input;		STACK = [1]

L,		; Create a lambda function
		; Example argument:		[21]
	RbUABk	; Push range;		STACK = [1 2 3 ... 19 20 21]
	ºg	; Any true from 'g';	STACK = [1]
	!	; Logical NOT;		STACK = [0]

caird coinheringaahing

Posted 2018-03-22T08:33:07.683

Reputation: 13 702

1

Elixir, 66 65 bytes

fn a->!Enum.any?1..a,fn b->a==b+Enum.sum Integer.digits b end end

Try it online!

Okx

Posted 2018-03-22T08:33:07.683

Reputation: 15 025

1

Stax, 8 bytes

Å8avj╒Éπ

Run and debug it

Explanation

ASCII equivalent:

{cE+|+x=f!
{       f     Filter [1..input] with block
 cE           Decimal digits of current value
   +|+        Sum of digits and the current value
      x=      Equals input
         !    Logical not, maps empty array to 1 and non-empty array to 0

fcE+|+x= is a Stax program that outputs all numbers whose sum of digits added to itself equals the input. For example for 101 the output would be 91 and 100, each on a separate line.

Run and debug it

Weijun Zhou

Posted 2018-03-22T08:33:07.683

Reputation: 3 396

1

Pyt, 7 bytes

ĐřĐŚ+∈¬

Try it online!

If swapping truthy/falsy values is allowed, then the ¬ at the end can be dropped for 6 bytes.

Explanation:

        Implicit input
Đ       Duplicate input
ř       Get [1,2,...,input]
Đ       Duplicate the top of the stack
Ś       Get the sum of the digits of each element in the array
+       Add the top two items on the stack element-wise
∈       Is the input in the list?
¬       Negate the top of the stack
        Implicit print

mudkip201

Posted 2018-03-22T08:33:07.683

Reputation: 833

1

J, 20 bytes

#@-.i.+1#.10#.inv i.

Try it online!

                  i.     Range [0,n-1]
          10#.inv        To base 10
       1#.               Sum the digits
    i.+                  Plus the corresponding number
  -.                     Remove from the input, leaves an empty list if it was a self number.
#@                       An empty list is truthy, so return the length instead.

FrownyFrog

Posted 2018-03-22T08:33:07.683

Reputation: 3 112