ZADANIE
wypisz liczby całkowite n, gdzie 12 <= n <= 123456789
i wszystkie pary kolejnych cyfr in mają tę samą dodatnią różnicę między nimi (np. 2468, ale nie 2469).
BRAK WEJŚCIA.
Wynik:
12
13
14
15
16
17
18
19
23
24
25
26
27
28
29
34
35
36
37
38
39
45
46
47
48
49
56
57
58
59
67
68
69
78
79
89
123
135
147
159
234
246
258
345
357
369
456
468
567
579
678
789
1234
1357
2345
2468
3456
3579
4567
5678
6789
12345
13579
23456
34567
45678
56789
123456
234567
345678
456789
1234567
2345678
3456789
12345678
23456789
123456789
Zasady
- Obowiązują standardowe luki.
- brak wejścia
najkrótszy kod wygrywa.
Kredyty anarchii golfa
Odpowiedzi:
Jelly,
1211 bytesTry it online!
How it works
źródło
ìà Find fastest route between two points using Dykstra's Algorithm
Python 2, 81 bytes
Try it online!
My solution from anarchy golf. The idea is to iterate over all possible triples of length, start value, and step, which gives sorted outputs. The triple is encoded as a value
r
from72
to647
, and the components are extracted ask/72
,k/8%9
, andk%8
. Startingk
high enough avoids single-digit numbers from being output.xsot saved two bytes off this by replacing the
range
with a hardcoded string of digits'123456789'
.This was written under the constraint of a two second runtime limit. A slower strategy that filters numbers rather than generating these may be shorter.
źródło
1
to123456789
, instead forcing answers to come up with some clever way to generate the right numbers in the right (sorted) order.C,
166152 Bytes6 bytes saved thanks to @KevinCruijssen!
8 bytes saved thanks to @JonathanFrech!
Try it online
The fully formatted version of the above code can be seen below.
źródło
while(i<123456789)
bewhile(i<=123456789)
instead according to the challenge range? Also, you can golf it by 6 bytes:p,l,d,i=11;main(){for(char s[10];i<=123456789;){sprintf(s,"%d",++i);p=0;for(l=strlen(s);--l>0;){d=s[l]-s[l-1];if(p<1)p=d;if(p^d|d<1)break;p=d;}if(l<1)puts(s);}}
i<1e9
.l<1
can be golfed to!l
, asl
never reaches a negative value.i<1e9
. And!l
whenl
is always>=0
sounds reasonable for C I guess (I've never programmed in C myself).Jelly,
14, 13 bytesTry it online!
One byte saved thanks to @MrXcoder!
This is extremely inefficient, so it'll time out on TIO, but if it ever finishes, it will produce the correct output. You can try it with smaller numbers here: Try it online!
Explanation:
źródło
$
at the end of your helper link.DIµ>0ȦȧE¶Ç77#
05AB1E, 23 bytes
Try it online!
Replace
•7=›ζ•
with 7000 to have it finish on TIO, or just hit the "terminate" button before it times out, resulting in the numbers printed up until that point.źródło
žh
'0123456789'
,1357
for example is also a valid number you need to output.•7=›ζ•
Mathematica, 79 bytes
Try it online! with a lower number because it is very slow
here is another approach that constructs all the numbers in 1sec
Mathematica, 123 bytes
Try it online! all numbers in a sec
źródło
Husk,
1413 bytesPrints newline-separated numbers to STDOUT. Try it online!
-1 byte due to inspiration from H.PWiz.
Explanation
źródło
Wolfram Language (Mathematica), 76 bytes
Try it online!
źródło
APL (Dyalog),
3728 bytesTry it online! (with shorter range, due to time-out)
How?
x←11+⍳123456789
-11, 12... 1e9
intox
¨
- for each⍎¨⍕⍵
- break into digits2-/
- get differences list∪
- get unique elements1=≢
- length == 1?x/⍨
- use this as a mask on the range created⍪
- and columnifyźródło
Husk, 14 bytes
Try it online!
źródło
≠
. :P I had forgotten it works for characters tooBatch,
210200 bytesNo optimizations, so very slow - takes about 25 seconds until 12345, so for the complete output you'd have to wait about 3 days.
źródło
Java 8,
169168145 bytesPort of @Jacobinski C answer (after I golfed it a bit).
-23 bytes thanks to @Nevay.
Explanation:
Try it here. (It's a bit too slow near the end, so doesn't print the final number on TIO. It prints the final number locally in about 20 seconds, though.)
źródło
v->{byte[]a;for(int i=9,p,l;++i<1e9;System.out.print(l<1?i+"\n":""))for(a=(i+"").getBytes(),p=0,l=a.length;--l>0&&p*(p^(p=a[l]-a[l-1]))<1&p>0;);}
break
somehow and add it to the for-loop check, but this I wouldn't have come up with myself. ;) Thanks!05AB1E, 14 bytes
Try it online!
źródło
12žhŸʒS¥D0›PsË&
, I can't get it to run locally. Can you get this to actually execute?Ÿ
, it works finePython 2,
1039795 bytes-2 bytes thanks to @JonathanFrech
Try it online!
źródło
`n`[1:]
.Python 2,
7675 bytesTakes about 3 minutes locally.
Try it online! (modified to print all numbers but the last)
źródło
JavaScript (Firefox 30-57), 105 bytes
Loops over lengths from 2 to 10 (x is the index of the last character and therefore 1 less than the length), starting digits from 1 to 9 and step from 1 to 9, then filters on the end digit being less than 10 and if so generates the result by filtering out digits from the digit string.
źródło
Uncaught SyntaxError: Unexpected token for
Pyth, 21 bytes
Port of Dennis' clever approach.
Try it here!
Pyth, 23 bytes
This times out on the online interpreter, but works if given enough time and memory.
Try it online with a lower number.
źródło
MATL,
1716 bytesTry it online! with
1e9
replaced by1e3
so that it doesn't time out in the online compiler.źródło
JavaScript (ES6), 121 bytes
Not nearly as short as Neil's answer, but I thought it was still worth posting.
Works by building a powerset of
'123456789'
where all non-matching entries are truncated and prefixed with0
, sorting the results in numerical order and keeping only the 77 relevant ones.Demo
Show code snippet
źródło
C (gcc), 106 bytes
Try it online!
The ungolfed (prettified) version:
źródło
JavaScript (ES6),
109104 bytesWorks by generating all possible numbers: loops through each increment from 8 to 1 (variable
i
), loops through each starting digit from 8 to 1 (variablej
), loops through each digit betweenj
and10-i
(variablek
) and generates a stringt
by appendingk
to the currentt
. At each stept
is added to the output array.Try it online!
źródło
Wolfram Language (Mathematica), 71 bytes
Try it online!
Lightning fast due to constructing rather than selecting the output.
źródło
Java (OpenJDK 8),
228174170163 bytesTry it online!
źródło
JavaScript (ES6), 145 bytes
A straight-forward approach with little magic.
Running the snippet will consume a lot of memory...
źródło
PHP,
8584 bytestry it online.
sorting cost 17 bytes. This version prints the results ordered differently:
źródło