List all possible titles for the Anno games

37

2

In the Anno video game series there are 6 games with a 7th one announced for early 2019. Their titles always feature a year in a specific pattern:

Anno 1602, Anno 1503, Anno 1701, Anno 1404, Anno 2070, Anno 2205, Anno 1800

  • The digital sum is always 9.
  • The years are four digits long.
  • They contain at least one zero.

Within these constrains there exist 109 possible titles:

[1008,1017,1026,1035,1044,1053,1062,1071,1080,1107,1170,1206,1260,1305,1350,1404,1440,1503,1530,1602,1620,1701,1710,1800,2007,2016,2025,2034,2043,2052,2061,2070,2106,2160,2205,2250,2304,2340,2403,2430,2502,2520,2601,2610,2700,3006,3015,3024,3033,3042,3051,3060,3105,3150,3204,3240,3303,3330,3402,3420,3501,3510,3600,4005,4014,4023,4032,4041,4050,4104,4140,4203,4230,4302,4320,4401,4410,4500,5004,5013,5022,5031,5040,5103,5130,5202,5220,5301,5310,5400,6003,6012,6021,6030,6102,6120,6201,6210,6300,7002,7011,7020,7101,7110,7200,8001,8010,8100,9000]

Your objective is to list them all in any reasonable form in the fewest number of bytes.

Laikoni

Posted 2018-10-15T13:50:36.240

Reputation: 23 676

How flexible is the output format? Is this acceptable?

– Luis Mendo – 2018-10-15T14:50:54.783

1@LuisMendo Yes, that's fine with me. – Laikoni – 2018-10-15T14:54:48.360

Are lists of digits allowed?

– Erik the Outgolfer – 2018-10-15T18:18:28.513

Are large numbers of spaces between number acceptable? 1008 1017 1035... – aslum – 2018-10-15T18:59:27.903

1@aslum I assume you mean a lot of spaces, not just one, right? Comment markdown doesn't allow for a good representation of that. And I would assume that's allowed, given that Luis's format above is allowed. ;-) – Erik the Outgolfer – 2018-10-15T20:20:07.810

Yeah, currently my code puts a space for every non-9-sum number... so 8 between the first and second result... 11 between third and fourth, and 899 spaces between last two... but as long as output goes to a standard web browser all duplicate spaces will be ignored, much like the spaces where ignored in my comment above. – aslum – 2018-10-15T20:22:56.080

1@EriktheOutgolfer I'd say no to lists of digits because they really do not look like years anymore. – Laikoni – 2018-10-15T21:50:51.793

@aslum Excessive spaces are fine. – Laikoni – 2018-10-15T21:51:19.747

Hmm I stopped around Anno 1404, my assumption was that the difference between the titles is always 99 but then it would have been 2097 and 2196... – Arsenal – 2018-10-16T15:24:45.577

Answers

20

R, 59 51 bytes

Outputs the valid numbers as the names of a list of 201's. Why 201? Because ASCII 0 is 48, and 4*48+9 is... yeah. Saved 6 bytes by aliasing ^ to Map and another 2 by using 1:9e3 as range.

"^"=Map;x=sum^utf8ToInt^grep(0,1:9e3,,,T);x[x==201]

Try it online!

Explanation

# Create list of sums of ASCII char values of numbers,
# with the original numbers as the names of the list
x <- Map(sum,
  # Create a list from the strings where each element is the string split 
  # into ASCII char values
  Map(utf8ToInt,
      # Find all numbers between 1 and 9e3 that contain a zero
      # Return the matched values as a vector of strings (6th T arg)
      grep(pattern=0,x=1:9000,value=TRUE)
  )
)
# Pick out elements with value 201 (i.e. 4-digits that sum to 9)
# This implicitly only picks out elements with 4 digits, since 3-digit 
# sums to 9 won't have this ASCII sum, letting us use the 1:9e3 range
x[x==201] 

J.Doe

Posted 2018-10-15T13:50:36.240

Reputation: 2 379

3ah, grep, why do I never remember that it casts to character... – Giuseppe – 2018-10-15T14:23:56.313

17

Perl 6, 35 33 bytes

-2 bytes thanks to Jo King

{grep {.ords.sum==201&&/0/},^1e4}

Try it online!

nwellnhof

Posted 2018-10-15T13:50:36.240

Reputation: 10 037

9

Python 2, 67 66 64 bytes

print[y for y in range(9001)if('0'in`y`)*sum(map(ord,`y`))==201]

Try it online!


Saved:

  • -1 byte, thanks to Luis felipe De jesus Munoz
  • -2 bytes, thanks to Kevin Cruijssen

TFeld

Posted 2018-10-15T13:50:36.240

Reputation: 19 246

64 bytes by using the ord sum == 201 trick from other answers. – Kevin Cruijssen – 2018-10-16T07:52:18.453

9

Jelly, 11 bytes

9ȷṢ€æ.ẹ9ṫ19

Try it online!

How it works

9ȷṢ€æ.ẹ9ṫ19  Main link. No arguments.

9ȷ           Set the left argument and the return value to 9000.
  Ṣ€         Sort the digits of each integer in [1, ..., 9000].
    æ.       Perform the dot product of each digit list and the left argument,
             which gets promoted from 9000 to [9000].
             Overflowing digits get summed without multiplying, so we essentially
             map the digit list [a, b, c, d] to (9000a + b + c + d).
      ẹ9     Find all 1-based indices of 9.
             Note that 9000a + b + c + d == 9 iff a == 0 and b + c + d == 9.
        ṫ19  Tail 19; discard the first 18 indices.

Dennis

Posted 2018-10-15T13:50:36.240

Reputation: 196 637

7

PowerShell, 50 49 bytes

999..1e4-match0|?{([char[]]"$_"-join'+'|iex)-eq9}

