WeixiangYan/CodeTransOcean huggingface dataset:
Task: RSA code

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>

int main(void)
{
    mpz_t n, d, e, pt, ct;

    mpz_init(pt);
    mpz_init(ct);
    mpz_init_set_str(n, "9516311845790656153499716760847001433441357", 10);
    mpz_init_set_str(e, "65537", 10);
    mpz_init_set_str(d, "5617843187844953170308463622230283376298685", 10);

    const char *plaintext = "Rossetta Code";
    mpz_import(pt, strlen(plaintext), 1, 1, 0, 0, plaintext);

    if (mpz_cmp(pt, n) > 0)
        abort();

    mpz_powm(ct, pt, e, n);
    gmp_printf("Encoded:   %Zd\n", ct);

    mpz_powm(pt, ct, d, n);
    gmp_printf("Decoded:   %Zd\n", pt);

    char buffer[64];
    mpz_export(buffer, NULL, 1, 1, 0, 0, pt);
    printf("As String: %s\n", buffer);

    mpz_clears(pt, ct, n, e, d, NULL);
    return 0;
}











Ada: WITH GMP, GMP.Integers, Ada.Text_IO, GMP.Integers.Aliased_Internal_Value, Interfaces.C;
USE GMP, Gmp.Integers, Ada.Text_IO, Interfaces.C;

PROCEDURE Main IS
   FUNCTION "+" (U : Unbounded_Integer) RETURN Mpz_T IS (Aliased_Internal_Value (U));
   FUNCTION "+" (S : String) RETURN Unbounded_Integer IS (To_Unbounded_Integer (S));
   FUNCTION Image_Cleared (M : Mpz_T) RETURN String IS (Image (To_Unbounded_Integer (M)));
   N                 : Unbounded_Integer := +"9516311845790656153499716760847001433441357";
   E                 : Unbounded_Integer := +"65537";
   D                 : Unbounded_Integer := +"5617843187844953170308463622230283376298685";
   Plain_Text        : CONSTANT String := "Rosetta Code";
   M, M_C, M_D       : Mpz_T;

   PROCEDURE Mpz_Import
     (Rop   : Mpz_T; Count : Size_T; Order : Int; Size : Size_T; Endian : Int;
      Nails : Size_T; Op : Char_Array);
   PRAGMA Import (C, Mpz_Import, "__gmpz_import");

   PROCEDURE Mpz_Export
     (Rop    : OUT Char_Array; Count : ACCESS Size_T; Order : Int; Size : Size_T;
      Endian : Int; Nails : Size_T; Op : Mpz_T);
   PRAGMA Import (C, Mpz_Export, "__gmpz_export");
BEGIN
   Mpz_Init (M);
   Mpz_Init (M_C);
   Mpz_Init (M_D);
   Mpz_Import (M, Plain_Text'Length + 1, 1, 1, 0, 0, To_C (Plain_Text));
   Mpz_Powm (M_C, M, +E, +N);
   Mpz_Powm (M_D, M_C, +D, +N);   
   Put_Line ("Encoded plain text: " & Image_Cleared (M));
   DECLARE Decrypted : Char_Array (1 .. Mpz_Sizeinbase (M_C, 256));
   BEGIN      
      Put_Line ("Encryption of this encoding: " & Image_Cleared (M_C));
      Mpz_Export (Decrypted, NULL, 1, 1, 0, 0, M_D);
      Put_Line ("Decryption of the encoding: " & Image_Cleared (M_D));
      Put_Line ("Final decryption: " &  To_Ada (Decrypted));
   END;
END Main;
Task: RSA code




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    var n, e, d, bb, ptn, etn, dtn big.Int
    pt := "Rosetta Code"
    fmt.Println("Plain text:            ", pt)

    
    
    
    n.SetString("9516311845790656153499716760847001433441357", 10)
    e.SetString("65537", 10)
    d.SetString("5617843187844953170308463622230283376298685", 10)

    
    for _, b := range []byte(pt) {
        ptn.Or(ptn.Lsh(&ptn, 8), bb.SetInt64(int64(b)))
    }
    if ptn.Cmp(&n) >= 0 {
        fmt.Println("Plain text message too long")
        return
    }
    fmt.Println("Plain text as a number:", &ptn)

    
    etn.Exp(&ptn, &e, &n)
    fmt.Println("Encoded:               ", &etn)

    
    dtn.Exp(&etn, &d, &n)
    fmt.Println("Decoded:               ", &dtn)

    
    var db [16]byte
    dx := 16
    bff := big.NewInt(0xff)
    for dtn.BitLen() > 0 {
        dx--
        db[dx] = byte(bb.And(&dtn, bff).Int64())
        dtn.Rsh(&dtn, 8)
    }
    fmt.Println("Decoded number as text:", string(db[dx:]))
}








