How to resolve the algorithm Next highest int from digits step by step in the Factor programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Next highest int from digits step by step in the Factor programming language

Table of Contents

Problem Statement

Given a zero or positive integer, the task is to generate the next largest integer using only the given digits*1.

The above could prove slow and memory hungry for numbers with large numbers of digits, but should be easy to reason about its correctness.

E.g.: This second algorithm is faster and more memory efficient, but implementations may be harder to test. One method of testing, (as used in developing the task),   is to compare results from both algorithms for random numbers generated from a range that the first algorithm can handle.

Calculate the next highest int from the digits of the following numbers:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Next highest int from digits step by step in the Factor programming language

Source code in the factor programming language

USING: formatting grouping kernel math math.combinatorics
math.parser sequences ;

: next-highest ( m -- n )
    number>string dup [ >= ] monotonic?
    [ drop 0 ] [ next-permutation string>number ] if ;

{
    0 9 12 21 12453 738440 45072010 95322020
    9589776899767587796600
}
[ dup next-highest "%d -> %d\n" printf ] each


  

You may also check:How to resolve the algorithm Ordered words step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Duffinian numbers step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Literals/String step by step in the LiveCode programming language
You may also check:How to resolve the algorithm String concatenation step by step in the D programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Seed7 programming language