WeixiangYan/CodeTransOcean huggingface dataset:
Task: Factors of an integer

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

typedef struct {
    int *list;
    short count; 
} Factors;

void xferFactors( Factors *fctrs, int *flist, int flix ) 
{
    int ix, ij;
    int newSize = fctrs->count + flix;
    if (newSize > flix)  {
        fctrs->list = realloc( fctrs->list, newSize * sizeof(int));
    }
    else {
        fctrs->list = malloc(  newSize * sizeof(int));
    }
    for (ij=0,ix=fctrs->count; ix<newSize; ij++,ix++) {
        fctrs->list[ix] = flist[ij];
    }
    fctrs->count = newSize;
}

Factors *factor( int num, Factors *fctrs)
{
    int flist[301], flix;
    int dvsr;
    flix = 0;
    fctrs->count = 0;
    free(fctrs->list);
    fctrs->list = NULL;
    for (dvsr=1; dvsr*dvsr < num; dvsr++) {
        if (num % dvsr != 0) continue;
        if ( flix == 300) {
            xferFactors( fctrs, flist, flix );
            flix = 0;
        }
        flist[flix++] = dvsr;
        flist[flix++] = num/dvsr;
    }
    if (dvsr*dvsr == num) 
        flist[flix++] = dvsr;
    if (flix > 0)
        xferFactors( fctrs, flist, flix );

    return fctrs;
}
        
int main(int argc, char*argv[])
{
    int nums2factor[] = { 2059, 223092870, 3135, 45 };
    Factors ftors = { NULL, 0};
    char sep;
    int i,j;

    for (i=0; i<4; i++) {
        factor( nums2factor[i], &ftors );
        printf("\nfactors of %d are:\n  ", nums2factor[i]);
        sep = ' ';
        for (j=0; j<ftors.count; j++) {
            printf("%c %d", sep, ftors.list[j]);
            sep = ',';
        }
        printf("\n");
    }
    return 0;
}










Fortran: program Factors
  implicit none
  integer :: i, number
  
  write(*,*) "Enter a number between 1 and 2147483647"
  read*, number

  do i = 1, int(sqrt(real(number))) - 1
    if (mod(number, i) == 0) write (*,*) i, number/i
  end do
  
  
  i = int(sqrt(real(number))) 
  if (i*i == number) then
     write (*,*) i
  else if (mod(number, i) == 0) then
     write (*,*) i, number/i
  end if
    
end program
Task: Factors of an integer





Java: public static TreeSet<Long> factors(long n)
{
 TreeSet<Long> factors = new TreeSet<Long>();
 factors.add(n);
 factors.add(1L);
 for(long test = n - 1; test >= Math.sqrt(n); test--)
  if(n % test == 0)
  {
   factors.add(test);
   factors.add(n / test);
  }
 return factors;
}






Fortran: program Factors
  implicit none
  integer :: i, number
  
  write(*,*) "Enter a number between 1 and 2147483647"
  read*, number

  do i = 1, int(sqrt(real(number))) - 1
    if (mod(number, i) == 0) write (*,*) i, number/i
  end do
  
  
  i = int(sqrt(real(number))) 
  if (i*i == number) then
     write (*,*) i
  else if (mod(number, i) == 0) then
     write (*,*) i, number/i
  end if
    
end program
Task: Factors of an integer







Python: >>> def factors(n):
      return [i for i in range(1, n + 1) if not n%i]




Fortran: program Factors
  implicit none
  integer :: i, number
  
  write(*,*) "Enter a number between 1 and 2147483647"
  read*, number

  do i = 1, int(sqrt(real(number))) - 1
    if (mod(number, i) == 0) write (*,*) i, number/i
  end do
  
  
  i = int(sqrt(real(number))) 
  if (i*i == number) then
     write (*,*) i
  else if (mod(number, i) == 0) then
     write (*,*) i, number/i
  end if
    
end program
Task: Factors of an integer








VB: Function factors(num As UInt64) As UInt64()
  
  Dim result() As UInt64
  Dim iFactor As UInt64 = 1
  While iFactor <= num/2 
    If num Mod iFactor = 0 Then
      result.Append(iFactor)
    End If
    iFactor = iFactor + 1
  Wend
  result.Append(num) 
  Return result
End Function



