WeixiangYan/CodeTransOcean huggingface dataset:
Task: Ascending primes



C#: using System;
using System.Collections.Generic;

namespace ascendingprimes
{
    class Program
    {
        static bool isPrime(uint n)
        {
            if (n == 2)
                return true;
            if (n == 1 || n % 2 = 0)
                return false;
            uint root = (uint)Math.Sqrt(n);
            for (uint k = 3; k <= root; k += 2)
                if (n % k == 0)
                    return false;
            return true;
        }
        static void Main(string[] args)
        {
            var queue = new Queue<uint>();
            var primes = new List<uint>();

            for (uint k = 1; k <= 9; k++)
                queue.Enqueue(k);
            while(queue.Count > 0)
            {
                uint n = queue.Dequeue();
                if (isPrime(n))
                    primes.Add(n);
                for (uint k = n % 10 + 1; k <= 9; k++)
                    queue.Enqueue(n * 10 + k);
            }

            foreach (uint p in primes)
            {
                Console.Write(p);
                Console.Write(" ");
            }
            Console.WriteLine();
        }
    }
}









Ada: #!/usr/local/bin/spar
  
pragma annotate( summary, "primes_asc" );
pragma annotate( description, "Generate and show all primes with strictly ascending decimal digits" );
pragma annotate( description, "Translation of Pascal" );
pragma annotate( see_also, "https://rosettacode.org/wiki/Ascending_primes" );
pragma annotate( author, "Ken O. Burtch" );

pragma software_model( nonstandard );
pragma restriction( no_external_commands );

procedure primes_asc is
  maxsize : constant natural := 1000;

  queue : array(1..maxsize) of natural;
  primes: array(1..maxsize) of natural;

  b : natural;
  e : natural;
  n : natural;
  v : natural;

function is_prime(num: integer) return boolean is
    found : boolean;
    num_root : natural;
    k : natural;
  begin
    if num = 2 then
      found;
    elsif (num = 1) or (num mod 2 = 0) then
      found := false;
    else
      num_root := numerics.truncation(numerics.sqrt(num));
      found;
      k := 3;
      while found and (k <= num_root) loop
        if num mod k = 0 then
          found := false;
        else
          k := @ + 2;
        end if;
      end loop;
   end if;
    return found;
  end is_prime;

begin
  b := 1;
  e := 1;
  n := 0;

  for k in 1..9 loop
    queue(e) := k;
    e := e + 1;
  end loop;

  while b < e loop
    v := queue(b);
    b := @ + 1;
    if is_prime(v) then
      n := @ + 1;
      primes(n) := v;
    end if;

    for k in v mod 10 + 1..9 loop
      queue(e) := v * 10 + k;
      e := @ + 1;
    end loop;
  end loop;

  for k in 1..n loop
    put(primes(k), "ZZZZZZZZ9");
    if k mod 8 = 0 then
       new_line;
    end if;
  end loop;
  new_line;
end primes_asc;
Task: Ascending primes

C: 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

#if UINT_MAX < 123456789
#error "we need at least 9 decimal digits (32-bit integers)"
#endif


#define MAXSIZE 1000

unsigned queue[MAXSIZE];
unsigned primes[MAXSIZE];

unsigned begin = 0;
unsigned end = 0;
unsigned n = 0;


bool isPrime(unsigned n)
{
    if (n == 2)
    {
        return true;
    }
    if (n == 1 || n % 2 == 0)
    {
        return false;
    }
    unsigned root = sqrt(n);
    for (unsigned k = 3; k <= root; k += 2)
    {
        if (n % k == 0)
        {
            return false;
        }
    }
    return true;
}


int main(int argc, char argv[])
{
    for (int k = 1; k <= 9; k++)
    {
        queue[end++] = k;
    }

    while (begin < end)
    {
        int value = queue[begin++];
        if (isPrime(value))
        {
            primes[n++] = value;
        }
        for (int k = value % 10 + 1; k <= 9; k++)
        {
            queue[end++] = value * 10 + k;
        }
    }

    for (int k = 0; k < n; k++)
    {
        printf("%u ", primes[k]);
    }
 
    return EXIT_SUCCESS;
}











