WeixiangYan/CodeTransOcean huggingface dataset:
Task: Sum of a series





Java: public class Sum{
    public static double f(double x){
       return 1/(x*x);
    }
 
    public static void main(String[] args){
       double start = 1;
       double end = 1000;
       double sum = 0;
 
       for(double x = start;x <= end;x++) sum += f(x);
 
       System.out.println("Sum of f(x) from " + start + " to " + end +" is " + sum);
    }
}







Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Sum_Series is
   function F(X : Long_Float) return Long_Float is
   begin
      return 1.0 / X**2;
   end F;
   package Lf_Io is new Ada.Text_Io.Float_Io(Long_Float);
   use Lf_Io;
   Sum : Long_Float := 0.0;
   subtype Param_Range is Integer range 1..1000;
begin
   for I in Param_Range loop
      Sum := Sum + F(Long_Float(I));
   end loop;
   Put("Sum of F(x) from" & Integer'Image(Param_Range'First) &
      " to" & Integer'Image(Param_Range'Last) & " is ");
   Put(Item => Sum, Aft => 10, Exp => 0);
   New_Line;
end Sum_Series;
Task: Sum of a series







Python: print ( sum(1.0 / (x * x) for x in range(1, 1001)) )





Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Sum_Series is
   function F(X : Long_Float) return Long_Float is
   begin
      return 1.0 / X**2;
   end F;
   package Lf_Io is new Ada.Text_Io.Float_Io(Long_Float);
   use Lf_Io;
   Sum : Long_Float := 0.0;
   subtype Param_Range is Integer range 1..1000;
begin
   for I in Param_Range loop
      Sum := Sum + F(Long_Float(I));
   end loop;
   Put("Sum of F(x) from" & Integer'Image(Param_Range'First) &
      " to" & Integer'Image(Param_Range'Last) & " is ");
   Put(Item => Sum, Aft => 10, Exp => 0);
   New_Line;
end Sum_Series;
Task: Sum of a series








VB: Public Sub Main() 

  Print "s(1000) = "; sumSeries(1000) 
  Print "zeta(2) = "; Pi * Pi / 6  

End 

Function sumSeries(n As Integer) As Float 

  If n = 0 Then Return 0 
  Dim sum As Float = 0 
  For k As Integer = 1 To n 
    sum += 1.0 / (k * k) 
  Next 
  Return sum 

End Function




Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Sum_Series is
   function F(X : Long_Float) return Long_Float is
   begin
      return 1.0 / X**2;
   end F;
   package Lf_Io is new Ada.Text_Io.Float_Io(Long_Float);
   use Lf_Io;
   Sum : Long_Float := 0.0;
   subtype Param_Range is Integer range 1..1000;
begin
   for I in Param_Range loop
      Sum := Sum + F(Long_Float(I));
   end loop;
   Put("Sum of F(x) from" & Integer'Image(Param_Range'First) &
      " to" & Integer'Image(Param_Range'Last) & " is ");
   Put(Item => Sum, Aft => 10, Exp => 0);
   New_Line;
end Sum_Series;
Task: Sum of a series

C: #include <stdio.h>

double Invsqr(double n)
{
	return 1 / (n*n);
}

int main (int argc, char *argv[])
{
	int i, start = 1, end = 1000;
	double sum = 0.0;
	
	for( i = start; i <= end; i++)
		sum += Invsqr((double)i);           
	
	printf("%16.14f\n", sum);
	
	return 0;
}
Task: Sum of a series



C#: class Program
{
    static void Main(string[] args)
    {
        

        List<double> myList = new List<double>();
        for (double i = 1; i < 1001; i++)
        {
            myList.Add(i);
        }
        

        var sum = myList.Sum(x => 1/(x*x));

        Console.WriteLine(sum);
        Console.ReadLine();
    }
}
Task: Sum of a series


C++: #include <iostream>

double f(double x);

int main()
{
    unsigned int start = 1;
    unsigned int end = 1000;
    double sum = 0;

    for( unsigned int x = start; x <= end; ++x )
    {
        sum += f(x);
    }
    std::cout << "Sum of f(x) from " << start << " to " << end << " is " << sum << std::endl;
    return 0;
}


double f(double x)
{
    return ( 1.0 / ( x * x ) );
}
Task: Sum of a series