Try it online!

Constructs a range from 999 to 10000, then uses inline -match as a filter to pull out those entries that regex match against 0. This leaves us with 1000, 1001, 1002, etc. We then pipe that into a Where-Object clause where we take the current number as a string "$_", cast it as a char-array, -join those characters together with + and Invoke-Expression (similar to eval) to come up with their digit sum. We check whether that is -equal to 9, and if so it's passed on the pipeline. At program completion, those numbers are picked up from the pipeline and implicitly output.

AdmBorkBork

Posted 2018-10-15T13:50:36.240

Reputation: 41 581

5

JavaScript (Node.js), 89 bytes

[...Array(9e3)].map(_=>i++,i=1e3).filter(a=>(s=[...a+""]).sort()[0]<1&eval(s.join`+`)==9)

Try it online!

  • -4 bytes thanks to @ETHproductions

JavaScript (Node.js), 129 127 126 124 115 114 111 110 105 97 93 92 90 bytes

[...Array(9e3)].map(f=(_,i)=>eval(s=[...(i+=1e3)+""].sort().join`+`)-9|s[0]?0:i).filter(f)

Try it online!

Explanation

[...Array(9e3)].map(f=(_,i)=>eval(s=[...(i+=1e3)+""].sort().join`+`)-9|s[0]?0:i).filter(f)
[...Array(9e3)].map(f=(_,i)=>                                                  )           // Create a 9000-length array and loop over it; store the loop body
                                    [...(i+=1e3)+""]                                       // Add 1000 to the index and split it into an array of characters (17 -> ["1", "0", "1", "7"])
                                                    .sort()                                // Sort the array of characters in ascending order by their code points ("0" will always be first) (["1", "0", "1", "7"] -> ["0", "1", "1", "7"])
                                  s=                       .join`+`                        // Join them together with "+" as the separator (["0", "1", "1", "7"] -> "0+0+2+9"); store the result
                             eval(                                 )-9                     // Evaluate and test if it's different than 9
                                                                       s[0]                // Take the first character of the string and implicitly test if it's different than "0"
                                                                      |    ?0              // If either of those tests succeeded, then the number doesn't meet challenge criteria - return a falsey value
                                                                             :i            // Otherwise, return the index
                                                                                .filter(f) // Filter out falsey values by reusing the loop body

First time doing code golf in JavaScript. I don't think I need to say it, but if I'm doing something wrong, please notify me in the comments below.

  • -3 bytes thanks to @Luis felipe De jesus Munoz

  • -6 bytes thanks to @Kevin Cruijssen

Alion

Posted 2018-10-15T13:50:36.240

Reputation: 965

1[...Array(9e3)] instead Array(9e3).fill() saves 2 bytes – Luis felipe De jesus Munoz – 2018-10-15T15:03:27.957

1.map(a=>+a) instead .map(Number) saves another byte – Luis felipe De jesus Munoz – 2018-10-15T15:05:41.070

1You can remove the space at (_, i) to save a byte, and s[0]+s[1]+s[2]+s[3] can be eval(s.join`+`) to save an additional 4 bytes. – Kevin Cruijssen – 2018-10-15T15:29:22.077

@KevinCruijssen Whoops! No clue how I missed that space there. Also, the eval trick is nuts, thanks a bunch for that one. – Alion – 2018-10-15T15:32:23.357

1Also, I'm pretty sure the || can be | in your answer. – Kevin Cruijssen – 2018-10-15T15:41:04.180

1

If you use .map() only to generate the range, and keep the filtering separate, you can save 8 bytes: Try it online!

– ETHproductions – 2018-10-15T18:16:16.347

83 bytes. Although you should either add _=> to the beginning to make it a function or make this a REPL solution. – Shaggy – 2018-10-15T22:56:45.763

