Next highest int from digits: Difference between revisions

Added FreeBASIC
(Added Delphi example)
(Added FreeBASIC)
 
(16 intermediate revisions by 15 users not shown)
Line 64:
:*   9589776899767587796600
<br><br>
 
=={{header|11l}}==
{{trans|Python: Algorithm 2}}
 
<syntaxhighlight lang="11l">F closest_more_than(n, lst)
V large = max(lst) + 1
R lst.index(min(lst, key' x -> (I x <= @n {@large} E x)))
 
F nexthigh(n)
V this = reversed(Array(n.map(digit -> Int(digit))))
V mx = this[0]
L(digit) this[1..]
V i = L.index + 1
I digit < mx
V mx_index = closest_more_than(digit, this[0 .< i + 1])
swap(&this[mx_index], &this[i])
this.sort_range(0 .< i, reverse' 1B)
R reversed(this).map(d -> String(d)).join(‘’)
E I digit > mx
mx = digit
R ‘0’
 
L(x) [‘0’, ‘9’, ‘12’, ‘21’, ‘12453’, ‘738440’, ‘45072010’, ‘95322020’,
‘9589776899767587796600’]
print(‘#12 -> #12’.format(x, nexthigh(x)))</syntaxhighlight>
 
{{out}}
<pre>
0 -> 0
9 -> 0
12 -> 21
21 -> 0
12453 -> 12534
738440 -> 740348
45072010 -> 45072100
95322020 -> 95322200
9589776899767587796600 -> 9589776899767587900667
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Should work with any Algol 68 implementation if LONG INT is large to hold the stretch test case.<br>
Implements algorithm 2.
<syntaxhighlight lang="algol68">
BEGIN # find the next integer > a given integer with the same digits #
 
OP - = ( CHAR a, b )INT: ABS a - ABS b; # character subtraction #
PROC swap = ( REF STRING s, INT a, b )VOID: # swap chars at a and b in s #
BEGIN CHAR t = s[ a ]; s[ a ] := s[ b ]; s[ b ] := t END;
 
# returns the next integer greater than v with the same digits as v #
OP NEXT = ( LONG INT v )LONG INT:
BEGIN
LONG INT result := 0;
STRING s := whole( v, 0 );
INT c pos := UPB s - 1;
# find the rightmost digit that has a lower digit to the left #
WHILE IF c pos < LWB s
THEN FALSE
ELSE s[ c pos ] >= s[ c pos + 1 ]
FI
DO
c pos -:=1
OD;
IF c pos >= LWB s THEN
# the digit at c pos is lower than one to its right #
# swap the lower digit with the smallest right digit greater #
# than the lower digit #
CHAR c = s[ c pos ];
INT min pos := c pos + 1;
INT min diff := s[ c pos + 1 ] - c;
FOR m pos FROM c pos + 2 TO UPB s DO
IF s[ m pos ] > c THEN
IF INT this diff = s[ m pos ] - c;
this diff < min diff
THEN
min diff := this diff;
min pos := m pos
FI
FI
OD;
swap( s, min pos, c pos );
# sort the right digits #
FOR u FROM UPB s - 1 BY -1 TO c pos + 1
WHILE BOOL sorted := TRUE;
FOR p FROM c pos + 1 BY 1 TO u DO
IF s[ p ] > s[ p + 1 ] THEN
swap( s, p, p + 1 );
sorted := FALSE
FI
OD;
NOT sorted
DO SKIP OD;
# convert back to an integer #
result := s[ LWB s ] - "0";
FOR i FROM LWB s + 1 TO UPB s DO
result *:= 10 +:= s[ i ] - "0"
OD
FI;
result
END # NEXT # ;
 
# task test cases #
[]LONG INT tests = ( 0, 9, 12, 21, 12453, 738440, 45072010, 95322020
, 9589776899767587796600
);
FOR i FROM LWB tests TO UPB tests DO
print( ( whole( tests[ i ], -24 ), " -> ", whole( NEXT tests[ i ], 0 ), newline ) )
OD
END
</syntaxhighlight>
{{out}}
<pre>
0 -> 0
9 -> 0
12 -> 21
21 -> 0
12453 -> 12534
738440 -> 740348
45072010 -> 45072100
95322020 -> 95322200
9589776899767587796600 -> 9589776899767587900667
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">canHaveGreater?: function [n][
mydigs: digits n
maxdigs: reverse sort mydigs
 
return some? 0..dec size mydigs 'i ->
maxdigs\[i] > mydigs\[i]
]
nextHighest: function [n][
if not? canHaveGreater? n -> return n
 
ndigs: sort digits n
i: n + 1
while [ndigs <> sort digits i]->
i: i + 1
 
return i
]
 
loop [0, 9, 12, 21, 12453, 738440, 45072010, 95322020] 'num ->
print [pad (to :string num) 9 "->" nextHighest num]</syntaxhighlight>
 
{{out}}
 
<pre> 0 -> 0
9 -> 9
12 -> 21
21 -> 21
12453 -> 12534
738440 -> 740348
45072010 -> 45072100
95322020 -> 95322200</pre>
 
=={{header|AutoHotkey}}==
===Permutation Version===
<langsyntaxhighlight AutoHotkeylang="autohotkey">Next_highest_int(num){
Arr := []
for i, v in permute(num)
Line 100 ⟶ 256:
res .= x[A_Index]
return res
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % "" Next_highest_int(0)
. "`n" Next_highest_int(9)
. "`n" Next_highest_int(12)
Line 108 ⟶ 264:
. "`n" Next_highest_int(738440)
. "`n" Next_highest_int(45072010)
. "`n" Next_highest_int(95322020)</langsyntaxhighlight>
{{out}}
<pre>0
Line 119 ⟶ 275:
95322200</pre>
===Scanning Version===
<langsyntaxhighlight AutoHotkeylang="autohotkey">Next_highest_int(num){
Loop % StrLen(num){
i := A_Index
Line 146 ⟶ 302:
res .= v
return res
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % "" Next_highest_int(0)
. "`n" Next_highest_int(9)
. "`n" Next_highest_int(12)
Line 155 ⟶ 311:
. "`n" Next_highest_int(45072010)
. "`n" Next_highest_int(95322020)
. "`n" Next_highest_int("9589776899767587796600") ; pass long numbers as text (between quotes)</langsyntaxhighlight>
{{out}}
<pre>0
Line 168 ⟶ 324:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
Line 222 ⟶ 378:
printf("%s -> %s\n", big, next);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 236 ⟶ 392:
9589776899767587796600 -> 9589776899767587900667
</pre>
 
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using System.Text;
 
public class NextHighestIntFromDigits
{
public static void Main(string[] args)
{
foreach (var s in new string[] { "0", "9", "12", "21", "12453", "738440", "45072010", "95322020", "9589776899767587796600", "3345333" })
{
Console.WriteLine($"{Format(s)} -> {Format(Next(s))}");
}
TestAll("12345");
TestAll("11122");
}
 
private static string Format(string s)
{
return BigInteger.Parse(s).ToString("N0", CultureInfo.InvariantCulture);
}
 
private static void TestAll(string s)
{
Console.WriteLine($"Test all permutations of: {s}");
var sOrig = s;
var sPrev = s;
int count = 1;
 
// Check permutation order. Each is greater than the last
bool orderOk = true;
var uniqueMap = new Dictionary<string, int>();
uniqueMap[s] = 1;
while ((s = Next(s)) != "0")
{
count++;
if (BigInteger.Parse(s) < BigInteger.Parse(sPrev))
{
orderOk = false;
}
if (uniqueMap.ContainsKey(s))
{
uniqueMap[s]++;
}
else
{
uniqueMap[s] = 1;
}
sPrev = s;
}
Console.WriteLine($" Order: OK = {orderOk}");
 
// Test last permutation
var reverse = Reverse(sOrig);
Console.WriteLine($" Last permutation: Actual = {sPrev}, Expected = {reverse}, OK = {sPrev == reverse}");
 
// Check permutations unique
bool unique = true;
foreach (var key in uniqueMap.Keys)
{
if (uniqueMap[key] > 1)
{
unique = false;
}
}
Console.WriteLine($" Permutations unique: OK = {unique}");
 
// Check expected count.
var charMap = new Dictionary<char, int>();
foreach (var c in sOrig)
{
if (charMap.ContainsKey(c))
{
charMap[c]++;
}
else
{
charMap[c] = 1;
}
}
BigInteger permCount = Factorial(sOrig.Length);
foreach (var c in charMap.Keys)
{
permCount /= Factorial(charMap[c]);
}
Console.WriteLine($" Permutation count: Actual = {count}, Expected = {permCount}, OK = {count == permCount}");
}
 
private static BigInteger Factorial(int n)
{
BigInteger fact = 1;
for (int num = 2; num <= n; num++)
{
fact *= num;
}
return fact;
}
 
private static string Next(string s)
{
var sb = new StringBuilder();
int index = s.Length - 1;
// Scan right-to-left through the digits of the number until you find a digit with a larger digit somewhere to the right of it.
while (index > 0 && s[index - 1] >= s[index])
{
index--;
}
// Reached beginning. No next number.
if (index == 0)
{
return "0";
}
 
// Find digit on the right that is both more than it, and closest to it.
int index2 = index;
for (int i = index + 1; i < s.Length; i++)
{
if (s[i] < s[index2] && s[i] > s[index - 1])
{
index2 = i;
}
}
 
// Found data, now build string
// Beginning of String
if (index > 1)
{
sb.Append(s.Substring(0, index - 1));
}
 
// Append found, place next
sb.Append(s[index2]);
 
// Get remaining characters
List<char> chars = new List<char>();
chars.Add(s[index - 1]);
for (int i = index; i < s.Length; i++)
{
if (i != index2)
{
chars.Add(s[i]);
}
}
 
// Order the digits to the right of this position, after the swap; lowest-to-highest, left-to-right.
chars.Sort();
foreach (var c in chars)
{
sb.Append(c);
}
return sb.ToString();
}
 
private static string Reverse(string s)
{
var charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
</syntaxhighlight>
{{out}}
<pre>
0 -> 0
9 -> 0
12 -> 21
21 -> 0
12,453 -> 12,534
738,440 -> 740,348
45,072,010 -> 45,072,100
95,322,020 -> 95,322,200
9,589,776,899,767,587,796,600 -> 9,589,776,899,767,587,900,667
3,345,333 -> 3,353,334
Test all permutations of: 12345
Order: OK = True
Last permutation: Actual = 54321, Expected = 54321, OK = True
Permutations unique: OK = True
Permutation count: Actual = 120, Expected = 120, OK = True
Test all permutations of: 11122
Order: OK = True
Last permutation: Actual = 22111, Expected = 22111, OK = True
Permutations unique: OK = True
Permutation count: Actual = 10, Expected = 10, OK = True
 
</pre>
 
 
=={{header|C++}}==
Line 241 ⟶ 589:
{{libheader|GMP}}
This solution makes use of std::next_permutation, which is essentially the same as Algorithm 2.
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <sstream>
Line 268 ⟶ 616:
std::cout << big << " -> " << next_highest(big) << '\n';
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 285 ⟶ 633:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.conv;
Line 408 ⟶ 756:
testAll("12345");
testAll("11122");
}</langsyntaxhighlight>
{{out}}
<pre>0 -> 0
Line 434 ⟶ 782:
{{libheader| System.Generics.Collections}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Next_highest_int_from_digits;
 
Line 606 ⟶ 954:
{$IFNDEF UNIX}
readln; {$ENDIF}
end.</langsyntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
proc reverse i j . dig[] .
while i < j
swap dig[i] dig[j]
i += 1
j -= 1
.
.
proc next_perm . dig[] .
if len dig[] >= 2
for i = 2 to len dig[]
if dig[i] < dig[i - 1]
k = 1
while dig[i] >= dig[k]
k += 1
.
swap dig[i] dig[k]
reverse 1 i - 1 dig[]
return
.
.
.
dig[] = [ ]
.
func next_highest n .
while n > 0
digs[] &= n mod 10
n = n div 10
.
next_perm digs[]
for i = len digs[] downto 1
r = r * 10 + digs[i]
.
return r
.
nums[] = [ 0 9 12 21 12453 738440 45072010 95322020 ]
for n in nums[]
print n & " -> " & next_highest n
.
</syntaxhighlight>
 
 
=={{header|Factor}}==
This uses the <code>next-permutation</code> word from the <code>math.combinatorics</code> vocabulary. <code>next-permutation</code> wraps around and returns the smallest lexicographic permutation after the largest one, so additionally we must check if we're at the largest permutation and return zero if so. See the implementation of <code>next-permutation</code> [https://docs.factorcode.org/content/word-next-permutation%2Cmath.combinatorics.html here].
{{works with|Factor|0.99 2020-01-23}}
<langsyntaxhighlight lang="factor">USING: formatting grouping kernel math math.combinatorics
math.parser sequences ;
 
Line 622 ⟶ 1,014:
9589776899767587796600
}
[ dup next-highest "%d -> %d\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 635 ⟶ 1,027:
9589776899767587796600 -> 9589776899767587900667
</pre>
 
=={{header|FreeBASIC}}==
===algorithm 1===
{{trans|Python}}
<syntaxhighlight lang="vbnet">Function factorial(n As Integer) As Uinteger
Return Iif(n = 0, 1, n * factorial(n - 1))
End Function
 
Sub swap_(s As String, i As Integer, j As Integer)
Dim As String temp = Mid(s, i, 1)
Mid(s, i, 1) = Mid(s, j, 1)
Mid(s, j, 1) = temp
End Sub
 
Sub permute(s As String, l As Integer, r As Integer, perms() As String)
If l = r Then
Redim Preserve perms(Ubound(perms) + 1)
perms(Ubound(perms)) = s
Else
For i As Uinteger = l To r
swap_(s, l, i)
permute(s, l + 1, r, perms())
swap_(s, l, i) ' backtrack
Next i
End If
End Sub
 
Sub bubbleSort(arr() As String)
Dim As Integer i, j, n = Ubound(arr)
Dim As String temp
For i = 0 To n - 1
For j = 0 To n - i - 1
If arr(j) > arr(j + 1) Then
temp = arr(j)
arr(j) = arr(j + 1)
arr(j + 1) = temp
End If
Next j
Next i
End Sub
 
Function nextHigh1(Byref n As String) As String
Dim As String perms()
Dim As Uinteger i, idx
permute(n, 1, Len(n), perms())
bubbleSort perms()
Dim As Uinteger k = Ubound(perms)
For i = 0 To k
If perms(i) = n Then
idx = i
Exit For
End If
Next i
Return Iif(idx < k, perms(idx + 1), "0")
End Function
 
Dim As String tests1(7) = {"0", "9", "12", "21", "12453", "738440", "45072010", "95322020"}
Dim As Double t0 = Timer
For i As Uinteger = 0 To Ubound(tests1)
Print tests1(i); " => "; nextHigh1(tests1(i))
Next i
Print Chr(10); Timer - t0; "sec."</syntaxhighlight>
{{out}}
<pre>0 => 0
9 => 0
12 => 21
21 => 0
12453 => 12534
738440 => 738440
45072010 => 45072010
95322020 => 95322020
 
67.04065610002726sec.</pre>
===algorithm 2===
{{trans|Phix}}
<syntaxhighlight lang="vbnet">Function sort(s As String) As String
Dim As Uinteger i, j, n = Len(s)
Dim As String temp
For i = 1 To n
For j = i + 1 To n
If Asc(Mid(s, i, 1)) > Asc(Mid(s, j, 1)) Then
temp = Mid(s, i, 1)
Mid(s, i, 1) = Mid(s, j, 1)
Mid(s, j, 1) = temp
End If
Next j
Next i
Return s
End Function
 
Function rfind(c As String, s As String) As Uinteger
Return Instr(s, c)
End Function
 
Function nextHigh2(n As String) As String
Dim As Uinteger hi = Asc(Right(n, 1))
Dim As Uinteger i, ni, idx
Dim As String sr
For i = Len(n) - 1 To 1 Step -1
ni = Asc(Mid(n, i, 1))
If ni < hi Then
sr = sort(Mid(n, i))
idx = rfind(Chr(ni), sr) + 1
Mid(n, i, 1) = Mid(sr, idx, 1)
Mid(sr, idx, 1) = ""
Mid(n, i + 1) = sr
Return n
End If
hi = Iif(hi > ni, hi, ni)
Next i
Return "0"
End Function
 
Dim As String tests2(8) = { "0", "9", "12", "21", "12453", _
"738440", "45072010", "95322020", "9589776899767587796600" }
Dim As Double t1 = Timer
For i As Uinteger = 0 To Ubound(tests2)
Print tests2(i); " => "; nextHigh2(tests2(i))
Next i
Print Chr(10); Timer - t1; "sec."</syntaxhighlight>
{{out}}
<pre>0 => 0
9 => 0
12 => 21
21 => 0
12453 => 12534
738440 => 740344
45072010 => 45072000
95322020 => 95322000
9589776899767587796600 => 9589776899767587900667
 
0.004686999949626625sec.</pre>
 
=={{header|Go}}==
This uses a modified version of the recursive code in the [[https://rosettacode.org/wiki/Permutations#Go Permutations#Go]] task.
<langsyntaxhighlight lang="go">package main
 
import (
Line 752 ⟶ 1,285:
algorithm1(nums[:len(nums)-1]) // exclude the last one
algorithm2(nums) // include the last one
}</langsyntaxhighlight>
 
{{out}}
Line 786 ⟶ 1,319:
{{Trans|Python}}
(Generator version)
<langsyntaxhighlight lang="haskell">import Data.List (nub, permutations, sort)
 
digitShuffleSuccessors :: Integer -> [Integer]
Line 824 ⟶ 1,357:
 
rjust :: Int -> Char -> String -> String
rjust n c = drop . length <*> (replicate n c <>)</langsyntaxhighlight>
{{Out}}
<pre>Taking up to 5 digit-shuffle successors of a positive integer:
Line 842 ⟶ 1,375:
(The digit-swap approach makes it feasible to obtain successors of this kind for much larger numbers)
 
<langsyntaxhighlight lang="haskell">import Data.List (unfoldr)
import Data.Bool (bool)
 
------------------- MINIMAL DIGIT-SWAPS --------------------
 
digitShuffleSuccessors
digitShuffleSuccessors :: Integral b => b -> [b]
=> b -> [b]
digitShuffleSuccessors n =
unDigits <$> unfoldr nexts (go $ reversedDigits n)
let go = minimalSwap . splitBy (>)
where
in unDigits <$>
go = minimalSwap . splitBy (>)
unfoldr
nexts x
((\x -> bool (Just (((,) <*> go . reverse) x)) Nothing) <*> null)
| (gonull (reversedDigitsx n))= Nothing
| otherwise = Just (((,) <*> go . reverse) x)
 
 
minimalSwap :: Ord a => ([a], [a]) -> [a]
minimalSwap ([], x : y : xs) = reverse (y : x : xs)
:: Ord a
=> ([a], [a]) -> [a]
minimalSwap ([], x:y:xs) = reverse (y : x : xs)
minimalSwap ([], xs) = []
minimalSwap (_, []) = []
minimalSwap (reversedSuffix, pivot : prefix) =
letreverse (less, h :more prefix) =<> breakless (<> (pivot) reversedSuffix: more)
where
in reverse (h : prefix) ++ less ++ pivot : more
(less, h : more) = break (> pivot) reversedSuffix
 
 
--------------------------- TEST ---------------------------
main :: IO ()
main = do
putStrLn $
fTable
( "Taking up to 5 digit-shuffle successors of a positive integer:\n"
<> "of a positive integer:\n"
)
show
( \xs ->
let harvest = take 5 xs
in rjust
12
' '
( show (length harvest) <> " of " <> show (length xs) ++ ": ") ++
<> show harvest(length xs)
<> ": "
)
<> show harvest
)
digitShuffleSuccessors
[0, 9, 12, 21, 12453, 738440, 45072010, 95322020]
Line 891 ⟶ 1,429:
[9589776899767587796600]
 
------------------------- GENERIC ------------------------
 
reversedDigits :: Integral a => a -> [a]
------------------------- GENERIC --------------------------
reversedDigits
:: Integral a
=> a -> [a]
reversedDigits 0 = [0]
reversedDigits n = go n
Line 909 ⟶ 1,444:
| otherwise = (fst (head ys) : map snd ys, map snd zs)
 
unDigits :: Integral a => [a] -> a
:: (Foldable t, Num a)
=> t a -> a
unDigits = foldl (\a b -> 10 * a + b) 0
 
------------------------- DISPLAY ------------------------
 
fTable ::
------------------------- DISPLAY --------------------------
String ->
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
(a -> String) ->
(b -> String) ->
(a -> b) ->
[a] ->
String
fTable s xShow fxShow f xs =
unlines $
s :
s : fmap (((<>) . rjust w ' ' . xShow) <*> ((" -> " <>) . fxShow . f)) xs
fmap
( ((<>) . rjust w ' ' . xShow)
<*> ((" -> " <>) . fxShow . f)
)
xs
where
w = maximum (length . xShow <$> xs)
 
rjust :: Int -> Char -> String -> String
rjust n c = drop . length <*> (replicate n c <>)</langsyntaxhighlight>
{{Out}}
<pre>Taking up to 5 digit-shuffle successors of a positive integer:
Line 970 ⟶ 1,513:
=={{header|Java}}==
Additional testing is performed, including a number with all unique digits and a number with duplicate digits. Included test of all permutations, that the order and correct number of permutations is achieved, and that each permutation is different than all others. If a library is not used, then this testing will provide a better proof of correctness.
<langsyntaxhighlight lang="java">
import java.math.BigInteger;
import java.text.NumberFormat;
Line 1,096 ⟶ 1,639:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,123 ⟶ 1,666:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">const compose = (...fn) => (...x) => fn.reduce((a, b) => c => a(b(c)))(...x);
const toString = x => x + '';
const reverse = x => Array.from(x).reduce((p, c) => [c, ...p], []);
Line 1,166 ⟶ 1,709:
test(95322020);
test('9589776899767587796600');
</syntaxhighlight>
</lang>
{{out}}
<pre>0 => 0
Line 1,178 ⟶ 1,721:
9589776899767587796600 => 9589776899767587900667</pre>
 
=={{header|jq}}==
<syntaxhighlight lang="jq"># Generate a stream of all the permutations of the input array
def permutations:
# Given an array as input, generate a stream by inserting $x at different positions
def insert($x):
range (0; length + 1) as $pos
| .[0:$pos] + [$x] + .[$pos:];
 
if length <= 1 then .
else
.[0] as $first
| .[1:] | permutations | insert($first)
end;
 
def next_highest:
(tostring | explode) as $digits
| ([$digits | permutations] | unique) as $permutations
| ($permutations | bsearch($digits)) as $i
| if $i == (($permutations|length) - 1) then 0
else $permutations[$i+1] | implode
end;
 
def task:
0,
9,
12,
21,
12453,
738440,
45072010,
95322020;
 
task | "\(.) => \(next_highest)"</syntaxhighlight>
{{out}}
<pre>
0 => 0
9 => 0
12 => 21
21 => 0
12453 => 12534
738440 => 740348
45072010 => 45072100
95322020 => 95322200
</pre>
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics, BenchmarkTools
 
asint(dig) = foldl((i, j) -> 10i + Int128(j), dig)
Line 1,269 ⟶ 1,856:
@btime nexthighest_2(n)
println(" for method 2 and n $n.")
</langsyntaxhighlight>{{out}}
<pre>
N 1A 1B 2
Line 1,295 ⟶ 1,882:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.math.BigInteger
import java.text.NumberFormat
 
Line 1,420 ⟶ 2,007:
}
return sb.toString()
}</langsyntaxhighlight>
{{out}}
<pre>0 -> 0
Line 1,442 ⟶ 2,029:
Permutations unique: OK = true
Permutation count: Actual = 10, Expected = 10, OK = true</pre>
 
=={{header|Lua}}==
Algorithm 2 with a reverse index of digit positions.
<syntaxhighlight lang="lua">unpack = unpack or table.unpack -- <=5.2 vs >=5.3 polyfill
function nexthighestint(n)
local digits, index = {}, {[0]={},{},{},{},{},{},{},{},{},{}}
for d in tostring(n):gmatch("%d") do digits[#digits+1]=tonumber(d) end
for i,d in ipairs(digits) do index[d][#index[d]+1]=i end
local function findswap(i,d)
for D=d+1,9 do
for I=1,#index[D] do
if index[D][I] > i then return index[D][I] end
end
end
end
for i = #digits-1,1,-1 do
local j = findswap(i,digits[i])
if j then
digits[i],digits[j] = digits[j],digits[i]
local sorted = {unpack(digits,i+1)}
table.sort(sorted)
for k=1,#sorted do digits[i+k]=sorted[k] end
return table.concat(digits)
end
end
end
 
tests = { 0, 9, 12, 21, 12453, 738440, 45072010, 95322020, -- task
"9589776899767587796600", -- stretch
"123456789098765432109876543210", -- stretchier
"1234567890999888777666555444333222111000" -- stretchiest
}
for _,n in ipairs(tests) do
print(n .. " -> " .. (nexthighestint(n) or "(none)"))
end</syntaxhighlight>
{{out}}
<pre>0 -> (none)
9 -> (none)
12 -> 21
21 -> (none)
12453 -> 12534
738440 -> 740348
45072010 -> 45072100
95322020 -> 95322200
9589776899767587796600 -> 9589776899767587900667
123456789098765432109876543210 -> 123456789098765432110023456789
1234567890999888777666555444333222111000 -> 1234567891000011222333444555666777888999</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[NextHighestIntFromDigits]
NextHighestIntFromDigits[n_Integer?NonNegative]:=Module[{digs},
digs=IntegerDigits[n];
digs=FromDigits/@Permutations[digs];
digs=Select[digs,GreaterEqualThan[n]];
If[Length[digs]==1,First[digs],RankedMin[digs,2]]
]
NextHighestIntFromDigits/@{0,9,12,21,12453,738440,45072010,95322020}</syntaxhighlight>
{{out}}
<pre>{0, 9, 21, 21, 12534, 740348, 45072100, 95322200}</pre>
 
=={{header|Nim}}==
Using the scanning algorithm.
<langsyntaxhighlight Nimlang="nim">import algorithm
 
type Digit = range[0..9]
Line 1,487 ⟶ 2,134:
when isMainModule:
for n in [0, 9, 12, 21, 12453, 738440, 45072010, 95322020]:
echo n, " → ", nextHighest(n)</langsyntaxhighlight>
 
{{out}}
Line 1,501 ⟶ 2,148:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,531 ⟶ 2,178:
for (0, 9, 12, 21, 12453, 738440, 45072010, 95322020, 9589776899767587796600, 3345333) {
printf "%30s -> %s\n", comma($_), comma next_greatest_integer $_;
}</langsyntaxhighlight>
{{out}}
<pre> 0 -> 0
Line 1,546 ⟶ 2,193:
=={{header|Phix}}==
===algorithm 1===
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function nigh(string n)
<span style="color: #008080;">function</span> <span style="color: #000000;">nigh</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
sequence p = repeat("",factorial(length(n)))
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)))</span>
for i=1 to length(p) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
p[i] = permute(i,n)
<span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">permute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
p = sort(p)
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
integer k = rfind(n,p)
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rfind</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
return iff(k=length(p)?"0",p[k+1])
<span style="color: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)?</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant tests = {"0","9","12","21","12453",
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"9"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"12"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"21"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"12453"</span><span style="color: #0000FF;">,</span>
"738440","45072010","95322020"}
<span style="color: #008000;">"738440"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"45072010"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"95322020"</span><span style="color: #0000FF;">}</span>
-- (crashes on) "9589776899767587796600"}
<span style="color: #000080;font-style:italic;">-- (crashes on) "9589776899767587796600"}</span>
atom t0 = time()
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
for i=1 to length(tests) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string t = tests[i]
<span style="color: #004080;">string</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
printf(1,"%22s => %s\n",{t,nigh(t)})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%22s =&gt; %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nigh</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)})</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
?elapsed(time()-t0)</lang>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,578 ⟶ 2,227:
</pre>
===algorithm 2===
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function nigh(string n)
<span style="color: #008080;">function</span> <span style="color: #000000;">nigh</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
integer hi = n[$]
<span style="color: #004080;">integer</span> <span style="color: #000000;">hi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">[$]</span>
for i=length(n)-1 to 1 by -1 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
integer ni = n[i]
<span style="color: #004080;">integer</span> <span style="color: #000000;">ni</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
if ni<hi then
<span style="color: #008080;">if</span> <span style="color: #000000;">ni</span><span style="color: #0000FF;"><</span><span style="color: #000000;">hi</span> <span style="color: #008080;">then</span>
string sr = sort(n[i..$])
<span style="color: #004080;">string</span> <span style="color: #000000;">sr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">..$])</span>
integer k = rfind(ni,sr)+1
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rfind</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ni</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sr</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span>
n[i] = sr[k]
<span style="color: #000000;">n</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
sr[k..k] = ""
<span style="color: #000000;">sr</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
n[i+1..$] = sr
<span style="color: #000000;">n</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sr</span>
return n
<span style="color: #008080;">return</span> <span style="color: #000000;">n</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
hi = max(hi,ni)
<span style="color: #000000;">hi</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hi</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ni</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return "0"
<span style="color: #008080;">return</span> <span style="color: #008000;">"0"</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
constant tests = {"0","9","12","21","12453",
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"9"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"12"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"21"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"12453"</span><span style="color: #0000FF;">,</span>
"738440","45072010","95322020",
<span style="color: #008000;">"738440"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"45072010"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"95322020"</span><span style="color: #0000FF;">,</span>
"9589776899767587796600"}
<span style="color: #008000;">"9589776899767587796600"</span><span style="color: #0000FF;">}</span>
atom t0 = time()
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
for i=1 to length(tests) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string t = tests[i]
<span style="color: #004080;">string</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
printf(1,"%22s => %s\n",{t,nigh(t)})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%22s =&gt; %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nigh</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)})</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
?elapsed(time()-t0)</lang>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,621 ⟶ 2,272:
===Python: Algorithm 2===
Like Algorithm 2, but digit order is reversed for easier indexing, then reversed on return.
<langsyntaxhighlight lang="python">def closest_more_than(n, lst):
"(index of) closest int from lst, to n that is also > n"
large = max(lst) + 1
Line 1,645 ⟶ 2,296:
for x in [0, 9, 12, 21, 12453, 738440, 45072010, 95322020,
9589776899767587796600]:
print(f"{x:>12_d} -> {nexthigh(x):>12_d}")</langsyntaxhighlight>
 