Java: public class Sum{
    public static double f(double x){
       return 1/(x*x);
    }
 
    public static void main(String[] args){
       double start = 1;
       double end = 1000;
       double sum = 0;
 
       for(double x = start;x <= end;x++) sum += f(x);
 
       System.out.println("Sum of f(x) from " + start + " to " + end +" is " + sum);
    }
}
Task: Sum of a series







Python: print ( sum(1.0 / (x * x) for x in range(1, 1001)) )
Task: Sum of a series








VB: Public Sub Main() 

  Print "s(1000) = "; sumSeries(1000) 
  Print "zeta(2) = "; Pi * Pi / 6  

End 

Function sumSeries(n As Integer) As Float 

  If n = 0 Then Return 0 
  Dim sum As Float = 0 
  For k As Integer = 1 To n 
    sum += 1.0 / (k * k) 
  Next 
  Return sum 

End Function
Task: Sum of a series




Go: package main

import ("fmt"; "math")

func main() {
    fmt.Println("known:   ", math.Pi*math.Pi/6)
    sum := 0.
    for i := 1e3; i > 0; i-- {
        sum += 1 / (i * i)
    }
    fmt.Println("computed:", sum)
}
Task: Sum of a series



C#: class Program
{
    static void Main(string[] args)
    {
        

        List<double> myList = new List<double>();
        for (double i = 1; i < 1001; i++)
        {
            myList.Add(i);
        }
        

        var sum = myList.Sum(x => 1/(x*x));

        Console.WriteLine(sum);
        Console.ReadLine();
    }
}








Fortran: real, dimension(1000) :: a = (/ (1.0/(i*i), i=1, 1000) /)
real :: result

result = sum(a);
Task: Sum of a series


C++: #include <iostream>

double f(double x);

int main()
{
    unsigned int start = 1;
    unsigned int end = 1000;
    double sum = 0;

    for( unsigned int x = start; x <= end; ++x )
    {
        sum += f(x);
    }
    std::cout << "Sum of f(x) from " << start << " to " << end << " is " << sum << std::endl;
    return 0;
}


double f(double x)
{
    return ( 1.0 / ( x * x ) );
}









Fortran: real, dimension(1000) :: a = (/ (1.0/(i*i), i=1, 1000) /)
real :: result

result = sum(a);
Task: Sum of a series

C: #include <stdio.h>

double Invsqr(double n)
{
	return 1 / (n*n);
}

int main (int argc, char *argv[])
{
	int i, start = 1, end = 1000;
	double sum = 0.0;
	
	for( i = start; i <= end; i++)
		sum += Invsqr((double)i);           
	
	printf("%16.14f\n", sum);
	
	return 0;
}










Fortran: real, dimension(1000) :: a = (/ (1.0/(i*i), i=1, 1000) /)
real :: result

result = sum(a);
Task: Sum of a series





Java: public class Sum{
    public static double f(double x){
       return 1/(x*x);
    }
 
    public static void main(String[] args){
       double start = 1;
       double end = 1000;
       double sum = 0;
 
       for(double x = start;x <= end;x++) sum += f(x);
 
       System.out.println("Sum of f(x) from " + start + " to " + end +" is " + sum);
    }
}






Fortran: real, dimension(1000) :: a = (/ (1.0/(i*i), i=1, 1000) /)
real :: result

result = sum(a);
Task: Sum of a series







Python: print ( sum(1.0 / (x * x) for x in range(1, 1001)) )




Fortran: real, dimension(1000) :: a = (/ (1.0/(i*i), i=1, 1000) /)
real :: result

result = sum(a);
Task: Sum of a series








VB: Public Sub Main() 

  Print "s(1000) = "; sumSeries(1000) 
  Print "zeta(2) = "; Pi * Pi / 6  

End 

Function sumSeries(n As Integer) As Float 

  If n = 0 Then Return 0 
  Dim sum As Float = 0 
  For k As Integer = 1 To n 
    sum += 1.0 / (k * k) 
  Next 
  Return sum 

End Function



Fortran: real, dimension(1000) :: a = (/ (1.0/(i*i), i=1, 1000) /)
real :: result

result = sum(a);
Task: Sum of a series






PHP: <?php