87 bytes (n=999)=>[...Array(n)].map(_=>n+=9).filter(_=>/0/.test(_)&eval([..._+''].join\+`)==9)` – guest271314 – 2018-10-22T14:07:28.263

5

JavaScript (ES6), 78 73 bytes

Saved 2 bytes thanks to @KevinCruijssen

Returns a space-separated string.

f=(n=9e3)=>n>999?f(n-9)+(eval([...n+''].join`+`)&/0/.test(n)?n+' ':''):''

Try it online!

How?

We iterate over the range \$[1008..9000]\$ with an increment of \$9\$, ignoring numbers that don't have a \$0\$.

All these numbers are multiples of \$9\$, so the sum of their digits is guaranteed to be a multiple of \$9\$ as well.

Because valid numbers have at least one \$0\$, they have no more than two \$9\$'s, which means that the sum of the remaining digits is at most \$18\$. Therefore, it's enough to test if the sum of the digits is odd.

Hence the test:

(eval([...n + ''].join`+`) & /0/.test(n)

Arnauld

Posted 2018-10-15T13:50:36.240

Reputation: 111 334

You can save a byte changing the 1008 to 999, since it doesn't contain a 0 anyway, and 999+9 = 1008. – Kevin Cruijssen – 2018-10-15T15:13:38.917

Or even 2 bytes by changing it to f=(n=9e3)=>n<1e3?'':(eval([...n+''].join`+`)<10&/0/.test(n)?[n,,]:'')+f(n-9) (does contain a trailing comma though, so f=(n=9e3)=>n<1e3?'':(eval([...n+''].join`+`)<10&/0/.test(n)?n+' ':'')+f(n-9) with space delimiter including trailing space might look prettier) – Kevin Cruijssen – 2018-10-15T15:17:44.803

@KevinCruijssen Thanks! I'm actually trying to update this for a while, but I have like 500B/s of Internet bandwidth where I am tonight. :/ – Arnauld – 2018-10-15T16:42:22.220

I know the feeling.. Lately our internet at home is crap for some reason.. Can't download anything above 10 MB, and sometimes have to refresh videos or pages with 10+ images a few times before it completely loads.. Really annoying when I'm working from home on Mondays/Tuesdays.. >.> Tomorrow someone comes to fix it (and I'm not leaving him until it's fixed xD) – Kevin Cruijssen – 2018-10-15T16:44:09.647

5

Python 2, 57 bytes

n=999
exec"n+=9\nif'0'in`n`>int(`n`,11)%10>8:print n\n"*n

Try it online!

2 bytes thanks to Dennis

Uses an exec loop to counts up n in steps of 9 as 1008, 1017, ..., 9981, 9990, printing those that meet the condition.

Only multiples of 9 can have digit sum 9, but multiples of 9 in this range can also have digits sum of 18 and 27. We rule these out with the condition int(`n`,11)%10>8. Interpreting n in base 11, its digit sum is equal to the number modulo 10, just like in base 10 a number equals its digit sum modulo 9. The digits sum of (9, 18, 27) correspond to (9, 8, 7) modulo 10, so taking those>8 works to filter out nines.

The number containing a zero is check with string membership. '0'in`n`. This condition is joined with the other one with a chained inequality, using that Python 2 treats strings as greater than numbers.

xnor

Posted 2018-10-15T13:50:36.240

Reputation: 115 687

I like how heavily golfed Python seems to often have enormously long autogenerated executables... – J.Doe – 2018-10-16T09:13:52.810

4

sed and grep (and seq), 72 64 63 bytes

seq 9e3|sed s/\\B/+/g|bc|grep -wn 9|sed s/:9//|grep 0|grep ....

Thor

Posted 2018-10-15T13:50:36.240

Reputation: 2 526

Some of these aren't four digits long (but I'm not sure what the final grep is, so maybe I'm running it wrong?) – Sparhawk – 2018-10-16T01:06:14.903

@Sparhawk: The last grep ensures that the number is 4 digits long – Thor – 2018-10-16T15:58:15.810

@Thor Ah right. For some reason I parsed that as an ellipsis. – Sparhawk – 2018-10-16T20:58:52.810

4

Haskell, 55 bytes

[i|i<-show<$>[1..5^6],201==sum(fromEnum<$>i),elem '0'i]

Thanks to @Laikoni, see the comments.

Readable:

import Data.Char (digitToInt)

[i | i <- show <$> [1000..9999]
   , sum (digitToInt <$> i) == 9
   , '0' `elem` i
   ]

mb21

Posted 2018-10-15T13:50:36.240

Reputation: 141

2Welcome to PPCG and Haskell golfing in particular! You can save a few bytes by dropping (-48+) and comparing the sum against 201 instead of 9. Incidentally this also allows you to use 1 instead of 1000 for the range. – Laikoni – 2018-10-16T12:10:41.820

Also your previous version without main=print was fine as per this consensus on Meta.

– Laikoni – 2018-10-16T12:13:18.363

9999 can be 5^6 instead. – Laikoni – 2018-10-16T13:38:50.247

1Ha, there's always another byte to shave! Thanks :-) – mb21 – 2018-10-16T13:56:30.083

3

R, 82 bytes

write((x=t(expand.grid(1:9,0:9,0:9,0:9)))[,colSums(x)==9&!apply(x,2,all)],1,4,,"")

Try it online!

Generates a matrix x of all possible 4-digit numbers, excluding leading zeros, going down columns. Then filters for column (digital) sums of 9 and containing zero, i.e., not all are nonzero. write prints down the columns, so we write to stdout with a width of 4 and a separator of "".

Outgolfed by J.Doe

Giuseppe

Posted 2018-10-15T13:50:36.240

Reputation: 21 077

Nice answer! I came up with a different route... – J.Doe – 2018-10-15T14:17:51.367

3

Japt, 20 18 bytes.

-2 bytes thanks to @Shaggy and @ETHproductions

A³òL² f_=ì)x ¥9«Z×

A³òL² f_=ì)x ¥9«Z×  Full program
A³òL²               Range [1000, 10000]
      f_            Filter by : 
        =ì)         Convert to array 
           x ¥9     Sum equal to 9?
               «    And 
                Z×  Product not 0

Try it online!

Luis felipe De jesus Munoz

Posted 2018-10-15T13:50:36.240

Reputation: 9 639

This is actually 28 bytes. Using a literal integer instead is 22 bytes but A³ò9000 f_ìx ¥9©ZsøT gets you back down to 20.

– Shaggy – 2018-10-15T17:28:22.230

1

You can save 1 byte by using ì instead of s and ¬, which has to be done in the filter: f_=ì)x ¥9.... Then you can save another by checking if the product of Z is zero with «Z×: Try it online!

– ETHproductions – 2018-10-15T17:59:35.880

3

05AB1E, 15 13 12 10 bytes

₄4°ŸεW°ö9Q

-2 bytes thanks to @Emigna
-3 bytes thanks to @Grimy

Try it online.

Explanation:

₄4°Ÿ        # Create a list in the range [1000,10000]
    ʒ       # Filter this list by:
     W      #  Get the smallest digit in the number (without popping the number itself)
      °     #  Take 10 to the power this digit
       ö    #  Convert the number from this base to an integer (in base-10)
        9Q  #  Check if it's equal to 9
  • If the smallest digit is \$d=0\$ it will become \$1\$ with the \$10^d\$ (°). And the number in base-1 converted to an integer in base-10 (ö) would act like a sum of digits.
  • If the smallest digit is \$d=1\$ it will become \$10\$ with the \$10^d\$ (°). And the number in base-10 converted to an integer in base-10 (ö) will of course remain the same.
  • If the smallest digit is \$d=2\$ it will become \$100\$ with the \$10^d\$ (°). And the number in base-100 convert to an integer in base-10 (ö) would act like a join with 0 in this case (i.e. 2345 becomes 2030405).
  • If the smallest digit is \$d=3\$ it will become \$1000\$ with the \$10^d\$ (°). And the number in base-100 convert to an integer in base-10 (ö) would act like a join with 00 in this case (i.e. 3456 becomes 3004005006).
  • ... etc. Smallest digits \$d=[4,9]\$ would act the same as \$d=2\$ and \$d=3\$ above, with \$d-1\$ amount of 0s in the 'join'.

