WeixiangYan/CodeTransOcean huggingface dataset:
Task: Monte Carlo methods






PHP: <?
$loop = 1000000; # loop to 1,000,000
$count = 0;
for ($i=0; $i<$loop; $i++) {
  $x = rand() / getrandmax();
  $y = rand() / getrandmax();
  if(($x*$x) + ($y*$y)<=1) $count++;
}
echo "loop=".number_format($loop).", count=".number_format($count).", pi=".($count/$loop*4);
?>






Ada: with Ada.Text_IO;                use Ada.Text_IO;
with Ada.Numerics.Float_Random;  use Ada.Numerics.Float_Random;

procedure Test_Monte_Carlo is
   Dice : Generator;
   
   function Pi (Throws : Positive) return Float is
      Inside : Natural := 0;
   begin
      for Throw in 1..Throws loop
         if Random (Dice) ** 2 + Random (Dice) ** 2 <= 1.0 then
            Inside := Inside + 1;
         end if;
      end loop;
      return 4.0 * Float (Inside) / Float (Throws);
   end Pi;
begin
   Put_Line ("     10_000:" & Float'Image (Pi (     10_000)));
   Put_Line ("    100_000:" & Float'Image (Pi (    100_000)));
   Put_Line ("  1_000_000:" & Float'Image (Pi (  1_000_000)));
   Put_Line (" 10_000_000:" & Float'Image (Pi ( 10_000_000)));
   Put_Line ("100_000_000:" & Float'Image (Pi (100_000_000)));
end Test_Monte_Carlo;
Task: Metered concurrency



C#: using System;
using System.Threading;
using System.Threading.Tasks;

namespace RosettaCode
{
  internal sealed class Program
  {
    private static void Worker(object arg, int id)
    {
      var sem = arg as SemaphoreSlim;
      sem.Wait();
      Console.WriteLine("Thread {0} has a semaphore & is now working.", id);
      Thread.Sleep(2*1000);
      Console.WriteLine("#{0} done.", id);
      sem.Release();
    }

    private static void Main()
    {
      var semaphore = new SemaphoreSlim(Environment.ProcessorCount*2, int.MaxValue);

      Console.WriteLine("You have {0} processors availiabe", Environment.ProcessorCount);
      Console.WriteLine("This program will use {0} semaphores.\n", semaphore.CurrentCount);

      Parallel.For(0, Environment.ProcessorCount*3, y => Worker(semaphore, y));
    }
  }
}









Ada: package Semaphores is
   protected type Counting_Semaphore(Max : Positive) is
      entry Acquire;
      procedure Release;
      function Count return Natural;
   private
      Lock_Count : Natural := 0;
   end Counting_Semaphore;
end Semaphores;
Task: Metered concurrency

C: #include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

sem_t sem;
int count = 3;


#define getcount() count
void acquire()
{
	sem_wait(&sem);
	count--;
}

void release()
{
	count++;
	sem_post(&sem);
}

void* work(void * id)
{
	int i = 10;
	while (i--) {
		acquire();
		printf("#%d acquired sema at %d\n", *(int*)id, getcount());
		usleep(rand() % 4000000); 
		release();
		usleep(0);  
	}
	return 0;
}

int main()
{
	pthread_t th[4];
	int i, ids[] = {1, 2, 3, 4};

	sem_init(&sem, 0, count);

	for (i = 4; i--;) pthread_create(th + i, 0, work, ids + i);
	for (i = 4; i--;) pthread_join(th[i], 0);
	printf("all workers done\n");

	return sem_destroy(&sem);
}











Ada: package Semaphores is
   protected type Counting_Semaphore(Max : Positive) is
      entry Acquire;
      procedure Release;
      function Count return Natural;
   private
      Lock_Count : Natural := 0;
   end Counting_Semaphore;
end Semaphores;
Task: Metered concurrency


C++: #include <chrono>
#include <iostream>
#include <format>
#include <semaphore>
#include <thread>
 
using namespace std::literals;

void Worker(std::counting_semaphore<>& semaphore, int id)
{	
    semaphore.acquire();
    std::cout << std::format("Thread {} has a semaphore & is now working.\n", id); 
    std::this_thread::sleep_for(2s);
    std::cout << std::format("Thread {} done.\n", id);
    semaphore.release();
}
 
int main()
{
    const auto numOfThreads = static_cast<int>( std::thread::hardware_concurrency() );
    std::counting_semaphore<> semaphore{numOfThreads / 2};

    std::vector<std::jthread> tasks;
    for (int id = 0; id < numOfThreads; ++id)
	tasks.emplace_back(Worker, std::ref(semaphore), id);

    return 0;
}










Ada: package Semaphores is
   protected type Counting_Semaphore(Max : Positive) is
      entry Acquire;
      procedure Release;
      function Count return Natural;
   private
      Lock_Count : Natural := 0;
   end Counting_Semaphore;
end Semaphores;
Task: Metered concurrency




Go: package main

import (
    "log"
    "os"
    "sync"
    "time"
)


type sem chan struct{}

func (s sem) acquire()   { s <- struct{}{} }
func (s sem) release()   { <-s }
func (s sem) count() int { return cap(s) - len(s) }


var fmt = log.New(os.Stdout, "", 0)


const nRooms = 10
const nStudents = 20

func main() {
    rooms := make(sem, nRooms)
    
    
    var studied sync.WaitGroup
    studied.Add(nStudents)
    
    for i := 0; i < nStudents; i++ {
        go student(rooms, &studied)
    }
    studied.Wait()
}

func student(rooms sem, studied *sync.WaitGroup) {
    rooms.acquire()
    
    fmt.Printf("Room entered.  Count is %d.  Studying...\n",
        rooms.count())
    time.Sleep(2 * time.Second) 
    rooms.release()
    studied.Done() 
}








Ada: package Semaphores is
   protected type Counting_Semaphore(Max : Positive) is
      entry Acquire;
      procedure Release;
      function Count return Natural;
   private
      Lock_Count : Natural := 0;
   end Counting_Semaphore;
end Semaphores;
Task: Metered concurrency





Java: public class CountingSemaphore{
   private int lockCount = 0;
   private int maxCount;

   CountingSemaphore(int Max){
      maxCount = Max;
   }
  
   public synchronized void acquire() throws InterruptedException{
      while( lockCount >= maxCount){
         wait();
      }
      lockCount++;
   }
   public synchronized void release(){
      if (lockCount > 0)
      {
         lockCount--;
         notifyAll();
      }
   }
   public synchronized int getCount(){
      return lockCount;
   }
}

public class Worker extends Thread{
   private CountingSemaphore lock;
   private int id;

   Worker(CountingSemaphore coordinator, int num){
      lock = coordinator;
      id = num;
   }
   Worker(){
   }
   public void run(){
      try{
         lock.acquire();
         System.out.println("Worker " + id + " has acquired the lock.");
         sleep(2000);
      }
      catch (InterruptedException e){
      }
      finally{
         lock.release();
      }
   }
   public static void main(String[] args){
      CountingSemaphore lock = new CountingSemaphore(3);
      Worker crew[];
      crew = new Worker[5];
      for (int i = 0; i < 5; i++){
         crew[i] = new Worker(lock, i);
         crew[i].start();
      }

   }
}







Ada: package Semaphores is
   protected type Counting_Semaphore(Max : Positive) is
      entry Acquire;
      procedure Release;
      function Count return Natural;
   private
      Lock_Count : Natural := 0;
   end Counting_Semaphore;
end Semaphores;
Task: Metered concurrency







Python: import time
import threading


sem = threading.Semaphore(4)

workers = []
running = 1


def worker():
    me = threading.currentThread()
    while 1:
        sem.acquire()
        try:
            if not running:
                break
            print '%s acquired semaphore' % me.getName()
            time.sleep(2.0)
        finally:
            sem.release()
        time.sleep(0.01) 


for i in range(10):
    t = threading.Thread(name=str(i), target=worker)
    workers.append(t)
    t.start()


try:
    while 1:
        time.sleep(0.1)
except KeyboardInterrupt:
    running = 0
    for t in workers:
        t.join()





Ada: package Semaphores is
   protected type Counting_Semaphore(Max : Positive) is
      entry Acquire;
      procedure Release;
      function Count return Natural;
   private
      Lock_Count : Natural := 0;
   end Counting_Semaphore;
end Semaphores;
Task: Metered concurrency








VB: Dim sem As New Semaphore(5, 5) 
sem.WaitOne() 
Dim oldCount = sem.Release() 





Ada: package Semaphores is
   protected type Counting_Semaphore(Max : Positive) is
      entry Acquire;
      procedure Release;
      function Count return Natural;
   private
      Lock_Count : Natural := 0;
   end Counting_Semaphore;
end Semaphores;
Task: Metered concurrency

C: #include <semaphore.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

sem_t sem;
int count = 3;


#define getcount() count
void acquire()
{
	sem_wait(&sem);
	count--;
}

void release()
{
	count++;
	sem_post(&sem);
}

void* work(void * id)
{
	int i = 10;
	while (i--) {
		acquire();
		printf("#%d acquired sema at %d\n", *(int*)id, getcount());
		usleep(rand() % 4000000); 
		release();
		usleep(0);  
	}
	return 0;
}

int main()
{
	pthread_t th[4];
	int i, ids[] = {1, 2, 3, 4};

	sem_init(&sem, 0, count);

	for (i = 4; i--;) pthread_create(th + i, 0, work, ids + i);
	for (i = 4; i--;) pthread_join(th[i], 0);
	printf("all workers done\n");

	return sem_destroy(&sem);
}
Task: Metered concurrency



C#: using System;
using System.Threading;
using System.Threading.Tasks;

namespace RosettaCode
{
  internal sealed class Program
  {
    private static void Worker(object arg, int id)
    {
      var sem = arg as SemaphoreSlim;
      sem.Wait();
      Console.WriteLine("Thread {0} has a semaphore & is now working.", id);
      Thread.Sleep(2*1000);
      Console.WriteLine("#{0} done.", id);
      sem.Release();
    }

    private static void Main()
    {
      var semaphore = new SemaphoreSlim(Environment.ProcessorCount*2, int.MaxValue);

      Console.WriteLine("You have {0} processors availiabe", Environment.ProcessorCount);
      Console.WriteLine("This program will use {0} semaphores.\n", semaphore.CurrentCount);

      Parallel.For(0, Environment.ProcessorCount*3, y => Worker(semaphore, y));
    }
  }
}
Task: Metered concurrency


C++: #include <chrono>
#include <iostream>
#include <format>
#include <semaphore>
#include <thread>
 
using namespace std::literals;

void Worker(std::counting_semaphore<>& semaphore, int id)
{	
    semaphore.acquire();
    std::cout << std::format("Thread {} has a semaphore & is now working.\n", id); 
    std::this_thread::sleep_for(2s);
    std::cout << std::format("Thread {} done.\n", id);
    semaphore.release();
}
 
int main()
{
    const auto numOfThreads = static_cast<int>( std::thread::hardware_concurrency() );
    std::counting_semaphore<> semaphore{numOfThreads / 2};

    std::vector<std::jthread> tasks;
    for (int id = 0; id < numOfThreads; ++id)
	tasks.emplace_back(Worker, std::ref(semaphore), id);

    return 0;
}
Task: Metered concurrency





Java: public class CountingSemaphore{
   private int lockCount = 0;
   private int maxCount;

   CountingSemaphore(int Max){
      maxCount = Max;
   }
  
   public synchronized void acquire() throws InterruptedException{
      while( lockCount >= maxCount){
         wait();
      }
      lockCount++;
   }
   public synchronized void release(){
      if (lockCount > 0)
      {
         lockCount--;
         notifyAll();
      }
   }
   public synchronized int getCount(){
      return lockCount;
   }
}

public class Worker extends Thread{
   private CountingSemaphore lock;
   private int id;

   Worker(CountingSemaphore coordinator, int num){
      lock = coordinator;
      id = num;
   }
   Worker(){
   }
   public void run(){
      try{
         lock.acquire();
         System.out.println("Worker " + id + " has acquired the lock.");
         sleep(2000);
      }
      catch (InterruptedException e){
      }
      finally{
         lock.release();
      }
   }
   public static void main(String[] args){
      CountingSemaphore lock = new CountingSemaphore(3);
      Worker crew[];
      crew = new Worker[5];
      for (int i = 0; i < 5; i++){
         crew[i] = new Worker(lock, i);
         crew[i].start();
      }

   }
}
Task: Metered concurrency







Python: import time
import threading


sem = threading.Semaphore(4)

workers = []
running = 1


def worker():
    me = threading.currentThread()
    while 1:
        sem.acquire()
        try:
            if not running:
                break
            print '%s acquired semaphore' % me.getName()
            time.sleep(2.0)
        finally:
            sem.release()
        time.sleep(0.01) 


for i in range(10):
    t = threading.Thread(name=str(i), target=worker)
    workers.append(t)
    t.start()


try:
    while 1:
        time.sleep(0.1)
except KeyboardInterrupt:
    running = 0
    for t in workers:
        t.join()
Task: Metered concurrency








VB: Dim sem As New Semaphore(5, 5) 
sem.WaitOne() 
Dim oldCount = sem.Release()
Task: Metered concurrency




Go: package main

import (
    "log"
    "os"
    "sync"
    "time"
)


type sem chan struct{}

func (s sem) acquire()   { s <- struct{}{} }
func (s sem) release()   { <-s }
func (s sem) count() int { return cap(s) - len(s) }


var fmt = log.New(os.Stdout, "", 0)


const nRooms = 10
const nStudents = 20

func main() {
    rooms := make(sem, nRooms)
    
    
    var studied sync.WaitGroup
    studied.Add(nStudents)
    
    for i := 0; i < nStudents; i++ {
        go student(rooms, &studied)
    }
    studied.Wait()
}

func student(rooms sem, studied *sync.WaitGroup) {
    rooms.acquire()
    
    fmt.Printf("Room entered.  Count is %d.  Studying...\n",
        rooms.count())
    time.Sleep(2 * time.Second) 
    rooms.release()
    studied.Done() 
}
Task: N'th



C#: using System;
using System.Linq;

class Program
{
    private static string Ordinalize(int i)
    {
        i = Math.Abs(i);

        if (new[] {11, 12, 13}.Contains(i%100))
            return i + "th";

        switch (i%10)
        {
            case 1:
                return i + "st";
            case 2:
                return i + "nd";
            case 3:
                return i + "rd";
            default:
                return i + "th";
        }
    }

    static void Main()
    {
        Console.WriteLine(string.Join(" ", Enumerable.Range(0, 26).Select(Ordinalize)));
        Console.WriteLine(string.Join(" ", Enumerable.Range(250, 16).Select(Ordinalize)));
        Console.WriteLine(string.Join(" ", Enumerable.Range(1000, 26).Select(Ordinalize)));
    }
}









Ada: with Ada.Text_IO;

procedure Nth is
   
   function Suffix(N: Natural) return String is
   begin
      if    N mod 10 = 1 and then N mod 100 /= 11 then return "st";
      elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
      elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
      else return "th";
      end if;
   end Suffix;
      
   procedure Print_Images(From, To: Natural) is
   begin
      for I in From .. To loop
	 Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Images;
   
begin
   Print_Images(   0,   25);
   Print_Images( 250,  265);
   Print_Images(1000, 1025);
end Nth;
Task: N'th

C: #include <stdio.h>
 
char* addSuffix(int num, char* buf, size_t len)
{
    char *suffixes[4] = { "th", "st", "nd", "rd" };
    int i;

    switch (num % 10)
    {
        case 1 : i = (num % 100 == 11) ? 0 : 1;
	         break;
        case 2 : i = (num % 100 == 12) ? 0 : 2;
                 break;
        case 3 : i = (num % 100 == 13) ? 0 : 3;
                 break;
        default: i = 0;
    };
 
    snprintf(buf, len, "%d%s", num, suffixes[i]);
    return buf;
}

int main(void)
{
    int i;

    printf("Set [0,25]:\n");
    for (i = 0; i < 26; i++)
    {
        char s[5];
        printf("%s ", addSuffix(i, s, 5));
    }
    putchar('\n'); 

    printf("Set [250,265]:\n");
    for (i = 250; i < 266; i++)
    {
        char s[6];
        printf("%s ", addSuffix(i, s, 6));
    }
    putchar('\n'); 

    printf("Set [1000,1025]:\n");
    for (i = 1000; i < 1026; i++)
    {
        char s[7];
        printf("%s ", addSuffix(i, s, 7));
    }
    putchar('\n');

    return 0;
}











Ada: with Ada.Text_IO;

procedure Nth is
   
   function Suffix(N: Natural) return String is
   begin
      if    N mod 10 = 1 and then N mod 100 /= 11 then return "st";
      elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
      elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
      else return "th";
      end if;
   end Suffix;
      
   procedure Print_Images(From, To: Natural) is
   begin
      for I in From .. To loop
	 Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Images;
   
begin
   Print_Images(   0,   25);
   Print_Images( 250,  265);
   Print_Images(1000, 1025);
end Nth;
Task: N'th


C++: #include <string>
#include <iostream>

using namespace std;

string Suffix(int num)
{
    switch (num % 10)
    {
        case 1 : if(num % 100 != 11) return "st";
           break;
        case 2 : if(num % 100 != 12) return "nd";
           break;
        case 3 : if(num % 100 != 13) return "rd";
    }

    return "th";
}

int main()
{
    cout << "Set [0,25]:" << endl;
    for (int i = 0; i < 26; i++)
        cout << i << Suffix(i) << " ";
    
    cout << endl;

    cout << "Set [250,265]:" << endl;
    for (int i = 250; i < 266; i++)
        cout << i << Suffix(i) << " ";
   
    cout << endl;

    cout << "Set [1000,1025]:" << endl;
    for (int i = 1000; i < 1026; i++)
        cout << i << Suffix(i) << " ";
   
    cout << endl;

    return 0;
}










Ada: with Ada.Text_IO;

procedure Nth is
   
   function Suffix(N: Natural) return String is
   begin
      if    N mod 10 = 1 and then N mod 100 /= 11 then return "st";
      elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
      elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
      else return "th";
      end if;
   end Suffix;
      
   procedure Print_Images(From, To: Natural) is
   begin
      for I in From .. To loop
	 Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Images;
   
begin
   Print_Images(   0,   25);
   Print_Images( 250,  265);
   Print_Images(1000, 1025);
end Nth;
Task: N'th




Go: package main

import "fmt"

func ord(n int) string {
    s := "th"
    switch c := n % 10; c {
    case 1, 2, 3:
        if n%100/10 == 1 {
            break
        }
        switch c {
        case 1:
            s = "st"
        case 2:
            s = "nd"
        case 3:
            s = "rd"
        }
    }
    return fmt.Sprintf("%d%s", n, s)
}

func main() {
    for n := 0; n <= 25; n++ {
        fmt.Printf("%s ", ord(n))
    }
    fmt.Println()
    for n := 250; n <= 265; n++ {
        fmt.Printf("%s ", ord(n))
    }
    fmt.Println()
    for n := 1000; n <= 1025; n++ {
        fmt.Printf("%s ", ord(n))
    }
    fmt.Println()
}








Ada: with Ada.Text_IO;

procedure Nth is
   
   function Suffix(N: Natural) return String is
   begin
      if    N mod 10 = 1 and then N mod 100 /= 11 then return "st";
      elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
      elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
      else return "th";
      end if;
   end Suffix;
      
   procedure Print_Images(From, To: Natural) is
   begin
      for I in From .. To loop
	 Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Images;
   
begin
   Print_Images(   0,   25);
   Print_Images( 250,  265);
   Print_Images(1000, 1025);
end Nth;
Task: N'th





Java: public class Nth {
	public static String ordinalAbbrev(int n){
		String ans = "th"; 
		if(n % 100 / 10 == 1) return ans; 
		switch(n % 10){
			case 1: ans = "st"; break;
			case 2: ans = "nd"; break;
			case 3: ans = "rd"; break;
		}
		return ans;
	}
	
	public static void main(String[] args){
		for(int i = 0; i <= 25;i++){
			System.out.print(i + ordinalAbbrev(i) + " ");
		}
		System.out.println();
		for(int i = 250; i <= 265;i++){
			System.out.print(i + ordinalAbbrev(i) + " ");
		}
		System.out.println();
		for(int i = 1000; i <= 1025;i++){
			System.out.print(i + ordinalAbbrev(i) + " ");
		}
	}
}







Ada: with Ada.Text_IO;

procedure Nth is
   
   function Suffix(N: Natural) return String is
   begin
      if    N mod 10 = 1 and then N mod 100 /= 11 then return "st";
      elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
      elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
      else return "th";
      end if;
   end Suffix;
      
   procedure Print_Images(From, To: Natural) is
   begin
      for I in From .. To loop
	 Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Images;
   
begin
   Print_Images(   0,   25);
   Print_Images( 250,  265);
   Print_Images(1000, 1025);
end Nth;
Task: N'th







Python: _suffix = ['th', 'st', 'nd', 'rd', 'th', 'th', 'th', 'th', 'th', 'th']

def nth(n):
    return "%i'%s" % (n, _suffix[n%10] if n % 100 <= 10 or n % 100 > 20 else 'th')

if __name__ == '__main__':
    for j in range(0,1001, 250):
        print(' '.join(nth(i) for i in list(range(j, j+25))))





Ada: with Ada.Text_IO;

procedure Nth is
   
   function Suffix(N: Natural) return String is
   begin
      if    N mod 10 = 1 and then N mod 100 /= 11 then return "st";
      elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
      elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
      else return "th";
      end if;
   end Suffix;
      
   procedure Print_Images(From, To: Natural) is
   begin
      for I in From .. To loop
	 Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Images;
   
begin
   Print_Images(   0,   25);
   Print_Images( 250,  265);
   Print_Images(1000, 1025);
end Nth;
Task: N'th








VB: Private Function ordinals() As Variant
    ordinals = [{"th","st","nd","rd"}]
End Function

Private Function Nth(n As Variant, Optional apostrophe As Boolean = False) As String
    Dim mod10 As Integer: mod10 = n Mod 10 + 1
    If mod10 > 4 Or n Mod 100 = mod10 + 9 Then mod10 = 1
    Nth = CStr(n) & String$(Val(-apostrophe), "
End Function

Public Sub main()
    Ranges = [{0,25;250,265;1000,1025}]
    For i = 1 To UBound(Ranges)
        For j = Ranges(i, 1) To Ranges(i, 2)
            If j Mod 10 = 0 Then Debug.Print
            Debug.Print Format(Nth(j, i = 2), "@@@@@@@");
        Next j
        Debug.Print
    Next i
End Sub




Ada: with Ada.Text_IO;

procedure Nth is
   
   function Suffix(N: Natural) return String is
   begin
      if    N mod 10 = 1 and then N mod 100 /= 11 then return "st";
      elsif N mod 10 = 2 and then N mod 100 /= 12 then return "nd";
      elsif N mod 10 = 3 and then N mod 100 /= 13 then return "rd";
      else return "th";
      end if;
   end Suffix;
      
   procedure Print_Images(From, To: Natural) is
   begin
      for I in From .. To loop
	 Ada.Text_IO.Put(Natural'Image(I) & Suffix(I));
      end loop;
      Ada.Text_IO.New_Line;
   end Print_Images;
   
begin
   Print_Images(   0,   25);
   Print_Images( 250,  265);
   Print_Images(1000, 1025);
end Nth;
Task: N'th

C: #include <stdio.h>
 
char* addSuffix(int num, char* buf, size_t len)
{
    char *suffixes[4] = { "th", "st", "nd", "rd" };
    int i;

    switch (num % 10)
    {
        case 1 : i = (num % 100 == 11) ? 0 : 1;
	         break;
        case 2 : i = (num % 100 == 12) ? 0 : 2;
                 break;
        case 3 : i = (num % 100 == 13) ? 0 : 3;
                 break;
        default: i = 0;
    };
 
    snprintf(buf, len, "%d%s", num, suffixes[i]);
    return buf;
}

int main(void)
{
    int i;

    printf("Set [0,25]:\n");
    for (i = 0; i < 26; i++)
    {
        char s[5];
        printf("%s ", addSuffix(i, s, 5));
    }
    putchar('\n'); 

    printf("Set [250,265]:\n");
    for (i = 250; i < 266; i++)
    {
        char s[6];
        printf("%s ", addSuffix(i, s, 6));
    }
    putchar('\n'); 

    printf("Set [1000,1025]:\n");
    for (i = 1000; i < 1026; i++)
    {
        char s[7];
        printf("%s ", addSuffix(i, s, 7));
    }
    putchar('\n');

    return 0;
}