| Bytes | Lang | Time | Link |
|---|---|---|---|
| 044 | TIBASIC TI84 Plus CE Python | 250523T150628Z | madeforl |
| 066 | YASEPL | 240208T182618Z | madeforl |
| 038 | Perl 5 p | 240203T065331Z | Xcali |
| 031 | Julia 1.0 | 220111T010220Z | MarcMush |
| 014 | Ly | 221006T040934Z | cnamejj |
| 042 | Prolog SWI | 221006T000210Z | naffetS |
| 027 | Raku | 221001T194028Z | Sean |
| 049 | Factor + math.unicode | 220210T053705Z | chunes |
| 095 | tinylisp | 220201T082332Z | Razetime |
| 039 | JavaScript Node.js | 220131T132257Z | l4m2 |
| 149 | Desmos | 220109T112907Z | Aiden Ch |
| 048 | PowerShell | 220110T170819Z | Zaelin G |
| 016 | APL | 220109T181809Z | Jayant C |
| 006 | 05AB1E | 220108T203135Z | Kevin Cr |
| 008 | Pip | 220110T051450Z | DLosc |
| 048 | C gcc | 220108T025027Z | att |
| 058 | R | 220108T120452Z | pajonk |
| 035 | Ruby | 220108T061834Z | Jonah |
| 007 | MathGolf | 220108T203947Z | Kevin Cr |
| 041 | Haskell | 220108T143141Z | Wheat Wi |
| 008 | Japt | 220108T165315Z | Shaggy |
| 048 | APL+WIN | 220108T160501Z | Graham |
| 026 | Perl 5 p | 220108T130700Z | Kjetil S |
| 006 | Husk | 220108T105728Z | Natte |
| 084 | Python 3 | 220108T095157Z | solid.py |
| 025 | Charcoal | 220108T100305Z | Neil |
| 049 | Python 2 | 220108T100129Z | loopy wa |
| 067 | Retina 0.8.2 | 220108T091640Z | Neil |
| 048 | C gcc | 220108T085742Z | dingledo |
| 003 | Brachylog | 220108T072207Z | ovs |
| 039 | Pari/GP | 220108T013752Z | alephalp |
| 006 | Vyxal | 220108T010757Z | emanresu |
| 007 | Jelly | 220108T011021Z | caird co |
TI-BASIC (TI-84 Plus CE Python), 74 63 44 bytes
Input N
{0,1
While 0<max(ΔList(Ans
N+1→N
int(10fPart(N₁₀^(seq(-X-1,X,0,log(N
End
N
I probably overthunk this
YASEPL, 66 bytes
=a'(+`1=1)aſ""=h®1=d`2!c¥d,1!f$d+!g¥f,1}2,c,5!d+}2,h,2|6`5!+|`6>a
explanation:
=a'(+`1=1)aſ""=h®1=d`2!c¥d,1!f$d+!g¥f,1}2,c,5!d+}2,h,2|6`5!+|`6>a packed
=a'( get user input and set it to "a", making it an integer
+ add one to variable a
`1 | loop forever...
=1)aſ"" set variable "1" to string version of variable a
=h®1 get length of variable "1" and set it to variable h
=d declare variable d / set it to 0
`2 }2,h,2 while d < h...
!c¥d,1 get item from variable "1" index d and set it to variable c
!f$d+ set variable f to be d + 1
!g¥f,1 get item from variable "1" index f and set it to variable g
}2,c,5 `5 if g > c...
!d+ add one to d.
!+ ...else, add one to a and loop back
|6 `6>a print a and exit if loop completed sucessfully (found next greater number)
Perl 5 -p, 38 bytes
$_++;s/./$t||($&<$`%10?$t=$`%10:$&)/ge
It's a little longer than @KjetilS' answer, but it has the advantage of being nearly "instant" for any number. Its runtime is based on the length of the number rather than the value. For really big numbers, -Mbigint should be added to the command line flags.
Ly, 14 bytes
1[p`sSaJlf=!]p
This is a pretty straightforward iterative take on the question. It splits the number into digits, sorts them, collapses back to a number, and compare that to the original. The loop ends when the digits are sorted.
1 - push a '1' to start the loop
[p ] - loop until the top of the stack is '0'
` - increment the current var (first time reads STDIN)
s - save number to backup cell
S - split the number into digits
a - sort the stack
J - recombine the digits into a number
l - pull the number from the backup cell
f - flip the top two entries of the stack
= - compare the original and sorted digits
! - negate result of comparison
p - delete iterator var, leaves the answer on the stack
Raku, 27 bytes
{first {[<=] .comb},$_^..*}
$_ ^.. *is the infinite sequence of numbers starting with, but not including (thanks to the^), the input number$_.first { ... }, ...returns the first number in that sequence for which the brace-delimited anonymous function returns a true value..combsplits the input number into its individual digits.[<=]reduces the digits with the<=operator. It's equivalent todigit₁ <= digit₂ <= ... <= digitₙ.
Factor + math.unicode, 49 bytes
[ [ 1 + dup present [ <= ] monotonic? ¬ ] loop ]
Add 1. If the string representation of the number isn't sorted, repeat.
tinylisp, 95 bytes
(load library
(d F(q((N)(i(e N(from-base 10(merge-sort(to-base 10 N))))N(G N
(d G(q((N)(F(a N 1
-3 from dlosc
merge-sort is shorter than insertion-sort. Surprisingly there's no plain sort alias. Since variable assignment is global, i've resorted to using (+ n 1) everywhere.
Desmos, 159 155 149 bytes
The code below supports all integers, with negative integers supported as described in Jonathan Allan's comment.
o->T(o+1-sign(o-c)min(1-sign(L-sort(L))^2)),c->T(c)
i=0
o=0
c=0
s=sign(o)
a=abs(o)
L=smod(floor(a/10^{[floor(log(a+1-ss))...0]}),10)
T(n)=\{i=c:n,i\}
Try It On Desmos! - Prettified
This uses a variable c to cache the input so that changes in the input can be detected (with the function T(n)), causing the rest of the program to react accordingly (That is why the program won't break if the input is changed while the ticker is still running).
The byte count can actually be lowered significantly if some liberties (A.K.A. removing the caching variable) can be taken with the I/O method:
127 123 117 bytes, with modified I/O method
o->o+1-sign(o-i)min(1-sign(L-sort(L))^2)
i=0
o=0
s=sign(o)
a=abs(o)
L=smod(floor(a/10^{[floor(log(a+1-ss))...0]}),10)
Try It On Desmos! - Prettified
This version removes the caching variable c and the function T(n) completely, which means that the code cannot detect any changes in the input. This means that extra steps have to be taken in order to enter in or change the input and run the code.
I'm not too sure which I/O method is considered the "accepted" version, so I'm putting both.
Also, it's unclear whether or not we have to support negative integers. If we only have to support non-negative integers or positive integers, the code can be shortened even more (the code below uses the caching variable):
140 134 bytes, supports all non-negative integers
o->T(o+1-sign(o-c)min(1-sign(L-sort(L))^2)),c->T(c)
i=0
o=0
c=0
L=mod(floor(o/10^{[floor(log(a+1-sign(o)))...0]}),10)
T(n)=\{i=c:n,i\}
Try It On Desmos! - Prettified
128 122 bytes, supports positive integers
o->T(o+1-sign(o-c)min(1-sign(L-sort(L))^2))),c->T(c)
i=1
o=1
c=0
L=mod(floor(o/10^{[floor(logo)...0]}),10)
T(n)=\{i=c:n,i\}
PowerShell, 48 bytes
Very simply iterates through numbers starting from the input until a number equals the sorted string representation of itself, then returns that number.
param($n)for(;++$n-ne-join("$n"|% T*y|sort)){}$n
APL 1612 16 bytes
New Approch
10⊥⌈/\⍎¨⍕1∘+
It's just a append scan and then maximum of each cell doesnt work in case idetified by @ova
1∘+⍣{∧/2≤/⍎¨⍕⍺}
Explanation
1∘+ adds 1
{∧/2≤/⍎¨⍕⍺} checks weather each number is bigger than the one preceding it
1∘+⍣{∧/2≤/⍎¨⍕⍺} continously adds 1 untill the previous function returns true i.e. A fixed point function
05AB1E, 6 bytes
[>Ð{Q#
Try it online or verify almost all test cases (except for the last one, which is shortened a bit).
Explanation:
[ # Loop indefinitely:
> # Increase the current value by 1
# (which will be the implicit input in the first iteration)
Ð # Triplicate it
{ # Sort the digits in the top copy
Q # Pop it and another copy and check if they're still the same
# # If it is: stop the infinite loop
# (after which the remaining third value is output implicitly as result)
Here a different approach which also handles the largest test case (14 bytes):
>Dü›Å¡ćJs˜¬s∍«
Try it online or verify all test cases.
Explanation:
# E.g. input = 11123159995399999
> # Increase the (implicit) input-integer by 1
# STACK: 11123159995400000
D # Duplicate it
# STACK: 11123159995400000,11123159995400000
ü # For each overlapping pair of digits:
› # Check if the first is larger than the second
# STACK: 11123159995400000,[0,0,0,0,1,0,0,0,0,1,1,1,0,0,0,0]
Å¡ # Split the (implicit) input-integer at the truthy positions
# STACK: [[1,1,1,2],[3,1,5,9,9],[9],[5],[4,0,0,0,0,0]]
ć # Extract head; pop and push first list and remainder-lists
# separated to the stack
# STACK: [[3,1,5,9,9],[9],[5],[4,0,0,0,0,0]],[1,1,1,2]
J # Join this first list together to a single integer
# STACK: [[3,1,5,9,9],[9],[5],[4,0,0,0,0,0]],1112
s # Swap to get the remainder-list
# STACK: 1112,[[3,1,5,9,9],[9],[5],[4,0,0,0,0,0]]
˜ # Flatten it
# STACK: 1112,[3,1,5,9,9,9,5,4,0,0,0,0,0]
¬ # Push its first digit (without popping the list)
# STACK: 1112,[3,1,5,9,9,9,5,4,0,0,0,0,0],3
s # Swap so the list is at the top
# STACK: 1112,3,[3,1,5,9,9,9,5,4,0,0,0,0,0]
∍ # Extend this digit to the length of this list
# STACK: 1112,3333333333333
« # Append the two strings together
# STACK: 11123333333333333
# (after which the result is output implicitly)
Pip, 8 bytes
T$<=Ua_a
Explanation
; a is command-line argument
T ; Loop till
Ua ; Increment a, and
$ ; Fold on
<= ; Less-than-or-equal
; is true:
_ ; No-op
a ; After the loop, autoprint the modified value of a
Folding a number such as 1455 on <= is the equivalent of computing 1 <= 4 <= 5 <= 5, which (thanks to Pip's chaining comparison operators) is equivalent to (1 <= 4) & (4 <= 5) & (5 <= 5).
Here's a 19-byte version that can handle the largest test case:
O@Ua{y?ya>b?Yab}MPa
O@Ua{y?ya>b?Yab}MPa
Ua ; Increment a
@ ; Get its first digit
O ; Output it without a newline
{ }MPa ; Map this function to each pair of adjacent digits in a:
y? ; Has y been assigned yet?
y ; If so, use that as the next digit
a>b? ; Otherwise, is the first digit of the pair greater than
; the second digit of the pair?
Ya ; If so, assign the first digit to y and use that
b ; Otherwise, use the second digit
C (gcc), 59 48 bytes
f(n,t){t=++n?t=f(n/10-1),10*t+t%10:0;n=t>n?t:n;}
f(n,t){
t=++n // increment input
? t=f(n/10-1), // recurse on prefix
10*t+t%10 // and append its last digit
: 0; // (base case =0)
n=t>n?t:n; // max of above and input
}
R, 65 58 bytes
Or R>=4.1, 51 bytes by replacing the word function with a \.
Edit: -7 bytes thanks to @Dominic van Essen.
function(n){while(any(diff((n=n+1)%/%10^(n:0)%%10)<0))0
n}
Try it online! Try it on rdrr.io (for larger test-cases)!
Straightforward brute-force:
- Increment
n. - Split to digits (with leading
0s that don't matter). - Take differences.
- If any of the differences is
<0, loop back to 1. Otherwise, stop and outputn.
MathGolf, 7 bytes
)∙▒sy=▼
Port of my 05AB1E answer.
Explanation:
▼ # Do-while false with pop:
) # Increase the current integer by 1
# (which will be the implicit input in the first iteration)
∙ # Triplicate it
▒ # Convert the top copy to a list of digits
s # Sort those digits
y # Join it back together to an integer
= # Check if the sorted integer is still the same as the triplicated integer
# (after the do-while loop, output the entire stack implicitly as result)
Haskell, 52 41 bytes
9 bytes saved by AZTECCO and 2 bytes saved by xnor
g(a:b)|[a]>b=a<$a:b|1>0=a:g b
g.show.(+1)
Solves all the cases nearly instantly.
Explanation
The first thing to observe is that:
Find the smallest ascending digit number greater than \$n\$.
Is significantly harder than:
Find the smallest ascending digit number greater or equal to than \$n\$.
The former has a lot of edge cases around numbers that are themselves ascending in digits while the latter can be solved fairly easily.
But we can turn the latter to the former by just adding 1 before we run our algorithm. If g is the function that solves it then we just have g.show.(+1) to solve the actual problem.
So I solve the latter first:
g(a:b)|[a]>b=a<$a:b|1>0=a:g b
This goes along the list until either we meet a pair of digits that is descending (e.g. 21) or we reach the end. At that point we replace everything left with the digit we just read and exit.
So for example
122334555612990
^^
122334555666666
Here's what the solution looks like without this observation just handling the edge cases naively:
92 bytes
x!q@(a:b:c)|a>b=a<$q|1>0=(x++[a])!(b:c)
x!"9"=""!x++"1"
x!""="1"
x!y=x++show(1+read y)
f=(""!)
APL+WIN, 48 bytes
Prompts for integer
n←⍴v←⍎¨⍕⎕+1⋄10⊥⌽((n-⍴v)⍴¯1↑v),⌽v←(^\0≤1,-2-/v)/v
Try it online! Thanks to Dyalog Classic
The final example throws an error in the final digit in Dyalog Classic on TIO. Dropping the last digit of that example gives the expected result.
Perl 5 -p, 26 bytes
1while++$_-join'',sort/./g
The last test case would take about 194 hours (which is 8 days where I live). So I removed the last five digits from the test case to get the result in 7 sec. For "instant" result, also for the last big test case, this is a 78 byte suggestion: $_++;s/(@{[join"|",map$_."[0-".($_-1)."]",1..9]}).*/substr($1,0,1)x length$&/e ...try it online!
Husk, 6 bytes
ḟoΛ≤d→
Explanation
ḟoΛ≤d→
ḟ find the first integer
→ starting from input incremented
o such that composed function
Λ d every digit
≤ is less that or equal to the previous
Python 3, 84
f=lambda n:n+1if all(int(i)<=int(j)for i,j in zip(str(n+1),str(n+1)[1:]))else f(n+1)
Fails on the last case due to exceeding the maximum recursion depth.
Charcoal, 25 bytes
≔I⊕Nθ…θ⌕Eθ›ι§θ⊕κ¹×⁻Lθⅈ§θⅈ
Try it online! Link is to verbose version of code. Explanation:
≔I⊕Nθ
Input n, increment it, and turn it into a string.
…θ⌕Eθ›ι§θ⊕κ¹
Print the prefix up to the first digit that is greater than its subsequent digit. (There is an edge case here when all the digits are equal but the remaining code will just fill with that digit because it has no other choice.)
×⁻Lθⅈ§θⅈ
Fill the rest of the string with that digit.
Python 2, 49 bytes
f=lambda n:-~n*(list(`-n`)<sorted(`~n`))or f(-~n)
Brute force: increments the given number until it is great. Unsurprisingly, chokes on the larger test case.
Python, 77 bytes
f=lambda n,t=1:(n:=n+t)*([*str(n)]<=sorted(str(n)))or 10*(g:=f(n//10,0))+g%10
This constructs the result directly. Handles all test cases.
Retina 0.8.2, 67 bytes
T`9d`d`.9*$
^0
10
.
$*_¶
+`((_+¶)(?!_*\2)(_*¶)*)_*¶$
$2$1
(_+)¶
$.1
Try it online! Link includes test cases. Explanation:
T`9d`d`.9*$
Increment the input.
^0
10
Deal with any carry.
.
$*_¶
Convert each digit to unary.
+`((_+¶)(?!_*\2)(_*¶)*)_*¶$
$2$1
Propagate the first digit greater than its successor to the end of the number. E.g. 11123159995400000, 11123315999540000, 11123331599954000, ... 1123333333333331, 1123333333333333.
(_+)¶
$.1
Convert the digits back to decimal.
Brachylog, 3 bytes
<.o
Try it online! or Try more cases
?<.o. # implicit input (?) and output (.)
?<. # the input is smaller than the output
.o. # the output is itself when ordered
Vyxal, 6 bytes
{›Ds≠|
{ # While...
› # Increment
D # Make three copies
s≠ # Check if sorted
| # Do nothing
Jelly, 7 bytes
‘DṢƑ$1#
How it works
‘DṢƑ$1# - Main link. Takes n on the left
‘ - Yield n+1
$1# - Starting from n+1, find the first integer after such that:
D - The digits are
Ƒ - Invariant under
Ṣ - Sorting