Ada: WITH GMP, GMP.Integers, Ada.Text_IO, GMP.Integers.Aliased_Internal_Value, Interfaces.C;
USE GMP, Gmp.Integers, Ada.Text_IO, Interfaces.C;

PROCEDURE Main IS
   FUNCTION "+" (U : Unbounded_Integer) RETURN Mpz_T IS (Aliased_Internal_Value (U));
   FUNCTION "+" (S : String) RETURN Unbounded_Integer IS (To_Unbounded_Integer (S));
   FUNCTION Image_Cleared (M : Mpz_T) RETURN String IS (Image (To_Unbounded_Integer (M)));
   N                 : Unbounded_Integer := +"9516311845790656153499716760847001433441357";
   E                 : Unbounded_Integer := +"65537";
   D                 : Unbounded_Integer := +"5617843187844953170308463622230283376298685";
   Plain_Text        : CONSTANT String := "Rosetta Code";
   M, M_C, M_D       : Mpz_T;

   PROCEDURE Mpz_Import
     (Rop   : Mpz_T; Count : Size_T; Order : Int; Size : Size_T; Endian : Int;
      Nails : Size_T; Op : Char_Array);
   PRAGMA Import (C, Mpz_Import, "__gmpz_import");

   PROCEDURE Mpz_Export
     (Rop    : OUT Char_Array; Count : ACCESS Size_T; Order : Int; Size : Size_T;
      Endian : Int; Nails : Size_T; Op : Mpz_T);
   PRAGMA Import (C, Mpz_Export, "__gmpz_export");