Ada: #!/usr/local/bin/spar
  
pragma annotate( summary, "primes_asc" );
pragma annotate( description, "Generate and show all primes with strictly ascending decimal digits" );
pragma annotate( description, "Translation of Pascal" );
pragma annotate( see_also, "https://rosettacode.org/wiki/Ascending_primes" );
pragma annotate( author, "Ken O. Burtch" );

pragma software_model( nonstandard );
pragma restriction( no_external_commands );

procedure primes_asc is
  maxsize : constant natural := 1000;

  queue : array(1..maxsize) of natural;
  primes: array(1..maxsize) of natural;

  b : natural;
  e : natural;
  n : natural;
  v : natural;

function is_prime(num: integer) return boolean is
    found : boolean;
    num_root : natural;
    k : natural;
  begin
    if num = 2 then
      found;
    elsif (num = 1) or (num mod 2 = 0) then
      found := false;
    else
      num_root := numerics.truncation(numerics.sqrt(num));
      found;
      k := 3;
      while found and (k <= num_root) loop
        if num mod k = 0 then
          found := false;
        else
          k := @ + 2;
        end if;
      end loop;
   end if;
    return found;
  end is_prime;

begin
  b := 1;
  e := 1;
  n := 0;

  for k in 1..9 loop
    queue(e) := k;
    e := e + 1;
  end loop;

  while b < e loop
    v := queue(b);
    b := @ + 1;
    if is_prime(v) then
      n := @ + 1;
      primes(n) := v;
    end if;

    for k in v mod 10 + 1..9 loop
      queue(e) := v * 10 + k;
      e := @ + 1;
    end loop;
  end loop;

  for k in 1..n loop
    put(primes(k), "ZZZZZZZZ9");
    if k mod 8 = 0 then
       new_line;
    end if;
  end loop;
  new_line;
end primes_asc;
Task: Ascending primes


C++: 

#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;


queue<unsigned> suspected;
vector<unsigned> primes;


bool isPrime(unsigned n)
{
    if (n == 2)
    {
        return true;
    }
    if (n == 1 || n % 2 == 0)
    {
        return false;
    }
    unsigned root = sqrt(n);
    for (unsigned k = 3; k <= root; k += 2)
    {
        if (n % k == 0)
        {
            return false;
        }
    }
    return true;
}


int main(int argc, char argv[])
{
    for (unsigned k = 1; k <= 9; k++)
    {
        suspected.push(k);
    }

    while (!suspected.empty())
    {
        int n = suspected.front();
        suspected.pop();

        if (isPrime(n))
        {
            primes.push_back(n);
        }

        
        
        for (unsigned k = n % 10 + 1; k <= 9; k++)
        {
            suspected.push(n * 10 + k);
        }
    }

    copy(primes.begin(), primes.end(), ostream_iterator<unsigned>(cout, " "));

    return EXIT_SUCCESS;
}










Ada: #!/usr/local/bin/spar
  
pragma annotate( summary, "primes_asc" );
pragma annotate( description, "Generate and show all primes with strictly ascending decimal digits" );
pragma annotate( description, "Translation of Pascal" );
pragma annotate( see_also, "https://rosettacode.org/wiki/Ascending_primes" );
pragma annotate( author, "Ken O. Burtch" );

pragma software_model( nonstandard );
pragma restriction( no_external_commands );

procedure primes_asc is
  maxsize : constant natural := 1000;

  queue : array(1..maxsize) of natural;
  primes: array(1..maxsize) of natural;

  b : natural;
  e : natural;
  n : natural;
  v : natural;

function is_prime(num: integer) return boolean is
    found : boolean;
    num_root : natural;
    k : natural;
  begin
    if num = 2 then
      found;
    elsif (num = 1) or (num mod 2 = 0) then
      found := false;
    else
      num_root := numerics.truncation(numerics.sqrt(num));
      found;
      k := 3;
      while found and (k <= num_root) loop
        if num mod k = 0 then
          found := false;
        else
          k := @ + 2;
        end if;
      end loop;
   end if;
    return found;
  end is_prime;