If the smallest digit is \$>0\$ with the given range \$[1000,10000]\$, the resulting number after °ö would then be within the range \$[1111,9000000009000000009000000009]\$, so can never be equal to \$9\$. If the result is equal to \$9\$ (9Q) it would mean the smallest digit is \$d=0\$, resulting in a base-1 with °ö; and the sum of the digits was \$9\$.

Kevin Cruijssen

Posted 2018-10-15T13:50:36.240

Reputation: 67 575

1₄4°Ÿʒ0å}ʒSO9Q. Splitting filters are usually shorter – Emigna – 2018-10-15T15:58:00.310

@Emigna Ah, I was looking for a shorter way for the range, but completely forgot about . Thanks. And you're indeed right that multiple loose filters (at the end) are shorter. Will also add it to one of my tip answers. Thanks for both bytes! – Kevin Cruijssen – 2018-10-15T16:12:37.147

A strictly hypothetical solution is ∞4úʒ0å}ʒSO9Q (12 bytes), but I don't think it should count as valid so I'll leave it here as-is. – Mr. Xcoder – 2018-10-16T04:55:12.500

1And my other 13-byter (inspired by the ord sum == 201 trick) is 4°Lʒ0å}ʒÇOт·-. Leaving this here, maybe someone can golf it further – Mr. Xcoder – 2018-10-16T05:06:07.080

@Mr.Xcoder Added it to the answer. Found another 13-bytes alternative as well: ₄4°ŸʒW_sSO9Q*. – Kevin Cruijssen – 2018-10-16T07:23:32.610

1₄4°ŸʒD0åôO9Q. Using a single filter is usually shorter. – Grimmy – 2019-05-07T12:52:00.887

@Grimy Oh, very smart using the ô to convert the number to a list of digits right away! – Kevin Cruijssen – 2019-05-07T13:00:43.100

Alternative 12: 4°LʒW.$ÇOт·-. Sad .$ is a two-byte built-in. – Grimmy – 2019-05-07T13:18:39.913

1Nevermind, here's a 10: ₄4°ŸʒW°ö9Q – Grimmy – 2019-05-07T13:21:42.653

@Grimy Dang.. I don't even recognize my own answer in it anymore, haha. Very impressive! You've even beat Dennis' 11-byter Jelly answer. – Kevin Cruijssen – 2019-05-07T13:53:22.110

3

Java 8, 128 117 115 bytes

v->{int i=109,r[]=new int[i],n=i;for(;i>0;n++)if((n+"").chars().sum()==201&(n+"").contains("0"))r[--i]=n;return r;}

-11 bytes thanks to @nwellnhof.

Try it online.

Explanation:

v->{                              // Method with empty unused parameter & int-array return
  int i=109,                      //  Index-integer, starting at 109
      r[]=new int[i],             //  Result-array of size 109
      n=i;                        //  Number integer, starting at 109
   for(;i>0;                      //  Loop as long as `i` is not 0 yet:
       n++)                       //    After every iteration, increase `n` by 1
     if((n+"").chars().sum()==201 //   If the sum of the unicode values of `n` is 201,
                                  //   this means there are four digits, with digit-sum = 9
        &(n+"").contains("0"))    //   and `n` contains a 0:
       r[--i                      //    Decrease `i` by 1 first
            ]=n;                  //    And put `n` in the array at index `i`
  return r;}                      //  Return the array as result

Kevin Cruijssen

Posted 2018-10-15T13:50:36.240

Reputation: 67 575

1What about chars().sum()==201? – nwellnhof – 2018-10-15T15:33:12.153

@nwellnhof Ah, of course. Thanks! – Kevin Cruijssen – 2018-10-15T15:37:41.483

3

R, 85 bytes

(just competing for the best abuse of R square brackets ... :P )

`[`=`for`;i[a<-0:9,j[a,k[a,w[a,if(sum(s<-c(i,j,k,w))==9&any(!s)&i)write(s,1,s='')]]]]

Try it online!

digEmAll

Posted 2018-10-15T13:50:36.240

Reputation: 4 599

1Holy for loops, Batman! – BLT – 2018-10-18T17:43:10.500

2

Octave, 49 bytes

6 bytes saved using a more convenient output format as suggested by J.Doe.

Thanks to @Laikoni for a correction.

y=dec2base(x=1e3:9999,10)'-48;x(sum(y)==9>all(y))

Try it online!

Luis Mendo

Posted 2018-10-15T13:50:36.240

Reputation: 87 464

I don't know anything about Octave, but it looks like you can leave the disp off... – J.Doe – 2018-10-15T14:48:36.827

@J.Doe OP has confirmed that that output format is acceptable. Thanks for the suggestion! – Luis Mendo – 2018-10-15T14:57:45.907

2

APL(Dyalog), 33 29 bytes

1e3+⍸(0∘∊∧9=+/)¨⍎¨∘⍕¨1e3+⍳9e3

-4 bytes thanks to @Adam

Try it online!

Quintec

Posted 2018-10-15T13:50:36.240

Reputation: 2 801

29: 1e3+⍸(0∘∊∧9=+/)¨⍎¨∘⍕¨1e3+⍳9e3