BEGIN
   Mpz_Init (M);
   Mpz_Init (M_C);
   Mpz_Init (M_D);
   Mpz_Import (M, Plain_Text'Length + 1, 1, 1, 0, 0, To_C (Plain_Text));
   Mpz_Powm (M_C, M, +E, +N);
   Mpz_Powm (M_D, M_C, +D, +N);   
   Put_Line ("Encoded plain text: " & Image_Cleared (M));
   DECLARE Decrypted : Char_Array (1 .. Mpz_Sizeinbase (M_C, 256));
   BEGIN      
      Put_Line ("Encryption of this encoding: " & Image_Cleared (M_C));
      Mpz_Export (Decrypted, NULL, 1, 1, 0, 0, M_D);
      Put_Line ("Decryption of the encoding: " & Image_Cleared (M_D));
      Put_Line ("Final decryption: " &  To_Ada (Decrypted));
   END;
END Main;
Task: RSA code





Java: public static void main(String[] args) {
    
    BigInteger n = new BigInteger("9516311845790656153499716760847001433441357");
    BigInteger e = new BigInteger("65537");
    BigInteger d = new BigInteger("5617843187844953170308463622230283376298685");
    Charset c = Charsets.UTF_8;
    String plainText = "Rosetta Code";
    System.out.println("PlainText : " + plainText);
    byte[] bytes = plainText.getBytes();
    BigInteger plainNum = new BigInteger(bytes);
    System.out.println("As number : " + plainNum);
    BigInteger Bytes = new BigInteger(bytes);
    if (Bytes.compareTo(n) == 1) {
        System.out.println("Plaintext is too long");
        return;
    }
    BigInteger enc = plainNum.modPow(e, n);
    System.out.println("Encoded: " + enc);
    BigInteger dec = enc.modPow(d, n);
    System.out.println("Decoded: " + dec);
    String decText = new String(dec.toByteArray(), c);
    System.out.println("As text: " + decText);
}







Ada: WITH GMP, GMP.Integers, Ada.Text_IO, GMP.Integers.Aliased_Internal_Value, Interfaces.C;
USE GMP, Gmp.Integers, Ada.Text_IO, Interfaces.C;

PROCEDURE Main IS
   FUNCTION "+" (U : Unbounded_Integer) RETURN Mpz_T IS (Aliased_Internal_Value (U));
   FUNCTION "+" (S : String) RETURN Unbounded_Integer IS (To_Unbounded_Integer (S));
   FUNCTION Image_Cleared (M : Mpz_T) RETURN String IS (Image (To_Unbounded_Integer (M)));
   N                 : Unbounded_Integer := +"9516311845790656153499716760847001433441357";
   E                 : Unbounded_Integer := +"65537";
   D                 : Unbounded_Integer := +"5617843187844953170308463622230283376298685";
   Plain_Text        : CONSTANT String := "Rosetta Code";
   M, M_C, M_D       : Mpz_T;

   PROCEDURE Mpz_Import
     (Rop   : Mpz_T; Count : Size_T; Order : Int; Size : Size_T; Endian : Int;
      Nails : Size_T; Op : Char_Array);
   PRAGMA Import (C, Mpz_Import, "__gmpz_import");

   PROCEDURE Mpz_Export
     (Rop    : OUT Char_Array; Count : ACCESS Size_T; Order : Int; Size : Size_T;
      Endian : Int; Nails : Size_T; Op : Mpz_T);
   PRAGMA Import (C, Mpz_Export, "__gmpz_export");
BEGIN
   Mpz_Init (M);
   Mpz_Init (M_C);
   Mpz_Init (M_D);
   Mpz_Import (M, Plain_Text'Length + 1, 1, 1, 0, 0, To_C (Plain_Text));
   Mpz_Powm (M_C, M, +E, +N);
   Mpz_Powm (M_D, M_C, +D, +N);   
   Put_Line ("Encoded plain text: " & Image_Cleared (M));
   DECLARE Decrypted : Char_Array (1 .. Mpz_Sizeinbase (M_C, 256));
   BEGIN      
      Put_Line ("Encryption of this encoding: " & Image_Cleared (M_C));
      Mpz_Export (Decrypted, NULL, 1, 1, 0, 0, M_D);
      Put_Line ("Decryption of the encoding: " & Image_Cleared (M_D));
      Put_Line ("Final decryption: " &  To_Ada (Decrypted));
   END;
END Main;
Task: RSA code







Python: import binascii

n = 9516311845790656153499716760847001433441357    
e = 65537
d = 5617843187844953170308463622230283376298685

message='Rosetta Code!'
print('message                 ', message)

hex_data   = binascii.hexlify(message.encode())
print('hex data                ', hex_data)

plain_text = int(hex_data, 16)
print('plain text integer      ', plain_text)

if plain_text > n:
  raise Exception('plain text too large for key')

encrypted_text = pow(plain_text,     e, n)
print('encrypted text integer  ', encrypted_text)

decrypted_text = pow(encrypted_text, d, n)
print('decrypted text integer  ', decrypted_text)

print('message                 ', binascii.unhexlify(hex(decrypted_text)[2:]).decode())     





Ada: WITH GMP, GMP.Integers, Ada.Text_IO, GMP.Integers.Aliased_Internal_Value, Interfaces.C;
USE GMP, Gmp.Integers, Ada.Text_IO, Interfaces.C;

PROCEDURE Main IS
   FUNCTION "+" (U : Unbounded_Integer) RETURN Mpz_T IS (Aliased_Internal_Value (U));
   FUNCTION "+" (S : String) RETURN Unbounded_Integer IS (To_Unbounded_Integer (S));
   FUNCTION Image_Cleared (M : Mpz_T) RETURN String IS (Image (To_Unbounded_Integer (M)));
   N                 : Unbounded_Integer := +"9516311845790656153499716760847001433441357";
   E                 : Unbounded_Integer := +"65537";
   D                 : Unbounded_Integer := +"5617843187844953170308463622230283376298685";
   Plain_Text        : CONSTANT String := "Rosetta Code";
   M, M_C, M_D       : Mpz_T;

   PROCEDURE Mpz_Import
     (Rop   : Mpz_T; Count : Size_T; Order : Int; Size : Size_T; Endian : Int;
      Nails : Size_T; Op : Char_Array);
   PRAGMA Import (C, Mpz_Import, "__gmpz_import");

   PROCEDURE Mpz_Export
     (Rop    : OUT Char_Array; Count : ACCESS Size_T; Order : Int; Size : Size_T;
      Endian : Int; Nails : Size_T; Op : Mpz_T);
   PRAGMA Import (C, Mpz_Export, "__gmpz_export");
BEGIN
   Mpz_Init (M);
   Mpz_Init (M_C);
   Mpz_Init (M_D);
   Mpz_Import (M, Plain_Text'Length + 1, 1, 1, 0, 0, To_C (Plain_Text));
   Mpz_Powm (M_C, M, +E, +N);
   Mpz_Powm (M_D, M_C, +D, +N);   
   Put_Line ("Encoded plain text: " & Image_Cleared (M));
   DECLARE Decrypted : Char_Array (1 .. Mpz_Sizeinbase (M_C, 256));
   BEGIN      
      Put_Line ("Encryption of this encoding: " & Image_Cleared (M_C));
      Mpz_Export (Decrypted, NULL, 1, 1, 0, 0, M_D);
      Put_Line ("Decryption of the encoding: " & Image_Cleared (M_D));
      Put_Line ("Final decryption: " &  To_Ada (Decrypted));
   END;
END Main;
Task: RSA code








VB: Imports System
Imports System.Numerics
Imports System.Text

Module Module1
    Sub Main()
        Dim n As BigInteger = BigInteger.Parse("9516311845790656153499716760847001433441357")
        Dim e As BigInteger = 65537
        Dim d As BigInteger = BigInteger.Parse("5617843187844953170308463622230283376298685")
        Dim plainTextStr As String = "Hello, Rosetta!"
        Dim plainTextBA As Byte() = ASCIIEncoding.ASCII.GetBytes(plainTextStr)
        Dim pt As BigInteger = New BigInteger(plainTextBA)
        If pt > n Then Throw New Exception() 
        Dim ct As BigInteger = BigInteger.ModPow(pt, e, n)
        Console.WriteLine(" Encoded: " & ct.ToString("X"))
        Dim dc As BigInteger = BigInteger.ModPow(ct, d, n)
        Console.WriteLine(" Decoded: " & dc.ToString("X"))
        Dim decoded As String = ASCIIEncoding.ASCII.GetString(dc.ToByteArray())
        Console.WriteLine("As ASCII: " & decoded)
    End Sub
End Module




Ada: WITH GMP, GMP.Integers, Ada.Text_IO, GMP.Integers.Aliased_Internal_Value, Interfaces.C;
USE GMP, Gmp.Integers, Ada.Text_IO, Interfaces.C;

PROCEDURE Main IS
   FUNCTION "+" (U : Unbounded_Integer) RETURN Mpz_T IS (Aliased_Internal_Value (U));
   FUNCTION "+" (S : String) RETURN Unbounded_Integer IS (To_Unbounded_Integer (S));
   FUNCTION Image_Cleared (M : Mpz_T) RETURN String IS (Image (To_Unbounded_Integer (M)));
   N                 : Unbounded_Integer := +"9516311845790656153499716760847001433441357";
   E                 : Unbounded_Integer := +"65537";
   D                 : Unbounded_Integer := +"5617843187844953170308463622230283376298685";
   Plain_Text        : CONSTANT String := "Rosetta Code";
   M, M_C, M_D       : Mpz_T;

   PROCEDURE Mpz_Import
     (Rop   : Mpz_T; Count : Size_T; Order : Int; Size : Size_T; Endian : Int;
      Nails : Size_T; Op : Char_Array);
   PRAGMA Import (C, Mpz_Import, "__gmpz_import");

   PROCEDURE Mpz_Export
     (Rop    : OUT Char_Array; Count : ACCESS Size_T; Order : Int; Size : Size_T;
      Endian : Int; Nails : Size_T; Op : Mpz_T);
   PRAGMA Import (C, Mpz_Export, "__gmpz_export");
BEGIN
   Mpz_Init (M);
   Mpz_Init (M_C);
   Mpz_Init (M_D);
   Mpz_Import (M, Plain_Text'Length + 1, 1, 1, 0, 0, To_C (Plain_Text));
   Mpz_Powm (M_C, M, +E, +N);
   Mpz_Powm (M_D, M_C, +D, +N);   
   Put_Line ("Encoded plain text: " & Image_Cleared (M));
   DECLARE Decrypted : Char_Array (1 .. Mpz_Sizeinbase (M_C, 256));
   BEGIN      
      Put_Line ("Encryption of this encoding: " & Image_Cleared (M_C));
      Mpz_Export (Decrypted, NULL, 1, 1, 0, 0, M_D);
      Put_Line ("Decryption of the encoding: " & Image_Cleared (M_D));
      Put_Line ("Final decryption: " &  To_Ada (Decrypted));
   END;
END Main;
Task: RSA code

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmp.h>

int main(void)
{
    mpz_t n, d, e, pt, ct;

    mpz_init(pt);
    mpz_init(ct);
    mpz_init_set_str(n, "9516311845790656153499716760847001433441357", 10);
    mpz_init_set_str(e, "65537", 10);
    mpz_init_set_str(d, "5617843187844953170308463622230283376298685", 10);

    const char *plaintext = "Rossetta Code";
    mpz_import(pt, strlen(plaintext), 1, 1, 0, 0, plaintext);

    if (mpz_cmp(pt, n) > 0)
        abort();

    mpz_powm(ct, pt, e, n);
    gmp_printf("Encoded:   %Zd\n", ct);

    mpz_powm(pt, ct, d, n);
    gmp_printf("Decoded:   %Zd\n", pt);

    char buffer[64];
    mpz_export(buffer, NULL, 1, 1, 0, 0, pt);
    printf("As String: %s\n", buffer);

    mpz_clears(pt, ct, n, e, d, NULL);
    return 0;
}
Task: RSA code



C#: using System;
using System.Numerics;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        BigInteger n = BigInteger.Parse("9516311845790656153499716760847001433441357");
        BigInteger e = 65537;
        BigInteger d = BigInteger.Parse("5617843187844953170308463622230283376298685");

        const string plaintextstring = "Hello, Rosetta!";
        byte[] plaintext = ASCIIEncoding.ASCII.GetBytes(plaintextstring);
        BigInteger pt = new BigInteger(plaintext);
        if (pt > n)
            throw new Exception();

        BigInteger ct = BigInteger.ModPow(pt, e, n);
        Console.WriteLine("Encoded:  " + ct);

        BigInteger dc = BigInteger.ModPow(ct, d, n);
        Console.WriteLine("Decoded:  " + dc);

        string decoded = ASCIIEncoding.ASCII.GetString(dc.ToByteArray());
        Console.WriteLine("As ASCII: " + decoded);
    }
}
Task: RSA code





Java: public static void main(String[] args) {
    
    BigInteger n = new BigInteger("9516311845790656153499716760847001433441357");
    BigInteger e = new BigInteger("65537");
    BigInteger d = new BigInteger("5617843187844953170308463622230283376298685");
    Charset c = Charsets.UTF_8;
    String plainText = "Rosetta Code";
    System.out.println("PlainText : " + plainText);
    byte[] bytes = plainText.getBytes();
    BigInteger plainNum = new BigInteger(bytes);
    System.out.println("As number : " + plainNum);
    BigInteger Bytes = new BigInteger(bytes);
    if (Bytes.compareTo(n) == 1) {
        System.out.println("Plaintext is too long");
        return;
    }
    BigInteger enc = plainNum.modPow(e, n);
    System.out.println("Encoded: " + enc);
    BigInteger dec = enc.modPow(d, n);
    System.out.println("Decoded: " + dec);
    String decText = new String(dec.toByteArray(), c);
    System.out.println("As text: " + decText);
}
Task: RSA code







Python: import binascii

n = 9516311845790656153499716760847001433441357    
e = 65537
d = 5617843187844953170308463622230283376298685

message='Rosetta Code!'
print('message                 ', message)

hex_data   = binascii.hexlify(message.encode())
print('hex data                ', hex_data)

plain_text = int(hex_data, 16)
print('plain text integer      ', plain_text)

if plain_text > n:
  raise Exception('plain text too large for key')

encrypted_text = pow(plain_text,     e, n)
print('encrypted text integer  ', encrypted_text)

decrypted_text = pow(encrypted_text, d, n)
print('decrypted text integer  ', decrypted_text)

print('message                 ', binascii.unhexlify(hex(decrypted_text)[2:]).decode())
Task: RSA code








VB: Imports System
Imports System.Numerics
Imports System.Text

Module Module1
    Sub Main()
        Dim n As BigInteger = BigInteger.Parse("9516311845790656153499716760847001433441357")
        Dim e As BigInteger = 65537
        Dim d As BigInteger = BigInteger.Parse("5617843187844953170308463622230283376298685")
        Dim plainTextStr As String = "Hello, Rosetta!"
        Dim plainTextBA As Byte() = ASCIIEncoding.ASCII.GetBytes(plainTextStr)
        Dim pt As BigInteger = New BigInteger(plainTextBA)
        If pt > n Then Throw New Exception() 
        Dim ct As BigInteger = BigInteger.ModPow(pt, e, n)
        Console.WriteLine(" Encoded: " & ct.ToString("X"))
        Dim dc As BigInteger = BigInteger.ModPow(ct, d, n)
        Console.WriteLine(" Decoded: " & dc.ToString("X"))
        Dim decoded As String = ASCIIEncoding.ASCII.GetString(dc.ToByteArray())
        Console.WriteLine("As ASCII: " & decoded)
    End Sub
End Module
Task: RSA code




Go: package main

import (
    "fmt"
    "math/big"
)

func main() {
    var n, e, d, bb, ptn, etn, dtn big.Int
    pt := "Rosetta Code"
    fmt.Println("Plain text:            ", pt)

    
    
    
    n.SetString("9516311845790656153499716760847001433441357", 10)
    e.SetString("65537", 10)
    d.SetString("5617843187844953170308463622230283376298685", 10)

    
    for _, b := range []byte(pt) {
        ptn.Or(ptn.Lsh(&ptn, 8), bb.SetInt64(int64(b)))
    }
    if ptn.Cmp(&n) >= 0 {
        fmt.Println("Plain text message too long")
        return
    }
    fmt.Println("Plain text as a number:", &ptn)

    
    etn.Exp(&ptn, &e, &n)
    fmt.Println("Encoded:               ", &etn)

    
    dtn.Exp(&etn, &d, &n)
    fmt.Println("Decoded:               ", &dtn)

    
    var db [16]byte
    dx := 16
    bff := big.NewInt(0xff)
    for dtn.BitLen() > 0 {
        dx--
        db[dx] = byte(bb.And(&dtn, bff).Int64())
        dtn.Rsh(&dtn, 8)
    }
    fmt.Println("Decoded number as text:", string(db[dx:]))
}
Task: Sort numbers lexicographically



C#: using static System.Console;
using static System.Linq.Enumerable;

public class Program
{
    public static void Main() {
        foreach (int n in new [] { 0, 5, 13, 21, -22 }) WriteLine($"{n}: {string.Join(", ", LexOrder(n))}");
    }

    public static IEnumerable<int> LexOrder(int n) => (n < 1 ? Range(n, 2 - n) : Range(1, n)).OrderBy(i => i.ToString());
}









Ada: WITH Ada.Containers.Generic_Array_Sort, Ada.Text_IO;
USE  Ada.Text_IO;
PROCEDURE Main IS
   TYPE Natural_Array IS ARRAY (Positive RANGE <>) OF Natural;
   FUNCTION Less (L, R : Natural) RETURN Boolean IS (L'Img < R'Img);
   PROCEDURE Sort_Naturals IS NEW Ada.Containers.Generic_Array_Sort
     (Positive, Natural, Natural_Array, Less);
   PROCEDURE Show (Last : Natural) IS
      A : Natural_Array (1 .. Last);
   BEGIN
      FOR I IN A'Range LOOP A (I) := I; END LOOP;
      Sort_Naturals (A);
      FOR I IN  A'Range LOOP Put (A (I)'Img); END LOOP;
      New_Line;
   END Show;
BEGIN
   Show (13);
   Show (21);
END Main;
Task: Sort numbers lexicographically

C: #include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compareStrings(const void *a, const void *b) {
    const char **aa = (const char **)a;
    const char **bb = (const char **)b;
    return strcmp(*aa, *bb);
}