begin
  b := 1;
  e := 1;
  n := 0;

  for k in 1..9 loop
    queue(e) := k;
    e := e + 1;
  end loop;

  while b < e loop
    v := queue(b);
    b := @ + 1;
    if is_prime(v) then
      n := @ + 1;
      primes(n) := v;
    end if;

    for k in v mod 10 + 1..9 loop
      queue(e) := v * 10 + k;
      e := @ + 1;
    end loop;
  end loop;

  for k in 1..n loop
    put(primes(k), "ZZZZZZZZ9");
    if k mod 8 = 0 then
       new_line;
    end if;
  end loop;
  new_line;
end primes_asc;
Task: Ascending primes




Go: package main

import (
    "fmt"
    "rcu"
    "sort"
)

var ascPrimesSet = make(map[int]bool) 

func generate(first, cand, digits int) {
    if digits == 0 {
        if rcu.IsPrime(cand) {
            ascPrimesSet[cand] = true
        }
        return
    }
    for i := first; i < 10; i++ {
        next := cand*10 + i
        generate(i+1, next, digits-1)
    }
}

func main() {
    for digits := 1; digits < 10; digits++ {
        generate(1, 0, digits)
    }
    le := len(ascPrimesSet)
    ascPrimes := make([]int, le)
    i := 0
    for k := range ascPrimesSet {
        ascPrimes[i] = k
        i++
    }
    sort.Ints(ascPrimes)
    fmt.Println("There are", le, "ascending primes, namely:")
    for i := 0; i < le; i++ {
        fmt.Printf("%8d ", ascPrimes[i])
        if (i+1)%10 == 0 {
            fmt.Println()
        }
    }
}








Ada: #!/usr/local/bin/spar
  
pragma annotate( summary, "primes_asc" );
pragma annotate( description, "Generate and show all primes with strictly ascending decimal digits" );
pragma annotate( description, "Translation of Pascal" );
pragma annotate( see_also, "https://rosettacode.org/wiki/Ascending_primes" );
pragma annotate( author, "Ken O. Burtch" );

pragma software_model( nonstandard );
pragma restriction( no_external_commands );

procedure primes_asc is
  maxsize : constant natural := 1000;

  queue : array(1..maxsize) of natural;
  primes: array(1..maxsize) of natural;

  b : natural;
  e : natural;
  n : natural;
  v : natural;

function is_prime(num: integer) return boolean is
    found : boolean;
    num_root : natural;
    k : natural;
  begin
    if num = 2 then
      found;
    elsif (num = 1) or (num mod 2 = 0) then
      found := false;
    else
      num_root := numerics.truncation(numerics.sqrt(num));
      found;
      k := 3;
      while found and (k <= num_root) loop
        if num mod k = 0 then
          found := false;
        else
          k := @ + 2;
        end if;
      end loop;
   end if;
    return found;
  end is_prime;

begin
  b := 1;
  e := 1;
  n := 0;

  for k in 1..9 loop
    queue(e) := k;
    e := e + 1;
  end loop;

  while b < e loop
    v := queue(b);
    b := @ + 1;
    if is_prime(v) then
      n := @ + 1;
      primes(n) := v;
    end if;

    for k in v mod 10 + 1..9 loop
      queue(e) := v * 10 + k;
      e := @ + 1;
    end loop;
  end loop;

  for k in 1..n loop
    put(primes(k), "ZZZZZZZZ9");
    if k mod 8 = 0 then
       new_line;
    end if;
  end loop;
  new_line;
end primes_asc;
Task: Ascending primes





Java: 

package example.rossetacode.ascendingprimes;

import java.util.Arrays;

public class Program implements Runnable {

    public static void main(String[] args) {
        long t1 = System.nanoTime();
        new Program().run();
        long t2 = System.nanoTime();
        System.out.println(
                "total time consumed = " + (t2 - t1) * 1E-6 + " milliseconds");
    }

