Dans la série de jeux vidéo Anno, il y a 6 jeux dont un septième annoncé pour début 2019. Leurs titres figurent toujours une année dans un modèle spécifique:
Anno 1602, Anno 1503, Anno 1701, Anno 1404, Anno 2070, Anno 2205, Anno 1800
- La somme numérique est toujours 9.
- Les années ont quatre chiffres.
- Ils contiennent au moins un zéro.
Parmi ces contraintes, il existe 109 titres possibles:
[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]
Votre objectif est de les lister sous une forme raisonnable dans le plus petit nombre d’octets.
Réponses:
R ,
5951 octetsAffiche les numéros valides sous forme de noms d'une liste de 201. Pourquoi 201? Parce que ASCII 0 est 48, et 4 * 48 + 9 est ... oui. Enregistré 6 octets par aliasing
^
àMap
et un autre 2 en utilisant1:9e3
comme plage.Essayez-le en ligne!
Explication
la source
grep
pourquoi ne me souviens-je jamais qu'il jettecharacter
...Perl 6 ,
3533 octets-2 octets grâce à Jo King
Essayez-le en ligne!
la source
Python 2 ,
676664 octetsEssayez-le en ligne!
Enregistré:
la source
ord sum == 201
truc d'autres réponses.Gelée , 11 octets
Essayez-le en ligne!
Comment ça marche
la source
PowerShell ,
5049 octetsEssayez-le en ligne!
Construit une plage allant de
999
à10000
, puis utilise inline-match
comme filtre pour extraire les entrées avec lesquelles la regex correspond0
. Cela nous laisse avec1000, 1001, 1002, etc.
Nous canalisons ensuite cela dans uneWhere-Object
clause où nous prenons le nombre actuel comme une chaîne"$_"
, lechar
transposons comme un tableau,-join
ces caractères avec+
etI
nvoke-Ex
(similaire à eval) pour arriver à leur somme. Nous vérifions que ce soit-eq
uel à9
, et s'il est passé sur le pipeline. À la fin du programme, ces chiffres sont extraits du pipeline et sortent implicitement.la source
JavaScript (ES6),
7873 octets2 octets sauvés grâce à @KevinCruijssen
Retourne une chaîne séparée par des espaces.
Essayez-le en ligne!
Comment?
Nous parcourons l'intervalle[1008..9000] avec un incrément de 9 , en ignorant les nombres qui n'ont pas de 0 .
Tous ces nombres sont des multiples de9 , la somme de leurs chiffres est donc également un multiple de 9 .
Comme les nombres valides ont au moins un0 , ils n’ont pas plus de deux 9 , ce qui signifie que la somme des chiffres restants est au maximum de 18 . Par conséquent, il suffit de vérifier si la somme des chiffres est impair.
D'où le test:
la source
1008
à999
, car il ne contient pas de 0 de toute façon, et999+9 = 1008
.f=(n=9e3)=>n<1e3?'':(eval([...n+''].join`+`)<10&/0/.test(n)?[n,,]:'')+f(n-9)
(contient une virgule de fin, cependant,f=(n=9e3)=>n<1e3?'':(eval([...n+''].join`+`)<10&/0/.test(n)?n+' ':'')+f(n-9)
avec un délimiteur d'espace incluant l'espace de fin peut paraître plus joli)JavaScript (Node.js) , 89 octets
Essayez-le en ligne!
JavaScript (Node.js)
12912712612411511411111010597939290 octetsEssayez-le en ligne!
Explication
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
la source
[...Array(9e3)]
insteadArray(9e3).fill()
saves 2 bytes.map(a=>+a)
instead.map(Number)
saves another byte(_, i)
to save a byte, ands[0]+s[1]+s[2]+s[3]
can beeval(s.join`+`)
to save an additional 4 bytes.||
can be|
in your answer..map()
only to generate the range, and keep the filtering separate, you can save 8 bytes: Try it online!Python 2, 57 bytes
Try it online!
2 bytes thanks to Dennis
Uses an
exec
loop to counts upn
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
. Interpretingn
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.la source
sed and grep (and seq),
726463 bytesla source
grep
is, so maybe I'm running it wrong?)Haskell, 55 bytes
Thanks to @Laikoni, see the comments.
Readable:
la source
(-48+)
and comparing the sum against201
instead of9
. Incidentally this also allows you to use1
instead of1000
for the range.main=print
was fine as per this consensus on Meta.9999
can be5^6
instead.R, 82 bytes
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., notall
are nonzero.write
prints down the columns, so wewrite
tostdout
with a width of4
and a separator of""
.Outgolfed by J.Doe
la source
Japt,
2018 bytes.-2 bytes thanks to @Shaggy and @ETHproductions
Try it online!
la source
A³ò9000 f_ìx ¥9©ZsøT
gets you back down to 20.ì
instead ofs
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!Java 8,
128117115 bytes-11 bytes thanks to @nwellnhof.
Try it online.
Explanation:
la source
chars().sum()==201
?R, 85 bytes
(just competing for the best abuse of R square brackets ... :P )
Try it online!
la source
05AB1E,
15131210 bytes-2 bytes thanks to @Emigna
-3 bytes thanks to @Grimy
Try it online.
Explanation:
°
). And the number in base-1 converted to an integer in base-10 (ö
) would act like a sum of digits.°
). And the number in base-10 converted to an integer in base-10 (ö
) will of course remain the same.°
). And the number in base-100 convert to an integer in base-10 (ö
) would act like a join with0
in this case (i.e.2345
becomes2030405
).°
). And the number in base-100 convert to an integer in base-10 (ö
) would act like a join with00
in this case (i.e.3456
becomes3004005006
).0
s in the 'join'.If the smallest digit is>0 with the given range [1000,10000] , the resulting number after [1111,9000000009000000009000000009] , so can never be equal to 9 . If the result is equal to 9 (d=0 , resulting in a base-1 with 9 .
°ö
would then be within the range9Q
) it would mean the smallest digit is°ö
; and the sum of the digits wasla source
₄4°Ÿʒ0å}ʒSO9Q
. Splitting filters are usually shorter4°
. 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!4°Lʒ0å}ʒÇOт·-
. Leaving this here, maybe someone can golf it further₄4°ŸʒD0åôO9Q
. Using a single filter is usually shorter.₄4°ŸʒW°ö9Q
Pip, 18 bytes
Use an ouput-format flag such as
-p
to get readable output. Try it online!la source
Wolfram Language (Mathematica),
5655 bytesTry 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 ifa+b+c+d == a*b*c*d+9
, which ends up being equivalent to The Anno Condition.la source
9*7*5*3*1
.Ruby,
46 4241 bytesTry it online!
How it works:
(Thanks Laikoni for -2 bytes)
la source
9*3
can be just9
, because checking against 201 already requires 4 digit numbers.Octave, 49 bytes
6 bytes saved using a more convenient output format as suggested by J.Doe.
Thanks to @Laikoni for a correction.
Try it online!
la source
disp
off...Dart,
103 10096 bytesPretty 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!
la source
Bash (with
seq
,grep
), 39 bytesTry it online!
la source
seq 0 9 1e4|awk '/([0-4].*){3}/&&/0/'
to save two bytes.K (ngn/k), 22 bytes
Try it online!
la source
55_&9=+/y*|/'~y:!4#10
for 21?'
in|/'
looks wrong. the result includes 1116, 1125, 1134, etc which are not supposed to be thereAPL (Dyalog Unicode), 23 bytes
Try it online!
la source
<
is clever. you can make it even shorter with⎕io←0
and(
)10⊥⍣¯1⍳9e3
->(
)¨,⍳4⍴10
PHP,
69, 87 bytes74 bytesfor($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!
la source
for($i=999;$i++<1e4;)echo!strpos($i,48)|array_sum(str_split($i))-9?" ":$i;
?"$i,":""
er ... now the other way round:?"":"$i,"
APL(Dyalog),
3329 bytes-4 bytes thanks to @Adam
Try it online!
la source
1e3+⍸(0∘∊∧9=+/)¨⍎¨∘⍕¨1e3+⍳9e3
Scala (
76636156 bytes)Try it online
la source
t.sum==201
works instead oft.map(_.asDigit).sum==9
.s"$n"
can ben+""
ands"$t "
can bet+" "
.Tcl, 77 bytes
Try it online!
la source
Japt, 16 bytes
Returns an array of digit arrays.
Test it
Explanation
la source
APL(NARS), 45 chars, 90 bytes
test afther some formatting:
possible alternative
la source
Jelly, 13 bytes
Try it online!
How?
la source