void lexOrder(int n, int *ints) {
    char **strs;
    int i, first = 1, last = n, k = n, len;
    if (n < 1) {
        first = n; last = 1; k = 2 - n;
    } 
    strs = malloc(k * sizeof(char *));
    for (i = first; i <= last; ++i) {
        if (i >= 1) len = (int)log10(i) + 2;
        else if (i == 0) len = 2;
        else len = (int)log10(-i) + 3; 
        strs[i-first] = malloc(len);
        sprintf(strs[i-first], "%d", i);
    }
    qsort(strs, k, sizeof(char *), compareStrings);
    for (i = 0; i < k; ++i) {
        ints[i] = atoi(strs[i]);
        free(strs[i]);
    }
    free(strs);
}

int main() {
    int i, j, k, n,  *ints;
    int numbers[5] = {0, 5, 13, 21, -22};
    printf("In lexicographical order:\n\n");
    for (i = 0; i < 5; ++i) {
        k = n = numbers[i];
        if (k < 1) k = 2 - k;
        ints = malloc(k * sizeof(int));
        lexOrder(n, ints);
        printf("%3d: [", n);
        for (j = 0; j < k; ++j) {
            printf("%d ", ints[j]);
        }
        printf("\b]\n");
        free(ints);
    }
    return 0;
}