    public void run() {

        final int MAX_SIZE = 1000;
        final int[] queue = new int[MAX_SIZE];
        int begin = 0;
        int end = 0;

        for (int k = 1; k <= 9; k++) {
            queue[end++] = k;
        }

        while (begin < end) {
            int n = queue[begin++];
            for (int k = n % 10 + 1; k <= 9; k++) {
                queue[end++] = n * 10 + k;
            }
        }

        
        
        
        System.out.println(Arrays.stream(queue).filter(this::isPrime).boxed().toList());
    }

    private boolean isPrime(int n) {
        if (n == 2) {
            return true;
        }
        if (n == 1 || n % 2 == 0) {
            return false;
        }
        int root = (int) Math.sqrt(n);
        for (int k = 3; k <= root; k += 2) {
            if (n % k == 0) {
                return false;
            }
        }
        return true;
    }
}







Ada: #!/usr/local/bin/spar
  
pragma annotate( summary, "primes_asc" );
pragma annotate( description, "Generate and show all primes with strictly ascending decimal digits" );
pragma annotate( description, "Translation of Pascal" );
pragma annotate( see_also, "https://rosettacode.org/wiki/Ascending_primes" );
pragma annotate( author, "Ken O. Burtch" );

pragma software_model( nonstandard );
pragma restriction( no_external_commands );

procedure primes_asc is
  maxsize : constant natural := 1000;

  queue : array(1..maxsize) of natural;
  primes: array(1..maxsize) of natural;

  b : natural;
  e : natural;
  n : natural;
  v : natural;

function is_prime(num: integer) return boolean is
    found : boolean;
    num_root : natural;
    k : natural;
  begin
    if num = 2 then
      found;
    elsif (num = 1) or (num mod 2 = 0) then
      found := false;
    else
      num_root := numerics.truncation(numerics.sqrt(num));
      found;
      k := 3;
      while found and (k <= num_root) loop
        if num mod k = 0 then
          found := false;
        else
          k := @ + 2;
        end if;
      end loop;
   end if;
    return found;
  end is_prime;

begin
  b := 1;
  e := 1;
  n := 0;

  for k in 1..9 loop
    queue(e) := k;
    e := e + 1;
  end loop;

  while b < e loop
    v := queue(b);
    b := @ + 1;
    if is_prime(v) then
      n := @ + 1;
      primes(n) := v;
    end if;

    for k in v mod 10 + 1..9 loop
      queue(e) := v * 10 + k;
      e := @ + 1;
    end loop;
  end loop;

  for k in 1..n loop
    put(primes(k), "ZZZZZZZZ9");
    if k mod 8 = 0 then
       new_line;
    end if;
  end loop;
  new_line;
end primes_asc;
Task: Ascending primes







Python: from sympy import isprime

def ascending(x=0):
    for y in range(x*10 + (x%10) + 1, x*10 + 10):
        yield from ascending(y)
        yield(y)

print(sorted(x for x in ascending() if isprime(x)))





Ada: #!/usr/local/bin/spar
  
pragma annotate( summary, "primes_asc" );
pragma annotate( description, "Generate and show all primes with strictly ascending decimal digits" );
pragma annotate( description, "Translation of Pascal" );
pragma annotate( see_also, "https://rosettacode.org/wiki/Ascending_primes" );
pragma annotate( author, "Ken O. Burtch" );

pragma software_model( nonstandard );
pragma restriction( no_external_commands );

procedure primes_asc is
  maxsize : constant natural := 1000;

  queue : array(1..maxsize) of natural;
  primes: array(1..maxsize) of natural;

  b : natural;
  e : natural;
  n : natural;
  v : natural;

function is_prime(num: integer) return boolean is
    found : boolean;
    num_root : natural;
    k : natural;
  begin
    if num = 2 then
      found;
    elsif (num = 1) or (num mod 2 = 0) then
      found := false;
    else
      num_root := numerics.truncation(numerics.sqrt(num));
      found;
      k := 3;
      while found and (k <= num_root) loop
        if num mod k = 0 then
          found := false;
        else
          k := @ + 2;
        end if;
      end loop;
   end if;
    return found;
  end is_prime;