– Adám – 2018-10-16T05:56:29.603

2

PHP, 69, 87 bytes 74 bytes

for($i=999;$i<9001;$i++){echo((array_sum(str_split($i))==9&strpos($i,"0")!=0)?$i:" ");} for($i=999;$i++<1e4;)echo!strpos($i,48)|array_sum(str_split($i))-9?" ":$i;

Note this puts a space for every "failed" number, leading to some kind of funky spacing. This can be changed to comma separation, but will add another 4 characters: ?$i.",":""

Got bigger because I wasn't checking for 0. Derp. Shortened by 13 by Titus!

aslum

Posted 2018-10-15T13:50:36.240

Reputation: 251

2I don't really know PHP, but does this code ensure that each year contains a zero? – Laikoni – 2018-10-16T00:13:24.403

This code does not check for zero in the number. – krzysiej – 2018-10-16T11:29:12.983

113 bytes shorter: for($i=999;$i++<1e4;)echo!strpos($i,48)|array_sum(str_split($i))-9?" ":$i; – Titus – 2018-10-18T15:29:12.883

Here´s another byte: ?"$i,":"" er ... now the other way round: ?"":"$i," – Titus – 2018-10-18T21:06:22.777

Actually @Titus that adds a couple bytes. We don't need quotes around $i unless we're including a string w/ it. – aslum – 2018-10-22T12:40:53.753

That´s for the comma separated version: $i."," has six bytes; "$i," has five. – Titus – 2018-10-23T10:21:41.883

Fair enough, though OP said the huges amount of spaces was fine, so no need for the comma. – aslum – 2018-10-23T12:58:34.520

2

Ruby, 46 42 41 bytes

?9.upto(?9*4){|x|x.sum==201&&x[?0]&&p(x)}

Try it online!

How it works:

  • Iterate on strings ranging from '9' to '9999'
  • Check that sum of ASCII values is 201
  • Check if string contains a zero (without regex, a regex would be 1 byte longer)

(Thanks Laikoni for -2 bytes)

G B

Posted 2018-10-15T13:50:36.240

Reputation: 11 099

19*3 can be just 9, because checking against 201 already requires 4 digit numbers. – Laikoni – 2018-10-16T00:01:59.297

2

Pip, 18 bytes

{0Na&$+a=9}FIm,t*m

Use an ouput-format flag such as -p to get readable output. Try it online!

{0Na&$+a=9}FIm,t*m
             m,t*m  Range from 1000 to 10*1000
{         }FI       Filter on this function:
 0Na                 There is at least one 0 in the argument
    &                and
     $+a             The sum of the argument
        =9           equals 9

DLosc

Posted 2018-10-15T13:50:36.240

Reputation: 21 213

2

Wolfram Language (Mathematica), 56 55 bytes