Ada: WITH Ada.Containers.Generic_Array_Sort, Ada.Text_IO;
USE  Ada.Text_IO;
PROCEDURE Main IS
   TYPE Natural_Array IS ARRAY (Positive RANGE <>) OF Natural;
   FUNCTION Less (L, R : Natural) RETURN Boolean IS (L'Img < R'Img);
   PROCEDURE Sort_Naturals IS NEW Ada.Containers.Generic_Array_Sort
     (Positive, Natural, Natural_Array, Less);
   PROCEDURE Show (Last : Natural) IS
      A : Natural_Array (1 .. Last);
   BEGIN
      FOR I IN A'Range LOOP A (I) := I; END LOOP;
      Sort_Naturals (A);
      FOR I IN  A'Range LOOP Put (A (I)'Img); END LOOP;
      New_Line;
   END Show;
BEGIN
   Show (13);
   Show (21);
END Main;
Task: Sort numbers lexicographically


C++: #include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

void lexicographical_sort(std::vector<int>& numbers) {
    std::vector<std::string> strings(numbers.size());
    std::transform(numbers.begin(), numbers.end(), strings.begin(),
                   [](int i) { return std::to_string(i); });
    std::sort(strings.begin(), strings.end());
    std::transform(strings.begin(), strings.end(), numbers.begin(),
                   [](const std::string& s) { return std::stoi(s); });
}