function sum_of_a_series($n,$k)
{
	$sum_of_a_series = 0;
	for($i=$k;$i<=$n;$i++)
	{
		$sum_of_a_series += (1/($i*$i));
	}
	return $sum_of_a_series;
}

echo sum_of_a_series(1000,1);





Fortran: real, dimension(1000) :: a = (/ (1.0/(i*i), i=1, 1000) /)
real :: result

result = sum(a);
Task: Sum of a series

C: #include <stdio.h>

double Invsqr(double n)
{
	return 1 / (n*n);
}

int main (int argc, char *argv[])
{
	int i, start = 1, end = 1000;
	double sum = 0.0;
	
	for( i = start; i <= end; i++)
		sum += Invsqr((double)i);           
	
	printf("%16.14f\n", sum);
	
	return 0;
}









COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. sum-of-series.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       78  N                       VALUE 1000.

       01  series-term             USAGE FLOAT-LONG.
       01  i                       PIC 9(4).

       PROCEDURE DIVISION.
           PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
               COMPUTE series-term = series-term + (1 / i ** 2)
           END-PERFORM

           DISPLAY series-term

           GOBACK
           .
Task: Sum of a series



C#: class Program
{
    static void Main(string[] args)
    {
        

        List<double> myList = new List<double>();
        for (double i = 1; i < 1001; i++)
        {
            myList.Add(i);
        }
        

        var sum = myList.Sum(x => 1/(x*x));

        Console.WriteLine(sum);
        Console.ReadLine();
    }
}







COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. sum-of-series.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       78  N                       VALUE 1000.

       01  series-term             USAGE FLOAT-LONG.
       01  i                       PIC 9(4).

       PROCEDURE DIVISION.
           PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
               COMPUTE series-term = series-term + (1 / i ** 2)
           END-PERFORM

           DISPLAY series-term

           GOBACK
           .
Task: Sum of a series


C++: #include <iostream>

double f(double x);

int main()
{
    unsigned int start = 1;
    unsigned int end = 1000;
    double sum = 0;

    for( unsigned int x = start; x <= end; ++x )
    {
        sum += f(x);
    }
    std::cout << "Sum of f(x) from " << start << " to " << end << " is " << sum << std::endl;
    return 0;
}


double f(double x)
{
    return ( 1.0 / ( x * x ) );
}








COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. sum-of-series.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       78  N                       VALUE 1000.

       01  series-term             USAGE FLOAT-LONG.
       01  i                       PIC 9(4).

       PROCEDURE DIVISION.
           PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
               COMPUTE series-term = series-term + (1 / i ** 2)
           END-PERFORM

           DISPLAY series-term

           GOBACK
           .
Task: Sum of a series





Java: public class Sum{
    public static double f(double x){
       return 1/(x*x);
    }
 
    public static void main(String[] args){
       double start = 1;
       double end = 1000;
       double sum = 0;
 
       for(double x = start;x <= end;x++) sum += f(x);
 
       System.out.println("Sum of f(x) from " + start + " to " + end +" is " + sum);
    }
}





COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. sum-of-series.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       78  N                       VALUE 1000.

       01  series-term             USAGE FLOAT-LONG.
       01  i                       PIC 9(4).

       PROCEDURE DIVISION.
           PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
               COMPUTE series-term = series-term + (1 / i ** 2)
           END-PERFORM

           DISPLAY series-term

           GOBACK
           .
Task: Sum of a series







Python: print ( sum(1.0 / (x * x) for x in range(1, 1001)) )



COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. sum-of-series.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       78  N                       VALUE 1000.

       01  series-term             USAGE FLOAT-LONG.
       01  i                       PIC 9(4).

       PROCEDURE DIVISION.
           PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
               COMPUTE series-term = series-term + (1 / i ** 2)
           END-PERFORM

           DISPLAY series-term

           GOBACK
           .
Task: Sum of a series








VB: Public Sub Main() 

  Print "s(1000) = "; sumSeries(1000) 
  Print "zeta(2) = "; Pi * Pi / 6  

End 

Function sumSeries(n As Integer) As Float 

  If n = 0 Then Return 0 
  Dim sum As Float = 0 
  For k As Integer = 1 To n 
    sum += 1.0 / (k * k) 
  Next 
  Return sum 

End Function


COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. sum-of-series.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       78  N                       VALUE 1000.

       01  series-term             USAGE FLOAT-LONG.
       01  i                       PIC 9(4).

       PROCEDURE DIVISION.
           PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
               COMPUTE series-term = series-term + (1 / i ** 2)
           END-PERFORM

           DISPLAY series-term

           GOBACK
           .
Task: Sum of a series




Go: package main

import ("fmt"; "math")

func main() {
    fmt.Println("known:   ", math.Pi*math.Pi/6)
    sum := 0.
    for i := 1e3; i > 0; i-- {
        sum += 1 / (i * i)
    }
    fmt.Println("computed:", sum)
}






COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. sum-of-series.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       78  N                       VALUE 1000.

       01  series-term             USAGE FLOAT-LONG.
       01  i                       PIC 9(4).

       PROCEDURE DIVISION.
           PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
               COMPUTE series-term = series-term + (1 / i ** 2)
           END-PERFORM

           DISPLAY series-term

           GOBACK
           .
Task: Sum of a series






PHP: <?php




function sum_of_a_series($n,$k)
{
	$sum_of_a_series = 0;
	for($i=$k;$i<=$n;$i++)
	{
		$sum_of_a_series += (1/($i*$i));
	}
	return $sum_of_a_series;
}

echo sum_of_a_series(1000,1);
Task: Sum of a series






PHP: <?php




function sum_of_a_series($n,$k)
{
	$sum_of_a_series = 0;
	for($i=$k;$i<=$n;$i++)
	{
		$sum_of_a_series += (1/($i*$i));
	}
	return $sum_of_a_series;
}

echo sum_of_a_series(1000,1);






Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Sum_Series is
   function F(X : Long_Float) return Long_Float is
   begin
      return 1.0 / X**2;
   end F;
   package Lf_Io is new Ada.Text_Io.Float_Io(Long_Float);
   use Lf_Io;
   Sum : Long_Float := 0.0;
   subtype Param_Range is Integer range 1..1000;
begin
   for I in Param_Range loop
      Sum := Sum + F(Long_Float(I));
   end loop;
   Put("Sum of F(x) from" & Integer'Image(Param_Range'First) &
      " to" & Integer'Image(Param_Range'Last) & " is ");
   Put(Item => Sum, Aft => 10, Exp => 0);
   New_Line;
end Sum_Series;
Task: Sum of a series






PHP: <?php




function sum_of_a_series($n,$k)
{
	$sum_of_a_series = 0;
	for($i=$k;$i<=$n;$i++)
	{
		$sum_of_a_series += (1/($i*$i));
	}
	return $sum_of_a_series;
}

echo sum_of_a_series(1000,1);




COBOL:        IDENTIFICATION DIVISION.
       PROGRAM-ID. sum-of-series.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       78  N                       VALUE 1000.

       01  series-term             USAGE FLOAT-LONG.
       01  i                       PIC 9(4).

       PROCEDURE DIVISION.
           PERFORM VARYING i FROM 1 BY 1 UNTIL N < i
               COMPUTE series-term = series-term + (1 / i ** 2)
           END-PERFORM

           DISPLAY series-term

           GOBACK
           .
Task: Sum of a series








VB: Private Function sumto(n As Integer) As Double
    Dim res As Double
    For i = 1 To n
        res = res + 1 / i ^ 2
    Next i
    sumto = res
End Function
Public Sub main()
    Debug.Print sumto(1000)
End Sub
Task: Sierpinski carpet



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

class Program
{
    static List<string> NextCarpet(List<string> carpet)
    {
        return carpet.Select(x => x + x + x)
                     .Concat(carpet.Select(x => x + x.Replace('#', ' ') + x))
                     .Concat(carpet.Select(x => x + x + x)).ToList();
    }

    static List<string> SierpinskiCarpet(int n)
    {
        return Enumerable.Range(1, n).Aggregate(new List<string> { "#" }, (carpet, _) => NextCarpet(carpet));
    }