begin
  b := 1;
  e := 1;
  n := 0;

  for k in 1..9 loop
    queue(e) := k;
    e := e + 1;
  end loop;

  while b < e loop
    v := queue(b);
    b := @ + 1;
    if is_prime(v) then
      n := @ + 1;
      primes(n) := v;
    end if;

    for k in v mod 10 + 1..9 loop
      queue(e) := v * 10 + k;
      e := @ + 1;
    end loop;
  end loop;

  for k in 1..n loop
    put(primes(k), "ZZZZZZZZ9");
    if k mod 8 = 0 then
       new_line;
    end if;
  end loop;
  new_line;
end primes_asc;
Task: Ascending primes








VB: Module AscendingPrimes

    Function isPrime(n As Integer)
        n = Math.Abs(n)
        If n = 2 Then
            Return True
        End If
        If n = 1 Or n Mod 2 = 0 Then
            Return False
        End If
        Dim root As Integer = Math.Sqrt(n)
        For k = 3 To root Step 2
            If n Mod k = 0 Then
                Return False
            End If
        Next
        Return True
    End Function


    Sub Main()

        Dim queue As Queue(Of Integer) = New Queue(Of Integer)
        Dim primes As List(Of Integer) = New List(Of Integer)

        For k = 1 To 9
            queue.Enqueue(k)
        Next

        While queue.Count > 0
            Dim n As Integer = queue.Dequeue()
            If (isPrime(n)) Then
                primes.Add(n)
            End If
            For k = n Mod 10 + 1 To 9
                queue.Enqueue(n * 10 + k)
            Next
        End While

        For Each p As Integer In primes
            Console.Write(p)
            Console.Write(" ")
        Next
        Console.WriteLine()

    End Sub

End Module




Ada: #!/usr/local/bin/spar
  
pragma annotate( summary, "primes_asc" );
pragma annotate( description, "Generate and show all primes with strictly ascending decimal digits" );
pragma annotate( description, "Translation of Pascal" );
pragma annotate( see_also, "https://rosettacode.org/wiki/Ascending_primes" );
pragma annotate( author, "Ken O. Burtch" );

pragma software_model( nonstandard );
pragma restriction( no_external_commands );

procedure primes_asc is
  maxsize : constant natural := 1000;

  queue : array(1..maxsize) of natural;
  primes: array(1..maxsize) of natural;

  b : natural;
  e : natural;
  n : natural;
  v : natural;

function is_prime(num: integer) return boolean is
    found : boolean;
    num_root : natural;
    k : natural;
  begin
    if num = 2 then
      found;
    elsif (num = 1) or (num mod 2 = 0) then
      found := false;
    else
      num_root := numerics.truncation(numerics.sqrt(num));
      found;
      k := 3;
      while found and (k <= num_root) loop
        if num mod k = 0 then
          found := false;
        else
          k := @ + 2;
        end if;
      end loop;
   end if;
    return found;
  end is_prime;

begin
  b := 1;
  e := 1;
  n := 0;

  for k in 1..9 loop
    queue(e) := k;
    e := e + 1;
  end loop;

  while b < e loop
    v := queue(b);
    b := @ + 1;
    if is_prime(v) then
      n := @ + 1;
      primes(n) := v;
    end if;

    for k in v mod 10 + 1..9 loop
      queue(e) := v * 10 + k;
      e := @ + 1;
    end loop;
  end loop;

  for k in 1..n loop
    put(primes(k), "ZZZZZZZZ9");
    if k mod 8 = 0 then
       new_line;
    end if;
  end loop;
  new_line;
end primes_asc;
Task: Ascending primes

C: 

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

#if UINT_MAX < 123456789
#error "we need at least 9 decimal digits (32-bit integers)"
#endif


#define MAXSIZE 1000

unsigned queue[MAXSIZE];
unsigned primes[MAXSIZE];

unsigned begin = 0;
unsigned end = 0;
unsigned n = 0;


bool isPrime(unsigned n)
{
    if (n == 2)
    {
        return true;
    }
    if (n == 1 || n % 2 == 0)
    {
        return false;
    }
    unsigned root = sqrt(n);
    for (unsigned k = 3; k <= root; k += 2)
    {
        if (n % k == 0)
        {
            return false;
        }
    }
    return true;
}