std::vector<int> lexicographically_sorted_vector(int n) {
    std::vector<int> numbers(n >= 1 ? n : 2 - n);
    std::iota(numbers.begin(), numbers.end(), std::min(1, n));
    lexicographical_sort(numbers);
    return numbers;
}

template <typename T>
void print_vector(std::ostream& out, const std::vector<T>& v) {
    out << '[';
    if (!v.empty()) {
        auto i = v.begin();
        out << *i++;
        for (; i != v.end(); ++i)
            out << ',' << *i;
    }
    out << "]\n";
}

int main(int argc, char** argv) {
    for (int i : { 0, 5, 13, 21, -22 }) {
        std::cout << i << ": ";
        print_vector(std::cout, lexicographically_sorted_vector(i));
    }
    return 0;
}










Ada: WITH Ada.Containers.Generic_Array_Sort, Ada.Text_IO;
USE  Ada.Text_IO;
PROCEDURE Main IS
   TYPE Natural_Array IS ARRAY (Positive RANGE <>) OF Natural;
   FUNCTION Less (L, R : Natural) RETURN Boolean IS (L'Img < R'Img);
   PROCEDURE Sort_Naturals IS NEW Ada.Containers.Generic_Array_Sort
     (Positive, Natural, Natural_Array, Less);
   PROCEDURE Show (Last : Natural) IS
      A : Natural_Array (1 .. Last);
   BEGIN
      FOR I IN A'Range LOOP A (I) := I; END LOOP;
      Sort_Naturals (A);
      FOR I IN  A'Range LOOP Put (A (I)'Img); END LOOP;
      New_Line;
   END Show;
BEGIN
   Show (13);
   Show (21);
END Main;