    static void Main(string[] args)
    {
        foreach (string s in SierpinskiCarpet(3))
            Console.WriteLine(s);
    }
}









Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Sierpinski_Carpet is
   subtype Index_Type is Integer range 1..81;
   type Pattern_Array is array(Index_Type range <>, Index_Type range <>) of Boolean;
   Pattern : Pattern_Array(1..81,1..81) := (Others =>(others => true));
   procedure Clear_Center(P : in out Pattern_Array; X1 : Index_Type; X2 : Index_Type;
         Y1 : Index_Type; Y2 : Index_Type) is
      Xfirst : Index_Type;
      Xlast  : Index_Type;
      Yfirst : Index_Type;
      Ylast  : Index_Type;
      Diff   : Integer;
   begin
      Xfirst :=(X2 - X1 + 1) / 3 + X1;
      Diff := Xfirst - X1;
      Xlast  := Xfirst + Diff;
      Yfirst := (Y2 - Y1) / 3 + Y1;
      YLast  := YFirst + Diff;

      for I in XFirst..XLast loop
         for J in YFirst..YLast loop
            P(I, J) := False;
         end loop;
      end loop;
   end Clear_Center;
   
   procedure Print(P : Pattern_Array) is
   begin
      for I in P'range(1) loop
         for J in P'range(2) loop
            if P(I,J) then
               Put('*');
            else
               Put(' ');
            end if;
         end loop;
         New_Line;
      end loop;
   end Print;
   
   procedure Divide_Square(P : in out Pattern_Array; Order : Positive) is
      Factor : Natural := 0;
      X1, X2 : Index_Type;
      Y1, Y2  : Index_Type;
      Division : Index_Type;
      Num_Sections : Index_Type;
   begin
      while Factor < Order loop
         Num_Sections := 3**Factor;
         Factor := Factor + 1;
         X1  := P'First;
         Division   := P'Last / Num_Sections;
         X2 := Division;
         Y1 := X1;
         Y2 := X2;
         loop
            loop
               Clear_Center(P, X1, X2, Y1, Y2);
               exit when X2 = P'Last;
               X1 := X2;
               X2 := X2 + Division;
            end loop;
            exit when Y2 = P'Last;
            Y1 := Y2;
            Y2 := Y2 + Division;
            X1 := P'First;
            X2 := Division;
         end loop;
      end loop;
   end Divide_Square;
   
begin
   Divide_Square(Pattern, 3);
   Print(Pattern);
end Sierpinski_Carpet;
Task: Sierpinski carpet

C: #include <stdio.h>

int main()
{
	int i, j, dim, d;
	int depth = 3;

	for (i = 0, dim = 1; i < depth; i++, dim *= 3);

	for (i = 0; i < dim; i++) {
		for (j = 0; j < dim; j++) {
			for (d = dim / 3; d; d /= 3)
				if ((i % (d * 3)) / d == 1 && (j % (d * 3)) / d == 1)
					break;
			printf(d ? "  " : "##");
		}
		printf("\n");
	}

	return 0;
}











Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Sierpinski_Carpet is
   subtype Index_Type is Integer range 1..81;
   type Pattern_Array is array(Index_Type range <>, Index_Type range <>) of Boolean;
   Pattern : Pattern_Array(1..81,1..81) := (Others =>(others => true));
   procedure Clear_Center(P : in out Pattern_Array; X1 : Index_Type; X2 : Index_Type;
         Y1 : Index_Type; Y2 : Index_Type) is
      Xfirst : Index_Type;
      Xlast  : Index_Type;
      Yfirst : Index_Type;
      Ylast  : Index_Type;
      Diff   : Integer;
   begin
      Xfirst :=(X2 - X1 + 1) / 3 + X1;
      Diff := Xfirst - X1;
      Xlast  := Xfirst + Diff;
      Yfirst := (Y2 - Y1) / 3 + Y1;
      YLast  := YFirst + Diff;

      for I in XFirst..XLast loop
         for J in YFirst..YLast loop
            P(I, J) := False;
         end loop;
      end loop;
   end Clear_Center;
   
   procedure Print(P : Pattern_Array) is
   begin
      for I in P'range(1) loop
         for J in P'range(2) loop
            if P(I,J) then
               Put('*');
            else
               Put(' ');
            end if;
         end loop;
         New_Line;
      end loop;
   end Print;
   
   procedure Divide_Square(P : in out Pattern_Array; Order : Positive) is
      Factor : Natural := 0;
      X1, X2 : Index_Type;
      Y1, Y2  : Index_Type;
      Division : Index_Type;
      Num_Sections : Index_Type;
   begin
      while Factor < Order loop
         Num_Sections := 3**Factor;
         Factor := Factor + 1;
         X1  := P'First;
         Division   := P'Last / Num_Sections;
         X2 := Division;
         Y1 := X1;
         Y2 := X2;
         loop
            loop
               Clear_Center(P, X1, X2, Y1, Y2);
               exit when X2 = P'Last;
               X1 := X2;
               X2 := X2 + Division;
            end loop;
            exit when Y2 = P'Last;
            Y1 := Y2;
            Y2 := Y2 + Division;
            X1 := P'First;
            X2 := Division;
         end loop;
      end loop;
   end Divide_Square;
   