int main(int argc, char argv[])
{
    for (int k = 1; k <= 9; k++)
    {
        queue[end++] = k;
    }

    while (begin < end)
    {
        int value = queue[begin++];
        if (isPrime(value))
        {
            primes[n++] = value;
        }
        for (int k = value % 10 + 1; k <= 9; k++)
        {
            queue[end++] = value * 10 + k;
        }
    }

    for (int k = 0; k < n; k++)
    {
        printf("%u ", primes[k]);
    }
 
    return EXIT_SUCCESS;
}
Task: Ascending primes



C#: using System;
using System.Collections.Generic;

namespace ascendingprimes
{
    class Program
    {
        static bool isPrime(uint n)
        {
            if (n == 2)
                return true;
            if (n == 1 || n % 2 = 0)
                return false;
            uint root = (uint)Math.Sqrt(n);
            for (uint k = 3; k <= root; k += 2)
                if (n % k == 0)
                    return false;
            return true;
        }
        static void Main(string[] args)
        {
            var queue = new Queue<uint>();
            var primes = new List<uint>();

            for (uint k = 1; k <= 9; k++)
                queue.Enqueue(k);
            while(queue.Count > 0)
            {
                uint n = queue.Dequeue();
                if (isPrime(n))
                    primes.Add(n);
                for (uint k = n % 10 + 1; k <= 9; k++)
                    queue.Enqueue(n * 10 + k);
            }

            foreach (uint p in primes)
            {
                Console.Write(p);
                Console.Write(" ");
            }
            Console.WriteLine();
        }
    }
}
Task: Ascending primes


C++: 

#include <cmath>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;


queue<unsigned> suspected;
vector<unsigned> primes;


bool isPrime(unsigned n)
{
    if (n == 2)
    {
        return true;
    }
    if (n == 1 || n % 2 == 0)
    {
        return false;
    }
    unsigned root = sqrt(n);
    for (unsigned k = 3; k <= root; k += 2)
    {
        if (n % k == 0)
        {
            return false;
        }
    }
    return true;
}


int main(int argc, char argv[])
{
    for (unsigned k = 1; k <= 9; k++)
    {
        suspected.push(k);
    }

    while (!suspected.empty())
    {
        int n = suspected.front();
        suspected.pop();

        if (isPrime(n))
        {
            primes.push_back(n);
        }

        
        
        for (unsigned k = n % 10 + 1; k <= 9; k++)
        {
            suspected.push(n * 10 + k);
        }
    }

    copy(primes.begin(), primes.end(), ostream_iterator<unsigned>(cout, " "));

    return EXIT_SUCCESS;
}
Task: Ascending primes





Java: 

package example.rossetacode.ascendingprimes;

import java.util.Arrays;

public class Program implements Runnable {

    public static void main(String[] args) {
        long t1 = System.nanoTime();
        new Program().run();
        long t2 = System.nanoTime();
        System.out.println(
                "total time consumed = " + (t2 - t1) * 1E-6 + " milliseconds");
    }

    public void run() {

        final int MAX_SIZE = 1000;
        final int[] queue = new int[MAX_SIZE];
        int begin = 0;
        int end = 0;

        for (int k = 1; k <= 9; k++) {
            queue[end++] = k;
        }

        while (begin < end) {
            int n = queue[begin++];
            for (int k = n % 10 + 1; k <= 9; k++) {
                queue[end++] = n * 10 + k;
            }
        }

        
        
        
        System.out.println(Arrays.stream(queue).filter(this::isPrime).boxed().toList());
    }

    private boolean isPrime(int n) {
        if (n == 2) {
            return true;
        }
        if (n == 1 || n % 2 == 0) {
            return false;
        }
        int root = (int) Math.sqrt(n);
        for (int k = 3; k <= root; k += 2) {
            if (n % k == 0) {
                return false;
            }
        }
        return true;
    }
}
Task: Ascending primes







Python: from sympy import isprime

def ascending(x=0):
    for y in range(x*10 + (x%10) + 1, x*10 + 10):
        yield from ascending(y)
        yield(y)

print(sorted(x for x in ascending() if isprime(x)))