Select[9!!~Range~9999,Tr@#==Times@@#+9&@*IntegerDigits]

Try it online!

We test the range from 9!! = 945 to 9999, since there are no results between 945 and 999. Maybe there's a shorter way to write a number between 9000 and 10007, as well.

Tr@#==Times@@#+9& applied to {a,b,c,d} tests if a+b+c+d == a*b*c*d+9, which ends up being equivalent to The Anno Condition.

Misha Lavrov

Posted 2018-10-15T13:50:36.240

Reputation: 4 846

In retrospect, 9!! isn't any shorter than 999 or something, but it beats 1000. – Misha Lavrov – 2018-10-16T00:37:44.020

What is 9!! ? In guessing it isnt related to factorials. – Robert Fraser – 2018-10-16T06:49:43.817

@RobertFraser Double factorial: 9*7*5*3*1. – Misha Lavrov – 2018-10-16T14:16:21.087

2

Dart,  103 100  96 bytes

f()=>List.generate(9001,(i)=>'$i').where((i)=>i.contains('0')&&i.runes.fold(0,(p,e)=>p+e)==201);

  • -3 bytes by setting the value in the array to string, making the conversion once and not twice
  • -4 bytes by using runes instead of codeUnits
  • Pretty self-explanatory. generates a list of 9001 (0-9000) cells with the cell's index as value, filters the ones containing a 0 then the one having an ASCII sum of 201 (The result if all the ASCII characters sum to 9). These conditions implictly include that the year is 4 digits long because using 2 ASCII numbers (and the 0), you cannot reach 201.

    Try it on Dartpad!

    Elcan

    Posted 2018-10-15T13:50:36.240

    Reputation: 913

    Welcome to PPCG. :) – Laikoni – 2018-10-16T13:36:54.100

    1Thanks ! Been lurking for a while, can finally participate – Elcan – 2018-10-16T13:43:45.737

    2

    Bash (with seq, grep), 39 bytes

    seq 0 9 1e4|egrep '([0-4].*){3}'|grep 0
    

    Try it online!

    tsh

    Posted 2018-10-15T13:50:36.240

    Reputation: 13 072

    @xobzoo suggested seq 0 9 1e4|awk '/([0-4].*){3}/&&/0/' to save two bytes. – Timtech – 2018-10-21T03:03:39.503

    2

    K (ngn/k), 22 bytes

    55_&(|/~a)&9=+/a:!4#10
    

    Try it online!

    ngn

    Posted 2018-10-15T13:50:36.240

    Reputation: 11 449

    Nice! 55_&9=+/y*|/'~y:!4#10 for 21? – streetster – 2018-10-17T23:21:03.437

    1@streetster thanks :) the ' in |/' looks wrong. the result includes 1116, 1125, 1134, etc which are not supposed to be there – ngn – 2018-10-18T06:43:35.817

    2

    APL (Dyalog Unicode), 23 bytes

    55↓⍸(×⌿<9=+⌿)10⊥⍣¯1⍳9e3
    

    Try it online!

    FrownyFrog

    Posted 2018-10-15T13:50:36.240

    Reputation: 3 112

    that use of < is clever. you can make it even shorter with ⎕io←0 and ( )10⊥⍣¯1⍳9e3 -> ( )¨,⍳4⍴10 – ngn – 2018-11-01T16:14:06.930

    2

    Scala (76 63 61 56 bytes)

    for(n<-0 to 9000;t=n+""if t.sum==201&t.min<49)println(t)
    

    Try it online

    • Thanks to Laikoni for the suggestions
    • Two more bytes shed after applying Jo King's comment

    jrook

    Posted 2018-10-15T13:50:36.240

    Reputation: 211

    1

    Welcome to PPCG! Do you have an idea what needs to be added to the header or footer section to get this code to run on TIO? Try it online!

    – Laikoni – 2018-10-18T21:18:25.953

    @Laikoni, didn't know I could run Scala in TIO. Fixed it. Thanks for the comment. – jrook – 2018-10-18T22:40:24.827

    1It looks like t.sum==201 works instead of t.map(_.asDigit).sum==9. – Laikoni – 2018-10-18T23:00:14.287

    You may find our tips for golfing in Scala interesting. E.g. it looks like s"$n" can be n+"" and s"$t " can be t+" ".

    – Laikoni – 2018-10-20T10:33:06.577

    1Since you're using the sum is 201 trick, the range doesn't need to start at 999 – Jo King – 2018-10-24T11:16:30.363

    @JoKing, thanks, I updated the answer – jrook – 2018-10-24T16:17:02.400

    1

    Tcl, 77 bytes

    time {if [incr i]>1e3&[regexp 0 $i]&9==[join [split $i ""] +] {puts $i}} 9999
    

    Try it online!

    sergiol

    Posted 2018-10-15T13:50:36.240

    Reputation: 3 055

    1

    J, 38 bytes

    -1 byte and bug fixed thanks to Quintec

    echo(#~(4#10)(0&e.*9=+/)@#:])1e3+i.9e3
    

    Try it online!

    Thanks to Laikoni for findng an even bigger bug!

    Galen Ivanov

    Posted 2018-10-15T13:50:36.240

    Reputation: 13 815

    1000 can be 1e3 for a byte saved, right? – Quintec – 2018-10-15T17:12:49.810

    1(Also, should it be 9e3 instead of 9e4?) – Quintec – 2018-10-15T17:17:11.747

    @Quintec Yes, sure. I tried it, but forgot it (golfing on the phone) – Galen Ivanov – 2018-10-15T17:18:07.103

    @Laikoni As in my Red answer, I forgot this, sorry! Fixed. – Galen Ivanov – 2018-10-16T06:29:04.350

    1

    Japt, 16 bytes

    Returns an array of digit arrays.

    L²õì l4 k_ת9aZx
    

    Test it


    Explanation

    L                    :100
     ²                   :Squared
      õ                  :Range [1,L²]
       ì                 :Convert each to a digit array
         l4              :Filter elements of length 4
            k_           :Remove each Z that returns truthy (not 0)
              ×          :  When reduced by multiplication
               ª         :  OR
                  Zx     :  When reduced by addition
                9a       :   And subtracted from 9
    

    Shaggy

    Posted 2018-10-15T13:50:36.240

    Reputation: 24 623

    1OP has ruled that digit arrays aren't valid output unfortunately :o( – Sok – 2018-10-16T08:12:43.330

    1

    APL(NARS), 45 chars, 90 bytes

    f←{⍵×⍳(0∊x)∧9=+/x←⍎¨⍕⍵}⋄f¨1e3..5e3⋄f¨5e3..9e3
    

    test afther some formatting:

    1008  1017  1026  1035  1044  1053  1062  1071  1080  1107  1170  1206  1260  
      1305  1350  1404  1440  1503  1530  1602  1620  1701  1710  1800  2007  2016  
      2025  2034  2043  2052  2061  2070  2106  2160  2205  2250  2304  2340  
      2403  2430  2502  2520  2601  2610  2700  3006  3015  3024  3033  3042  3051  
      3060  3105  3150  3204  3240  3303  3330  3402  3420  3501  3510  3600  
      4005  4014  4023  4032  4041  4050  4104  4140  4203  4230  4302  4320  4401  
      4410  4500 
    5004  5013  5022  5031  5040  5103  5130  5202  5220  5301  5310  5400  6003  
      6012  6021  6030  6102  6120  6201  6210  6300  7002  7011  7020  7101  7110  
      7200  8001  8010  8100  9000 
    

    possible alternative

    r←f;i;x
       r←⍬⋄i←1e3⋄→B
    A: r←r,i
    B: i+←1⋄→A×⍳(0∊x)∧9=+/x←⍎¨⍕i⋄→B×⍳i≤9e3
    

    RosLuP

    Posted 2018-10-15T13:50:36.240

    Reputation: 3 036

    1

    Jelly, 13 bytes

    ȷ4µṢÄm3Ḍ)ẹ9ṫ4
    

    Try it online!

    How?

    ȷ4µṢÄm3Ḍ)ẹ9ṫ4 - Link: no arguments
    ȷ4            - literal 10^4 = 10000
      µ     )     - for each in range (1,2,3,...,10000): e.g. 3042       or  5211
       Ṣ          -   sort (given an integer makes digits)    [0,2,3,4]      [1,1,2,5]
        Ä         -   cumulative addition                     [0,2,5,9]      [1,2,4,9]
         m3       -   modulo 3 slice (1st,4th,7th...)         [0,9]          [1,9]
           Ḍ      -   convert from decimal digits             9              19
             ẹ9   - 1-based indices equal to nine             [9,99,999,1008,1017,...,8100,9000]
               ṫ4 - tail from the 4th index                   [1008,1017,...,8100,9000]
    

    Jonathan Allan

    Posted 2018-10-15T13:50:36.240

    Reputation: 67 804

    1

    Stax, 13 bytes

    ü┌o☻≈;╫▄mI░↨Z
    

    Run and debug it

    Unpacked, ungolfed, and commented, it looks like this.

    VM      Constant one million
            this program produces its output pretty quickly, but takes much longer to end
    f       filter the numbers [1..n] using the rest of the program as a predicate
      $     convert to ascii decimal string e.g. "1234"
      |+    sum of ascii codes
      201=  is equal to 201 [result a]
      _E:*  product of the decimal digits [result b]
      >     [result a] is greater than [result b]
    

    Run this one

    recursive

    Posted 2018-10-15T13:50:36.240

    Reputation: 8 616

    @Laikoni: I read the challenge wrong, and missed that requirement. I've updated my submission, at the cost of 4 bytes. – recursive – 2018-10-16T00:20:50.377

    1

    MathGolf, 16 13 bytes

    ♫♪↨Ç{▒ε*\Σ8=┌
    

    Try it online!

    Explanation:

      ↨              Range from 
    ♫♪               1000 to 10000
       Ç{            Filter out by
         ▒ε*         The product of all digits is not 0
            Σ8=┌     The digit sum of the loop index (0 based) is not equal to 8
    

    Jo King

    Posted 2018-10-15T13:50:36.240

    Reputation: 38 234

    1Does MathGolf have an ord command to transform characters/digits to unicode values? If yes, you might be able to use the same ord sum == 201 trick other answers use, which covers both the length == 4 and digit sum == 9 at the same time. – Kevin Cruijssen – 2018-10-16T08:02:31.267

    @KevinCruijssen Unfortunately, it doesn't, but I've requested it – Jo King – 2018-10-16T11:48:53.830

    @JoKing I'm a bit late to the party, but both of these things have been implemented, and have been submitted for pulling to TIO. check out the README for instructions. I've had a very stressful month, but I'm trying to get everything implemented. – maxb – 2018-11-27T10:18:55.240

    1

    JavaScript (SpiderMonkey), 57 bytes

    for(y=0;y<9e3;)/(?=([0-4].?){3}).*0/.test(y+=9)&&print(y)
    

    Try it online!


    JavaScript (SpiderMonkey), 58 bytes

    for(y=999;++y<1e4;9-p-q-r-s|q*r*s||print(y))[p,q,r,s]=''+y
    

    Try it online!

    You may change print to alert to test it in your browser. :)

    tsh

    Posted 2018-10-15T13:50:36.240

    Reputation: 13 072

    Maybe the first time I realize that submitting a whole program is even shorter than a function in JavaScript... – tsh – 2018-10-16T08:19:11.560

    1

    K (oK), 37 bytes

    x@&{9=+/x*0in x}'(4#10)\'x:1000+!9000
    

    Try it online!

    On mobile, will add explanation later. Couldn't get 0 in' to work without the lambda which wastes bytes...

    streetster

    Posted 2018-10-15T13:50:36.240

    Reputation: 3 635

    36 - x@&9=+/+y*|/~+y:(4#10)\'x:1000+!9000 – streetster – 2018-10-16T22:45:03.957

    1

    Common Lisp, 140 bytes

    (defun x()(loop for x from 1008 to 9000 for y =(map'list #'digit-char-p(prin1-to-string x))when(and(member 0 y)(=(apply #'+ y)9))collect x))
    

    Try it online!

    JRowan

    Posted 2018-10-15T13:50:36.240

    Reputation: 231

    This does not ensure that each year contains at least one zero. – Laikoni – 2018-10-18T21:27:18.047

    1Edited, i must have overlooked that – JRowan – 2018-10-21T22:36:11.877

    It looks like you can drop the space in map 'list. Also there are two trailing newlines that do not appear to be necessary. – Laikoni – 2018-10-22T05:40:04.100

    1

    Julia 1.0, 52 bytes

    f()=[x for x=1:9^5 if'0' in"$x"&&sum(Int,"$x")==201]
    

    Try it online!

    gggg

    Posted 2018-10-15T13:50:36.240

    Reputation: 1 715

    1

    K (ngn/k), 33 bytes

    t@&"0"=*'t@'<'t:$&201=+/'i:$!9001
    

    Try it online!

    Will edit with explanation later.

    Thaufeki

    Posted 2018-10-15T13:50:36.240

    Reputation: 421

    1

    C (clang), 95 91 bytes

    f(i){char*c;for(i=1e4;--i;*c+c[1]+c[2]+c[3]-201||index(c,48)&&puts(c))asprintf(&c,"%d",i);}
    

    Try it online!

    -4 bytes thanks to @ceilingcat

    Logern

    Posted 2018-10-15T13:50:36.240

    Reputation: 845

    1

    Haskell, 66 79 74 bytes

    drop 55[x>>=show|x<-sequence.replicate 4$[0..9],sum x==9,elem 0x]
    

    Damn you with your type juggling, you can treat numbers as strings :(

    Basically just an exact description of whats asked here. Just no spaces.

    Try it online! (Modifications for it to run were necessary, tio doesn't support pure functions in Haskell.)

    schuelermine

    Posted 2018-10-15T13:50:36.240

    Reputation: 151

    1Nice approach, but unfortunately lists of digits are not valid output. However, there a few ways you can save some bytes to make up for the additional ones you need for the final conversion to a string. First of all, a list comprehension is shorter than filter and you can drop the second space in elem 0 x. – Laikoni – 2018-10-22T22:01:54.943

    A few more: Instead of using map the conversion to string can be put into the list comprehension. concat.map is common enough that there exists a build-in called concatMap, however the overloading of the bind operator =<< on lists has the same effect and is even shorter. – Laikoni – 2018-10-23T14:13:49.327

    The default bind operator is (>>=), what you showed is the reverse bind operator. – schuelermine – 2018-10-24T09:59:09.690

    0

    Retina 0.8.2, 52 bytes

    
    9001$*
    .
    $.`=>$.`;
    >|\G\d
    $*
    M!`\d{4}(?==1{9};)
    G`0
    

    Try it online! Explanation:

    
    9001$*
    

    Insert 9001 1s.

    .
    $.`=>$.`;
    

    Replace each 1 with the number of 1s to its left, followed by =>, followed by the same number again, followed by ;.

    >|\G\d
    $*
    

    Convert > and subsequent digits to unary, thus calculating the digit sum.

    M!`\d{4}(?==1{9};)
    

    List all the 4-digit numbers with a digit sum of 9.

    G`0
    

    Keep those that contain a zero.

    Neil

    Posted 2018-10-15T13:50:36.240

    Reputation: 95 035

    0

    Jelly, 13 bytes

    ȷ4ȷrDS9Ƒ>ẠƲƇḌ
    

    Try it online!

    -2 (yucky, yucky trivial) bytes thanks to Dennis.

    Erik the Outgolfer

    Posted 2018-10-15T13:50:36.240

    Reputation: 38 134

    0

    Red, 109 bytes

    repeat n 8000[s: 0 p: 1 foreach t form d: 1000 + n[s: s + r: do t - 48 p: p * r]if all[s = 9 p = 0][print d]]
    

    Try it online!

    Galen Ivanov

    Posted 2018-10-15T13:50:36.240

    Reputation: 13 815

    @Laikoni Sorry, apparently I've missed that part. Fixed. – Galen Ivanov – 2018-10-16T06:20:39.393

    0

    Pyth, 22 19 bytes

    f&}\0Tq201sCMT`M^T4
    

    Output is an array of strings. Try it online here.

    f&}\0Tq201sCMT`M^T4   
                  `M^T4   Array of strings of numbers 0-999
    f                     Keep the elements of the above, as T, where:
               CMT          Get the code points of each character in T
              s             Take the sum
          q201              Does the above == 201?
      }\0T                  Does T contain "0" ?
     &                      Logical AND the two previous results
    

    The 201 trick is the same as used in other answers.

    Previous answer, without using 201 trick, 22 bytes: iRTf}0Tfq9sTjRTr^T3^T4

    Sok

    Posted 2018-10-15T13:50:36.240

    Reputation: 5 592

    0

    PHP, 114 bytes

    <?php
    for($i=1e3;$i<=9e3;$i++)
        if(array_sum($a=[$i/1e3%10,$i/100%10,$i/10%10,$i%10])==9&in_array(0,$a))
            echo $i.' ';
    

    Try it online!

    Mic1780

    Posted 2018-10-15T13:50:36.240

    Reputation: 121

    0

    Prolog 117 bytes

    b(X):-between(0,9,X). w(X):-write(X). y():-b(A),b(B),b(C),b(D),X is A+B+C+D,X=9,(A=0;B=0;C=0;D=0),w(A),w(B),w(C),w(D).

    Geo

    Posted 2018-10-15T13:50:36.240

    Reputation: 91

    1

    Would you consider adding a link to a service like Try it online! - (link is to your code, but I'm not sure if the footer is right since I don't prolog much.)?

    – Οurous – 2018-10-18T05:25:38.293

    98 – ASCII-only – 2018-11-14T05:07:16.757

    91 – ASCII-only – 2018-11-14T05:18:20.990

    0

    J, 35 bytes

    echo@>55}.I.(4#10)(*/<9=+/)@#:i.1e4
    

    Try it online!

    FrownyFrog

    Posted 2018-10-15T13:50:36.240

    Reputation: 3 112

    0

    C# (.NET Core), 106 bytes

    for(int i=999;++i<9999;)if(i.ToString().Sum(c=>c-'0')==9&&i.ToString().Contains("0"))Console.WriteLine(i);
    

    Try it online!

    Ungolfed:

    for(int i = 999; ++i < 9999;)               // from 1000 to 9999 (fulfills four digit parameter)
        if(i.ToString().Sum(c => c - '0') == 9  // if the sum of the digits is nine
                && i.ToString().Contains("0"))  //   and if the year contains at least one zero
            Console.WriteLine(i);               // write the year to the console
    

    Meerkat

    Posted 2018-10-15T13:50:36.240

    Reputation: 371

    0

    Perl 5 -MList::Util=sum, 40 32 bytes

    map/0/*9-(sum/./g)||say,1E3..9E3
    

    Try it online!

    Xcali

    Posted 2018-10-15T13:50:36.240

    Reputation: 7 671

    0

    Scala, 58 bytes

    jrook's solution is currently 2 byte shorter

    val v=(9 to 9000).filter(x=>(x+"").min<49&(x+"").sum==201)
    
    val v=(9 to 9000).map(_+"").filter(x=>x.min<49&x.sum==201)
    

    Try it online!

    Dr Y Wit

    Posted 2018-10-15T13:50:36.240

    Reputation: 511

    0

    Attache, 37 bytes

    {{Sum@Ords@_=201and"0"in _}@S\1:9000}
    

    Try it online!

    Based on the perl answer.

    Conor O'Brien

    Posted 2018-10-15T13:50:36.240

    Reputation: 36 228

    0

    Kotlin, 59 bytes

    Lambda that returns a List<Int> containing every valid year.

    {(1008..9000).filter{"$it".sumBy{it-'0'}==9&&'0' in "$it"}}
    

    Try it online!

    snail_

    Posted 2018-10-15T13:50:36.240

    Reputation: 1 982