{{out}}
Line 1,661 ⟶ 2,312:
===Python: Algorithm 1===
I would not try it on the stretch goal, otherwise results as above.
<langsyntaxhighlight lang="python">from itertools import permutations
 
 
Line 1,672 ⟶ 2,323:
if perm != this:
return int(''.join(perm))
return 0</langsyntaxhighlight>
 
===Python: Generator===
Line 1,678 ⟶ 2,329:
A variant which defines (in terms of a concatMap over permutations), a generator of '''all''' digit-shuffle successors for a given integer:
 
<langsyntaxhighlight lang="python">'''Next highest int from digits'''
 
from itertools import chain, islice, permutations, tee
Line 1,800 ⟶ 2,451:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Taking up to 5 digit-shuffle successors for each:
Line 1,812 ⟶ 2,463:
45072010 -> 5 of 1861: [45072100, 45100027, 45100072, 45100207, 45100270]
95322020 -> 1 of 1: [95322200]</pre>
 
=={{header|Quackery}}==
 
<code>nextperm</code> is defined at [[Permutations with some identical elements#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ [] swap
[ 10 /mod
rot join swap
dup 0 = until ]
drop ] is ->digits ( n --> [ )
 
[ 0 swap
witheach
[ swap 10 * + ] ] is digits-> ( [ --> n )
 
[ dup ->digits
nextperm
digits->
tuck < not if
[ drop 0 ] ] is task ( n- -> n )
' [ 0 9 12 21 12453 738440 45072010
95322020 9589776899767587796600 ]
witheach [ task echo sp ]</syntaxhighlight>
 
{{out}}
 
<pre>0 0 21 0 12534 740348 45072100 95322200 9589776899767587900667</pre>
 
=={{header|Raku}}==
Line 1,818 ⟶ 2,498:
Minimal error trapping. Assumes that the passed number is an integer. Handles positive or negative integers, always returns next largest regardless (if possible).
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
 
sub next-greatest-index ($str, &op = &infix:«<» ) {
Line 1,850 ⟶ 2,530:
printf "%30s -> %s%s\n", .&comma, .&next-greatest-integer < 0 ?? '' !! ' ', .&next-greatest-integer.&comma for
flat 0, (9, 12, 21, 12453, 738440, 45072010, 95322020, 9589776899767587796600, 3345333,
95897768997675877966000000000000000000000000000000000000000000000000000000000000000000).map: { $_, -$_ };</langsyntaxhighlight>
{{out}}
<pre>Next largest integer able to be made from these digits, or zero if no larger exists:
Line 1,876 ⟶ 2,556:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program finds the next highest positive integer from a list of decimal digits. */
parse arg n /*obtain optional arguments from the CL*/
if n='' | n="," then n= 0 9 12 21 12453 738440 45072010 95322020 /*use the defaults?*/
Line 1,900 ⟶ 2,580:
end /*k*/
do m=0 for 10; if @.m==0 then iterate; $= $ || copies(m, @.m)
end /*m*/; return $ /* [↑] build a sorted digit mask.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,914 ⟶ 2,594:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 2,013 ⟶ 2,693:
last -= 1
end
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,026 ⟶ 2,706:
</pre>
 
=={{header|RPL}}==
{{works with|HP|48G}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ '''IF''' DUP SIZE 1 == '''THEN''' DROP 1 GET '''ELSE''' STREAM '''END'''
≫ '<span style="color:blue">REDUCE</span>' STO
'''IF''' DUP 9 > '''THEN'''
DUP MANT IP SWAP →STR DUP SIZE 0 → digit num size pos
≪ { } digit +
2 size '''FOR''' j
num j DUP SUB STR→
'''IF''' DUP digit > '''THEN''' j 1 - ‘pos’ STO '''END'''
DUP ‘digit’ STO +
'''NEXT'''
'''IF''' pos '''THEN'''
DUP pos GET ‘digit’ STO
DUP pos 1 + size SUB
DUP digit - DUP 0 ≤ 100 * ADD
≪ MIN ≫ <span style="color:blue">REDUCE</span> digit + POS pos +
DUP2 GET ROT pos ROT PUT SWAP digit PUT
DUP ‘pos’ INCR size SUB SORT pos SWAP REPL
≪ SWAP 10 * + ≫ <span style="color:blue">REDUCE</span>
'''ELSE''' DROP num STR→ '''END'''
≫ '''END'''
≫ '<span style="color:blue">NEXTHI</span>' STO
|
<span style="color:blue">REDUCE</span> ''( { list } ≪ func ≫ → func(list) ) ''
<span style="color:blue">NEXTHI</span> ''( int → next_highest_int ) ''
if int > 9 then
initialize variables
initialize list of digits with digit #1
for j = 2 to last digit index
get digit
if higher than previous digit, store position
store digit as previous and append to list
end loop
if there is an highest int
get d1 = first digit to swap
take the rest of list
look for d2 = lowest digit being greater than d1
swap d1 and d2
sort all digits at right of d1
turn the list of digits into a number
else return the initial number
|}
{0 9 12 21 12453 738440 45072010 95322020} 1 ≪ <span style="color:blue">NEXTHI</span> ≫ DOLIST
{{out}}
<pre>
1: { 0 9 21 21 12534 740348 45072100 95322200 }
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">def next_perm(ar)
ar = ar.dup
rev_ar = ar.reverse
(a, pivot), i = rev_ar.each_cons(2).with_index(1).detect{|(e1, e2),i| e1 > e2}
return [0] if i.nil?
suffix = ar[-i..]
min_dif = suffix.map{|el|el-pivot }.reject{|el|el <= 0}.min
ri = ar.rindex{|el| el == min_dif + pivot}
ar[-(i+1)], ar[ri] = ar[ri], ar[-(i+1)]
ar[-i..] = ar[-i..].reverse
ar
end
 
tests = [0, 9, 12, 21, 12453, 738440, 45072010, 95322020, 9589776899767587796600]
tests.each{|t| puts "%25d -> %d" % [t, next_perm(t.to_s.chars.map(&:to_i)).join]}
</syntaxhighlight>
 
{{out}}
<pre> 0 -> 0
9 -> 0
12 -> 21
21 -> 0
12453 -> 12534
738440 -> 740348
45072010 -> 45072100
95322020 -> 95322200
9589776899767587796600 -> 9589776899767587900667
</pre>
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">fn next_permutation<T: PartialOrd>(array: &mut [T]) -> bool {
let len = array.len();
if len < 2 {
Line 2,062 ⟶ 2,832:
println!("{} -> {}", n, next_highest_int(*n));
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,078 ⟶ 2,848:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func next_from_digits(n, b = 10) {
 
var a = n.digits(b).flip
Line 2,098 ⟶ 2,868:
) {
printf("%30s -> %s\n", n, next_from_digits(n))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,120 ⟶ 2,890:
{{libheader|Wren-fmt}}
{{libheader|Wren-str}}
<langsyntaxhighlight ecmascriptlang="wren">import "./sort" for Sort, Find
import "./fmt" for Fmt
import "./str" for Str
 
var permute = Fn.new { |s|
Line 2,227 ⟶ 2,997:
var nums = ["0", "9", "12", "21", "12453", "738440", "45072010", "95322020", "9589776899767587796600"]
algorithm1.call(nums[0...-1]) // exclude the last one
algorithm2.call(nums) // include the last one</langsyntaxhighlight>
 
{{out}}
Line 2,256 ⟶ 3,026:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn nextHightest(N){ // N is int, BigInt or string -->String. Algorithm 2
// ds:=N.split().copy(); // mutable, int
ds:=N.toString().split("").apply("toInt").copy(); // handle "234" or BigInt
Line 2,272 ⟶ 3,042:
}
"0"
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">ns:=T(0, 9, 12, 21, 12453, 738440, 45072010, 95322020);
foreach n in (ns){ println("%,d --> %,d".fmt(n,nextHightest(n))) }
 
n:="9589776899767587796600"; // or BigInt(n)
println("%s --> %s".fmt(n,nextHightest(n)));</langsyntaxhighlight>
{{out}}
<pre>
2,122

edits