Fortran: program Factors
  implicit none
  integer :: i, number
  
  write(*,*) "Enter a number between 1 and 2147483647"
  read*, number

  do i = 1, int(sqrt(real(number))) - 1
    if (mod(number, i) == 0) write (*,*) i, number/i
  end do
  
  
  i = int(sqrt(real(number))) 
  if (i*i == number) then
     write (*,*) i
  else if (mod(number, i) == 0) then
     write (*,*) i, number/i
  end if
    
end program
Task: Factors of an integer






PHP: function GetFactors($n){
   $factors = array(1, $n);
   for($i = 2; $i * $i <= $n; $i++){
      if($n % $i == 0){
         $factors[] = $i;
         if($i * $i != $n)
            $factors[] = $n/$i;
      }
   }
   sort($factors);
   return $factors;
}





Fortran: program Factors
  implicit none
  integer :: i, number
  
  write(*,*) "Enter a number between 1 and 2147483647"
  read*, number

  do i = 1, int(sqrt(real(number))) - 1
    if (mod(number, i) == 0) write (*,*) i, number/i
  end do
  
  
  i = int(sqrt(real(number))) 
  if (i*i == number) then
     write (*,*) i
  else if (mod(number, i) == 0) then
     write (*,*) i, number/i
  end if
    
end program
Task: Factors of an integer

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

typedef struct {
    int *list;
    short count; 
} Factors;

void xferFactors( Factors *fctrs, int *flist, int flix ) 
{
    int ix, ij;
    int newSize = fctrs->count + flix;
    if (newSize > flix)  {
        fctrs->list = realloc( fctrs->list, newSize * sizeof(int));
    }
    else {
        fctrs->list = malloc(  newSize * sizeof(int));
    }
    for (ij=0,ix=fctrs->count; ix<newSize; ij++,ix++) {
        fctrs->list[ix] = flist[ij];
    }
    fctrs->count = newSize;
}

Factors *factor( int num, Factors *fctrs)
{
    int flist[301], flix;
    int dvsr;
    flix = 0;
    fctrs->count = 0;
    free(fctrs->list);
    fctrs->list = NULL;
    for (dvsr=1; dvsr*dvsr < num; dvsr++) {
        if (num % dvsr != 0) continue;
        if ( flix == 300) {
            xferFactors( fctrs, flist, flix );
            flix = 0;
        }
        flist[flix++] = dvsr;
        flist[flix++] = num/dvsr;
    }
    if (dvsr*dvsr == num) 
        flist[flix++] = dvsr;
    if (flix > 0)
        xferFactors( fctrs, flist, flix );

    return fctrs;
}
        
int main(int argc, char*argv[])
{
    int nums2factor[] = { 2059, 223092870, 3135, 45 };
    Factors ftors = { NULL, 0};
    char sep;
    int i,j;

    for (i=0; i<4; i++) {
        factor( nums2factor[i], &ftors );
        printf("\nfactors of %d are:\n  ", nums2factor[i]);
        sep = ' ';
        for (j=0; j<ftors.count; j++) {
            printf("%c %d", sep, ftors.list[j]);
            sep = ',';
        }
        printf("\n");
    }
    return 0;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CALCULATING.
           03  NUM  USAGE BINARY-LONG VALUE ZERO.
           03  LIM  USAGE BINARY-LONG VALUE ZERO.
           03  CNT  USAGE BINARY-LONG VALUE ZERO.
           03  DIV  USAGE BINARY-LONG VALUE ZERO.
           03  REM  USAGE BINARY-LONG VALUE ZERO.
           03  ZRS  USAGE BINARY-SHORT VALUE ZERO.

       01  DISPLAYING.
           03  DIS  PIC 9(10) USAGE DISPLAY.

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "Factors of? " WITH NO ADVANCING
           ACCEPT NUM
           DIVIDE NUM BY 2 GIVING LIM.

           PERFORM VARYING CNT FROM 1 BY 1 UNTIL CNT > LIM
               DIVIDE NUM BY CNT GIVING DIV REMAINDER REM
               IF REM = 0
                   MOVE CNT TO DIS
                   PERFORM SHODIS
               END-IF
           END-PERFORM.

           MOVE NUM TO DIS.
           PERFORM SHODIS.
           STOP RUN.

       SHODIS.
           MOVE ZERO TO ZRS.
           INSPECT DIS TALLYING ZRS FOR LEADING ZERO.
           DISPLAY DIS(ZRS + 1:)
           EXIT PARAGRAPH.

       END PROGRAM FACTORS.
Task: Factors of an integer



C#: static void Main (string[] args) {
    do {
        Console.WriteLine ("Number:");
        Int64 p = 0;
        do {
            try {
                p = Convert.ToInt64 (Console.ReadLine ());
                break;
            } catch (Exception) { }

        } while (true);

        Console.WriteLine ("For 1 through " + ((int) Math.Sqrt (p)).ToString () + "");
        for (int x = 1; x <= (int) Math.Sqrt (p); x++) {
            if (p % x == 0)
                Console.WriteLine ("Found: " + x.ToString () + ". " + p.ToString () + " / " + x.ToString () + " = " + (p / x).ToString ());
        }

        Console.WriteLine ("Done.");
    } while (true);
}







COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CALCULATING.
           03  NUM  USAGE BINARY-LONG VALUE ZERO.
           03  LIM  USAGE BINARY-LONG VALUE ZERO.
           03  CNT  USAGE BINARY-LONG VALUE ZERO.
           03  DIV  USAGE BINARY-LONG VALUE ZERO.
           03  REM  USAGE BINARY-LONG VALUE ZERO.
           03  ZRS  USAGE BINARY-SHORT VALUE ZERO.

       01  DISPLAYING.
           03  DIS  PIC 9(10) USAGE DISPLAY.

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "Factors of? " WITH NO ADVANCING
           ACCEPT NUM
           DIVIDE NUM BY 2 GIVING LIM.

           PERFORM VARYING CNT FROM 1 BY 1 UNTIL CNT > LIM
               DIVIDE NUM BY CNT GIVING DIV REMAINDER REM
               IF REM = 0
                   MOVE CNT TO DIS
                   PERFORM SHODIS
               END-IF
           END-PERFORM.

           MOVE NUM TO DIS.
           PERFORM SHODIS.
           STOP RUN.

       SHODIS.
           MOVE ZERO TO ZRS.
           INSPECT DIS TALLYING ZRS FOR LEADING ZERO.
           DISPLAY DIS(ZRS + 1:)
           EXIT PARAGRAPH.

       END PROGRAM FACTORS.
Task: Factors of an integer


C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>

std::vector<int> GenerateFactors(int n) {
    std::vector<int> factors = { 1, n };
    for (int i = 2; i * i <= n; ++i) {
        if (n % i == 0) {
            factors.push_back(i);
            if (i * i != n)
                factors.push_back(n / i);
        }
    }

    std::sort(factors.begin(), factors.end());
    return factors;
}