begin
   Divide_Square(Pattern, 3);
   Print(Pattern);
end Sierpinski_Carpet;
Task: Sierpinski carpet


C++: 


#include <cstdint>
#include <cstdlib>
#include <cstdio>

static constexpr int32_t bct_low_bits = 0x55555555;

static int32_t bct_decrement(int32_t v) {
    --v;            
    return v ^ (v & (v>>1) & bct_low_bits);     
}

int main (int argc, char *argv[])
{
    
    const int32_t n = (1 < argc) ? std::atoi(argv[1]) : 3;
    
    if (n < 0 || 9 < n) {                       
        std::printf("N out of range (use 0..9): %ld\n", long(n));
        return 1;
    }

    const int32_t size_bct = 1<<(n*2);          
    
    int32_t y = size_bct;
    do {                                        
        y = bct_decrement(y);                   
        int32_t x = size_bct;
        do {                                    
            x = bct_decrement(x);               
            
            std::putchar((x & y & bct_low_bits) ? ' ' : '#');
        } while (0 < x);
        std::putchar('\n');
    } while (0 < y);

    return 0;
}










Ada: with Ada.Text_Io; use Ada.Text_Io;

procedure Sierpinski_Carpet is
   subtype Index_Type is Integer range 1..81;
   type Pattern_Array is array(Index_Type range <>, Index_Type range <>) of Boolean;
   Pattern : Pattern_Array(1..81,1..81) := (Others =>(others => true));
   procedure Clear_Center(P : in out Pattern_Array; X1 : Index_Type; X2 : Index_Type;
         Y1 : Index_Type; Y2 : Index_Type) is
      Xfirst : Index_Type;
      Xlast  : Index_Type;
      Yfirst : Index_Type;
      Ylast  : Index_Type;
      Diff   : Integer;
   begin
      Xfirst :=(X2 - X1 + 1) / 3 + X1;
      Diff := Xfirst - X1;
      Xlast  := Xfirst + Diff;
      Yfirst := (Y2 - Y1) / 3 + Y1;
      YLast  := YFirst + Diff;

      for I in XFirst..XLast loop
         for J in YFirst..YLast loop
            P(I, J) := False;
         end loop;
      end loop;
   end Clear_Center;
   
   procedure Print(P : Pattern_Array) is
   begin
      for I in P'range(1) loop
         for J in P'range(2) loop
            if P(I,J) then
               Put('*');
            else
               Put(' ');
            end if;
         end loop;
         New_Line;
      end loop;
   end Print;
   
   procedure Divide_Square(P : in out Pattern_Array; Order : Positive) is
      Factor : Natural := 0;
      X1, X2 : Index_Type;
      Y1, Y2  : Index_Type;
      Division : Index_Type;
      Num_Sections : Index_Type;
   begin
      while Factor < Order loop
         Num_Sections := 3**Factor;
         Factor := Factor + 1;
         X1  := P'First;
         Division   := P'Last / Num_Sections;
         X2 := Division;
         Y1 := X1;
         Y2 := X2;
         loop
            loop
               Clear_Center(P, X1, X2, Y1, Y2);
               exit when X2 = P'Last;
               X1 := X2;
               X2 := X2 + Division;
            end loop;
            exit when Y2 = P'Last;
            Y1 := Y2;
            Y2 := Y2 + Division;
            X1 := P'First;
            X2 := Division;
         end loop;
      end loop;
   end Divide_Square;
   
begin
   Divide_Square(Pattern, 3);
   Print(Pattern);
end Sierpinski_Carpet;