int main() {
    const int SampleNumbers[] = { 3135, 45, 60, 81 };

    for (size_t i = 0; i < sizeof(SampleNumbers) / sizeof(int); ++i) {
        std::vector<int> factors = GenerateFactors(SampleNumbers[i]);
        std::cout << "Factors of ";
        std::cout.width(4);
        std::cout << SampleNumbers[i] << " are: ";
        std::copy(factors.begin(), factors.end(), std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
    }

    return EXIT_SUCCESS;
}








COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CALCULATING.
           03  NUM  USAGE BINARY-LONG VALUE ZERO.
           03  LIM  USAGE BINARY-LONG VALUE ZERO.
           03  CNT  USAGE BINARY-LONG VALUE ZERO.
           03  DIV  USAGE BINARY-LONG VALUE ZERO.
           03  REM  USAGE BINARY-LONG VALUE ZERO.
           03  ZRS  USAGE BINARY-SHORT VALUE ZERO.

       01  DISPLAYING.
           03  DIS  PIC 9(10) USAGE DISPLAY.

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "Factors of? " WITH NO ADVANCING
           ACCEPT NUM
           DIVIDE NUM BY 2 GIVING LIM.

           PERFORM VARYING CNT FROM 1 BY 1 UNTIL CNT > LIM
               DIVIDE NUM BY CNT GIVING DIV REMAINDER REM
               IF REM = 0
                   MOVE CNT TO DIS
                   PERFORM SHODIS
               END-IF
           END-PERFORM.

           MOVE NUM TO DIS.
           PERFORM SHODIS.
           STOP RUN.

       SHODIS.
           MOVE ZERO TO ZRS.
           INSPECT DIS TALLYING ZRS FOR LEADING ZERO.
           DISPLAY DIS(ZRS + 1:)
           EXIT PARAGRAPH.

       END PROGRAM FACTORS.
Task: Factors of an integer





Java: public static TreeSet<Long> factors(long n)
{
 TreeSet<Long> factors = new TreeSet<Long>();
 factors.add(n);
 factors.add(1L);
 for(long test = n - 1; test >= Math.sqrt(n); test--)
  if(n % test == 0)
  {
   factors.add(test);
   factors.add(n / test);
  }
 return factors;
}





COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CALCULATING.
           03  NUM  USAGE BINARY-LONG VALUE ZERO.
           03  LIM  USAGE BINARY-LONG VALUE ZERO.
           03  CNT  USAGE BINARY-LONG VALUE ZERO.
           03  DIV  USAGE BINARY-LONG VALUE ZERO.
           03  REM  USAGE BINARY-LONG VALUE ZERO.
           03  ZRS  USAGE BINARY-SHORT VALUE ZERO.

       01  DISPLAYING.
           03  DIS  PIC 9(10) USAGE DISPLAY.

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "Factors of? " WITH NO ADVANCING
           ACCEPT NUM
           DIVIDE NUM BY 2 GIVING LIM.

           PERFORM VARYING CNT FROM 1 BY 1 UNTIL CNT > LIM
               DIVIDE NUM BY CNT GIVING DIV REMAINDER REM
               IF REM = 0
                   MOVE CNT TO DIS
                   PERFORM SHODIS
               END-IF
           END-PERFORM.

           MOVE NUM TO DIS.
           PERFORM SHODIS.
           STOP RUN.

       SHODIS.
           MOVE ZERO TO ZRS.
           INSPECT DIS TALLYING ZRS FOR LEADING ZERO.
           DISPLAY DIS(ZRS + 1:)
           EXIT PARAGRAPH.

       END PROGRAM FACTORS.
Task: Factors of an integer







Python: >>> def factors(n):
      return [i for i in range(1, n + 1) if not n%i]



COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CALCULATING.
           03  NUM  USAGE BINARY-LONG VALUE ZERO.
           03  LIM  USAGE BINARY-LONG VALUE ZERO.
           03  CNT  USAGE BINARY-LONG VALUE ZERO.
           03  DIV  USAGE BINARY-LONG VALUE ZERO.
           03  REM  USAGE BINARY-LONG VALUE ZERO.
           03  ZRS  USAGE BINARY-SHORT VALUE ZERO.

       01  DISPLAYING.
           03  DIS  PIC 9(10) USAGE DISPLAY.

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "Factors of? " WITH NO ADVANCING
           ACCEPT NUM
           DIVIDE NUM BY 2 GIVING LIM.

           PERFORM VARYING CNT FROM 1 BY 1 UNTIL CNT > LIM
               DIVIDE NUM BY CNT GIVING DIV REMAINDER REM
               IF REM = 0
                   MOVE CNT TO DIS
                   PERFORM SHODIS
               END-IF
           END-PERFORM.

           MOVE NUM TO DIS.
           PERFORM SHODIS.
           STOP RUN.

       SHODIS.
           MOVE ZERO TO ZRS.
           INSPECT DIS TALLYING ZRS FOR LEADING ZERO.
           DISPLAY DIS(ZRS + 1:)
           EXIT PARAGRAPH.

       END PROGRAM FACTORS.
Task: Factors of an integer








VB: Function factors(num As UInt64) As UInt64()
  
  Dim result() As UInt64
  Dim iFactor As UInt64 = 1
  While iFactor <= num/2 
    If num Mod iFactor = 0 Then
      result.Append(iFactor)
    End If
    iFactor = iFactor + 1
  Wend
  result.Append(num) 
  Return result
End Function


COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CALCULATING.
           03  NUM  USAGE BINARY-LONG VALUE ZERO.
           03  LIM  USAGE BINARY-LONG VALUE ZERO.
           03  CNT  USAGE BINARY-LONG VALUE ZERO.
           03  DIV  USAGE BINARY-LONG VALUE ZERO.
           03  REM  USAGE BINARY-LONG VALUE ZERO.
           03  ZRS  USAGE BINARY-SHORT VALUE ZERO.

       01  DISPLAYING.
           03  DIS  PIC 9(10) USAGE DISPLAY.

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "Factors of? " WITH NO ADVANCING
           ACCEPT NUM
           DIVIDE NUM BY 2 GIVING LIM.

           PERFORM VARYING CNT FROM 1 BY 1 UNTIL CNT > LIM
               DIVIDE NUM BY CNT GIVING DIV REMAINDER REM
               IF REM = 0
                   MOVE CNT TO DIS
                   PERFORM SHODIS
               END-IF
           END-PERFORM.

           MOVE NUM TO DIS.
           PERFORM SHODIS.
           STOP RUN.

       SHODIS.
           MOVE ZERO TO ZRS.
           INSPECT DIS TALLYING ZRS FOR LEADING ZERO.
           DISPLAY DIS(ZRS + 1:)
           EXIT PARAGRAPH.

       END PROGRAM FACTORS.
Task: Factors of an integer




Go: package main

import "fmt"

func main() {
    printFactors(-1)
    printFactors(0)
    printFactors(1)
    printFactors(2)
    printFactors(3)
    printFactors(53)
    printFactors(45)
    printFactors(64)
    printFactors(600851475143)
    printFactors(999999999999999989)
}

func printFactors(nr int64) {
    if nr < 1 {
        fmt.Println("\nFactors of", nr, "not computed")
        return
    }
    fmt.Printf("\nFactors of %d: ", nr)
    fs := make([]int64, 1)
    fs[0] = 1
    apf := func(p int64, e int) {
        n := len(fs)
        for i, pp := 0, p; i < e; i, pp = i+1, pp*p {
            for j := 0; j < n; j++ {
                fs = append(fs, fs[j]*pp)
            }
        }
    }
    e := 0
    for ; nr & 1 == 0; e++ {
        nr >>= 1
    }
    apf(2, e)
    for d := int64(3); nr > 1; d += 2 {
        if d*d > nr {
            d = nr
        }
        for e = 0; nr%d == 0; e++ {
            nr /= d
        }
        if e > 0 {
            apf(d, e)
        }
    }
    fmt.Println(fs)
    fmt.Println("Number of factors =", len(fs))
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CALCULATING.
           03  NUM  USAGE BINARY-LONG VALUE ZERO.
           03  LIM  USAGE BINARY-LONG VALUE ZERO.
           03  CNT  USAGE BINARY-LONG VALUE ZERO.
           03  DIV  USAGE BINARY-LONG VALUE ZERO.
           03  REM  USAGE BINARY-LONG VALUE ZERO.
           03  ZRS  USAGE BINARY-SHORT VALUE ZERO.

       01  DISPLAYING.
           03  DIS  PIC 9(10) USAGE DISPLAY.

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "Factors of? " WITH NO ADVANCING
           ACCEPT NUM
           DIVIDE NUM BY 2 GIVING LIM.

           PERFORM VARYING CNT FROM 1 BY 1 UNTIL CNT > LIM
               DIVIDE NUM BY CNT GIVING DIV REMAINDER REM
               IF REM = 0
                   MOVE CNT TO DIS
                   PERFORM SHODIS
               END-IF
           END-PERFORM.

           MOVE NUM TO DIS.
           PERFORM SHODIS.
           STOP RUN.

       SHODIS.
           MOVE ZERO TO ZRS.
           INSPECT DIS TALLYING ZRS FOR LEADING ZERO.
           DISPLAY DIS(ZRS + 1:)
           EXIT PARAGRAPH.

       END PROGRAM FACTORS.
Task: Factors of an integer






PHP: function GetFactors($n){
   $factors = array(1, $n);
   for($i = 2; $i * $i <= $n; $i++){
      if($n % $i == 0){
         $factors[] = $i;
         if($i * $i != $n)
            $factors[] = $n/$i;
      }
   }
   sort($factors);
   return $factors;
}
Task: Factors of an integer






PHP: function GetFactors($n){
   $factors = array(1, $n);
   for($i = 2; $i * $i <= $n; $i++){
      if($n % $i == 0){
         $factors[] = $i;
         if($i * $i != $n)
            $factors[] = $n/$i;
      }
   }
   sort($factors);
   return $factors;
}






Ada: with Ada.Text_IO;
with Ada.Command_Line;
procedure Factors is
   Number  : Positive;
   Test_Nr : Positive := 1;
begin
   if Ada.Command_Line.Argument_Count /= 1 then
      Ada.Text_IO.Put (Ada.Text_IO.Standard_Error, "Missing argument!");
      Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
      return;
   end if;
   Number := Positive'Value (Ada.Command_Line.Argument (1));
   Ada.Text_IO.Put ("Factors of" & Positive'Image (Number) & ": ");
   loop
      if Number mod Test_Nr = 0 then
         Ada.Text_IO.Put (Positive'Image (Test_Nr) & ",");
      end if;
      exit when Test_Nr ** 2 >= Number;
      Test_Nr := Test_Nr + 1;
   end loop;
   Ada.Text_IO.Put_Line (Positive'Image (Number) & ".");
end Factors;
Task: Factors of an integer






PHP: function GetFactors($n){
   $factors = array(1, $n);
   for($i = 2; $i * $i <= $n; $i++){
      if($n % $i == 0){
         $factors[] = $i;
         if($i * $i != $n)
            $factors[] = $n/$i;
      }
   }
   sort($factors);
   return $factors;
}




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. FACTORS.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  CALCULATING.
           03  NUM  USAGE BINARY-LONG VALUE ZERO.
           03  LIM  USAGE BINARY-LONG VALUE ZERO.
           03  CNT  USAGE BINARY-LONG VALUE ZERO.
           03  DIV  USAGE BINARY-LONG VALUE ZERO.
           03  REM  USAGE BINARY-LONG VALUE ZERO.
           03  ZRS  USAGE BINARY-SHORT VALUE ZERO.

       01  DISPLAYING.
           03  DIS  PIC 9(10) USAGE DISPLAY.

       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           DISPLAY "Factors of? " WITH NO ADVANCING
           ACCEPT NUM
           DIVIDE NUM BY 2 GIVING LIM.

           PERFORM VARYING CNT FROM 1 BY 1 UNTIL CNT > LIM
               DIVIDE NUM BY CNT GIVING DIV REMAINDER REM
               IF REM = 0
                   MOVE CNT TO DIS
                   PERFORM SHODIS
               END-IF
           END-PERFORM.

           MOVE NUM TO DIS.
           PERFORM SHODIS.
           STOP RUN.

       SHODIS.
           MOVE ZERO TO ZRS.
           INSPECT DIS TALLYING ZRS FOR LEADING ZERO.
           DISPLAY DIS(ZRS + 1:)
           EXIT PARAGRAPH.

       END PROGRAM FACTORS.
Task: Regular expressions



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

class Program {
    static void Main(string[] args) {
        string str = "I am a string";

        if (new Regex("string$").IsMatch(str)) {
            Console.WriteLine("Ends with string.");
        }

        str = new Regex(" a ").Replace(str, " another ");
        Console.WriteLine(str);
    }
}









Ada: with Ada.Text_IO; with Gnat.Regpat; use Ada.Text_IO;

procedure Regex is

   package Pat renames Gnat.Regpat;

   procedure Search_For_Pattern(Compiled_Expression: Pat.Pattern_Matcher;
                                Search_In: String;
                                First, Last: out Positive;
                                Found: out Boolean) is
      Result: Pat.Match_Array (0 .. 1);
   begin
      Pat.Match(Compiled_Expression, Search_In, Result);
      Found := not Pat."="(Result(1), Pat.No_Match);
      if Found then
         First := Result(1).First;
         Last := Result(1).Last;
      end if;
   end Search_For_Pattern;

   Word_Pattern: constant String := "([a-zA-Z]+)";

   Str:           String:= "I love PATTERN matching!";
   Current_First: Positive := Str'First;
   First, Last:   Positive;
   Found:         Boolean;

begin
   
   loop
      Search_For_Pattern(Pat.Compile(Word_Pattern),
                         Str(Current_First .. Str'Last),
                         First, Last, Found);
   exit when not Found;
      Put_Line("<" & Str(First .. Last) & ">");
      Current_First := Last+1;
   end loop;

   
   Search_For_Pattern(Pat.Compile("(PATTERN)"), Str, First, Last, Found);
   Str := Str(Str'First .. First-1) & "pattern" & Str(Last+1 .. Str'Last);
   Put_Line(Str);
end Regex;