WeixiangYan/CodeTransOcean huggingface dataset:
Task: Globally replace text in several files







Python: import fileinput

for line in fileinput.input(inplace=True):
    print(line.replace('Goodbye London!', 'Hello New York!'), end='')




Fortran:       SUBROUTINE FILEHACK(FNAME,THIS,THAT)	
       CHARACTER*(*) FNAME	
       CHARACTER*(*) THIS	
       CHARACTER*(*) THAT	
       INTEGER F,T		
       PARAMETER (F=66,T=67)	
       INTEGER L		
       CHARACTER*6666 ALINE	
       LOGICAL AHIT		
        INQUIRE(FILE = FNAME, EXIST = AHIT)	
        IF (.NOT.AHIT) RETURN			
        OPEN (F,FILE=FNAME,STATUS="OLD",ACTION="READWRITE")	
        OPEN (T,STATUS="SCRATCH")	
        AHIT = .FALSE.		
Chew through the input, replacing THIS by THAT while writing to the temporary file..
   10   READ (F,11,END = 20) L,ALINE(1:MIN(L,LEN(ALINE)))	
        IF (L.GT.LEN(ALINE)) STOP "Monster record
   11   FORMAT (Q,A)		
        L1 = 1			
   12   L2 = INDEX(ALINE(L1:L),THIS)	
        IF (L2.LE.0) THEN		
          WRITE (T,13) ALINE(L1:L)	
   13     FORMAT (A)			
          GO TO 10			
        END IF				
   14   L2 = L1 + L2 - 2	
        WRITE (T,15) ALINE(L1:L2)	
   15   FORMAT (A,$)			
        WRITE (T,15) THAT		
        AHIT = .TRUE.			
        L1 = L2 + LEN(THIS) + 1		
        IF (L - L1 + 1 .GE. LEN(THIS)) GO TO 12	
        WRITE (T,13) ALINE(L1:L)	
        GO TO 10		
Copy the temporary file back over the source file. Hope for no mishap and data loss
   20   IF (AHIT) THEN	
          CLOSE (F)		
          REWIND T		
          OPEN (F,FILE="new"//FNAME,STATUS = "REPLACE",ACTION = "WRITE")	
   21     READ (T,11,END = 22) L,ALINE(1:MIN(L,LEN(ALINE)))	
          IF (L.GT.LEN(ALINE)) STOP "Monster changed record
          WRITE (F,13) ALINE(1:L) 		
          GO TO 21			
        END IF		
   22   CLOSE(T)	
        CLOSE(F)	
      END	

      PROGRAM ATTACK
      INTEGER N
      PARAMETER (N = 6)		
      CHARACTER*48 VICTIM(N)	
      DATA VICTIM/		
     1 "StaffStory.txt",
     2 "Accounts.dat",
     3 "TravelAgent.txt",
     4 "RemovalFirm.dat",
     5 "Addresses.txt",
     6 "SongLyrics.txt"/	

      DO I = 1,N	
        CALL FILEHACK(VICTIM(I),"Goodbye London
      END DO		

      END
Task: Continued fraction



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

namespace ContinuedFraction {
    class Program {
        static double Calc(Func<int, int[]> f, int n) {
            double temp = 0.0;
            for (int ni = n; ni >= 1; ni--) {
                int[] p = f(ni);
                temp = p[1] / (p[0] + temp);
            }
            return f(0)[0] + temp;
        }

        static void Main(string[] args) {
            List<Func<int, int[]>> fList = new List<Func<int, int[]>>();
            fList.Add(n => new int[] { n > 0 ? 2 : 1, 1 });
            fList.Add(n => new int[] { n > 0 ? n : 2, n > 1 ? (n - 1) : 1 });
            fList.Add(n => new int[] { n > 0 ? 6 : 3, (int) Math.Pow(2 * n - 1, 2) });

            foreach (var f in fList) {
                Console.WriteLine(Calc(f, 200));
            }
        }
    }
}









Ada: generic
   type Scalar is digits <>;

   with function A (N : in Natural)  return Natural;
   with function B (N : in Positive) return Natural;
function Continued_Fraction (Steps : in Natural) return Scalar;
Task: Continued fraction

C: 
#include <stdio.h>


typedef double (*coeff_func)(unsigned n);


double calc(coeff_func f_a, coeff_func f_b, unsigned expansions)
{
	double a, b, r;
	a = b = r = 0.0;

	unsigned i;
	for (i = expansions; i > 0; i--) {
		a = f_a(i);
		b = f_b(i);
		r = b / (a + r);
	}
	a = f_a(0);

	return a + r;
}


double sqrt2_a(unsigned n)
{
	return n ? 2.0 : 1.0;
}

double sqrt2_b(unsigned n)
{
	return 1.0;
}


double napier_a(unsigned n)
{
	return n ? n : 2.0;
}

double napier_b(unsigned n)
{
	return n > 1.0 ? n - 1.0 : 1.0;
}


double pi_a(unsigned n)
{
	return n ? 6.0 : 3.0;
}

double pi_b(unsigned n)
{
	double c = 2.0 * n - 1.0;

	return c * c;
}

int main(void)
{
	double sqrt2, napier, pi;

	sqrt2  = calc(sqrt2_a,  sqrt2_b,  1000);
	napier = calc(napier_a, napier_b, 1000);
	pi     = calc(pi_a,     pi_b,     1000);

	printf("%12.10g\n%12.10g\n%12.10g\n", sqrt2, napier, pi);

	return 0;
}











Ada: generic
   type Scalar is digits <>;

   with function A (N : in Natural)  return Natural;
   with function B (N : in Positive) return Natural;
function Continued_Fraction (Steps : in Natural) return Scalar;
Task: Continued fraction


C++: #include <iomanip>
#include <iostream>
#include <tuple>

typedef std::tuple<double,double> coeff_t; 
typedef coeff_t (*func_t)(int); 

double calc(func_t func, int n)
{
    double a, b, temp = 0;
    for (; n > 0; --n) {
        std::tie(a, b) = func(n);
        temp = b / (a + temp);
    }
    std::tie(a, b) = func(0);
    return a + temp;
}

coeff_t sqrt2(int n)
{
    return coeff_t(n > 0 ? 2 : 1, 1);
}

coeff_t napier(int n)
{
    return coeff_t(n > 0 ? n : 2, n > 1 ? n - 1 : 1);
}

coeff_t pi(int n)
{
    return coeff_t(n > 0 ? 6 : 3, (2 * n - 1) * (2 * n - 1));
}

int main()
{
    std::streamsize old_prec = std::cout.precision(15); 
    std::cout 
        << calc(sqrt2, 20) << '\n'
        << calc(napier, 15) << '\n'
        << calc(pi, 10000) << '\n'
        << std::setprecision(old_prec); 
}










Ada: generic
   type Scalar is digits <>;

   with function A (N : in Natural)  return Natural;
   with function B (N : in Positive) return Natural;
function Continued_Fraction (Steps : in Natural) return Scalar;
Task: Continued fraction




Go: package main

import "fmt"

type cfTerm struct {
    a, b int
}



type cf []cfTerm

func cfSqrt2(nTerms int) cf {
    f := make(cf, nTerms)
    for n := range f {
        f[n] = cfTerm{2, 1}
    }
    f[0].a = 1
    return f
}

func cfNap(nTerms int) cf {
    f := make(cf, nTerms)
    for n := range f {
        f[n] = cfTerm{n, n - 1}
    }
    f[0].a = 2
    f[1].b = 1
    return f
}

func cfPi(nTerms int) cf {
    f := make(cf, nTerms)
    for n := range f {
        g := 2*n - 1
        f[n] = cfTerm{6, g * g}
    }
    f[0].a = 3
    return f
}

func (f cf) real() (r float64) {
    for n := len(f) - 1; n > 0; n-- {
        r = float64(f[n].b) / (float64(f[n].a) + r)
    }
    return r + float64(f[0].a)
}

func main() {
    fmt.Println("sqrt2:", cfSqrt2(20).real())
    fmt.Println("nap:  ", cfNap(20).real())
    fmt.Println("pi:   ", cfPi(20).real())
}








Ada: generic
   type Scalar is digits <>;

   with function A (N : in Natural)  return Natural;
   with function B (N : in Positive) return Natural;
function Continued_Fraction (Steps : in Natural) return Scalar;
Task: Continued fraction





Java: import static java.lang.Math.pow;
import java.util.*;
import java.util.function.Function;

public class Test {
    static double calc(Function<Integer, Integer[]> f, int n) {
        double temp = 0;

        for (int ni = n; ni >= 1; ni--) {
            Integer[] p = f.apply(ni);
            temp = p[1] / (double) (p[0] + temp);
        }
        return f.apply(0)[0] + temp;
    }

    public static void main(String[] args) {
        List<Function<Integer, Integer[]>> fList = new ArrayList<>();
        fList.add(n -> new Integer[]{n > 0 ? 2 : 1, 1});
        fList.add(n -> new Integer[]{n > 0 ? n : 2, n > 1 ? (n - 1) : 1});
        fList.add(n -> new Integer[]{n > 0 ? 6 : 3, (int) pow(2 * n - 1, 2)});

        for (Function<Integer, Integer[]> f : fList)
            System.out.println(calc(f, 200));
    }
}







Ada: generic
   type Scalar is digits <>;

   with function A (N : in Natural)  return Natural;
   with function B (N : in Positive) return Natural;
function Continued_Fraction (Steps : in Natural) return Scalar;
Task: Continued fraction







Python: from fractions import Fraction
import itertools
try: zip = itertools.izip
except: pass
 

def CF(a, b, t):
  terms = list(itertools.islice(zip(a, b), t))
  z = Fraction(1,1)
  for a, b in reversed(terms):
    z = a + b / z
  return z
 

def pRes(x, d):
  q, x = divmod(x, 1)
  res = str(q)
  res += "."
  for i in range(d):
    x *= 10
    q, x = divmod(x, 1)
    res += str(q)
  return res
 

def sqrt2_a():
  yield 1
  for x in itertools.repeat(2):
    yield x
 
def sqrt2_b():
  for x in itertools.repeat(1):
    yield x
 
cf = CF(sqrt2_a(), sqrt2_b(), 950)
print(pRes(cf, 200))

 
 

def Napier_a():
  yield 2
  for x in itertools.count(1):
    yield x
 
def Napier_b():
  yield 1
  for x in itertools.count(1):
    yield x
 
cf = CF(Napier_a(), Napier_b(), 950)
print(pRes(cf, 200))

 

def Pi_a():
  yield 3
  for x in itertools.repeat(6):
    yield x
 
def Pi_b():
  for x in itertools.count(1,2):
    yield x*x
 
cf = CF(Pi_a(), Pi_b(), 950)
print(pRes(cf, 10))






Ada: generic
   type Scalar is digits <>;

   with function A (N : in Natural)  return Natural;
   with function B (N : in Positive) return Natural;
function Continued_Fraction (Steps : in Natural) return Scalar;
Task: Continued fraction








VB: Public Const precision = 10000
Private Function continued_fraction(steps As Integer, rid_a As String, rid_b As String) As Double
    Dim res As Double
    res = 0
    For n = steps To 1 Step -1
       res = Application.Run(rid_b, n) / (Application.Run(rid_a, n) + res)
    Next n
    continued_fraction = Application.Run(rid_a, 0) + res
End Function
     
Function sqr2_a(n As Integer) As Integer
    sqr2_a = IIf(n = 0, 1, 2)
End Function

Function sqr2_b(n As Integer) As Integer
    sqr2_b = 1
End Function
 
Function nap_a(n As Integer) As Integer
    nap_a = IIf(n = 0, 2, n)
End Function

Function nap_b(n As Integer) As Integer
    nap_b = IIf(n = 1, 1, n - 1)
End Function
 
Function pi_a(n As Integer) As Integer
    pi_a = IIf(n = 0, 3, 6)
End Function

Function pi_b(n As Integer) As Long
    pi_b = IIf(n = 1, 1, (2 * n - 1) ^ 2)
End Function

Public Sub main()
    Debug.Print "Precision:", precision
    Debug.Print "Sqr(2):", continued_fraction(precision, "sqr2_a", "sqr2_b")
    Debug.Print "Napier:", continued_fraction(precision, "nap_a", "nap_b")
    Debug.Print "Pi:", continued_fraction(precision, "pi_a", "pi_b")
End Sub




Ada: generic
   type Scalar is digits <>;

   with function A (N : in Natural)  return Natural;
   with function B (N : in Positive) return Natural;
function Continued_Fraction (Steps : in Natural) return Scalar;
Task: Continued fraction

C: 
#include <stdio.h>


typedef double (*coeff_func)(unsigned n);


double calc(coeff_func f_a, coeff_func f_b, unsigned expansions)
{
	double a, b, r;
	a = b = r = 0.0;

	unsigned i;
	for (i = expansions; i > 0; i--) {
		a = f_a(i);
		b = f_b(i);
		r = b / (a + r);
	}
	a = f_a(0);

	return a + r;
}


double sqrt2_a(unsigned n)
{
	return n ? 2.0 : 1.0;
}

double sqrt2_b(unsigned n)
{
	return 1.0;
}


double napier_a(unsigned n)
{
	return n ? n : 2.0;
}

double napier_b(unsigned n)
{
	return n > 1.0 ? n - 1.0 : 1.0;
}


double pi_a(unsigned n)
{
	return n ? 6.0 : 3.0;
}

double pi_b(unsigned n)
{
	double c = 2.0 * n - 1.0;

	return c * c;
}

int main(void)
{
	double sqrt2, napier, pi;

	sqrt2  = calc(sqrt2_a,  sqrt2_b,  1000);
	napier = calc(napier_a, napier_b, 1000);
	pi     = calc(pi_a,     pi_b,     1000);

	printf("%12.10g\n%12.10g\n%12.10g\n", sqrt2, napier, pi);

	return 0;
}
Task: Continued fraction



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

namespace ContinuedFraction {
    class Program {
        static double Calc(Func<int, int[]> f, int n) {
            double temp = 0.0;
            for (int ni = n; ni >= 1; ni--) {
                int[] p = f(ni);
                temp = p[1] / (p[0] + temp);
            }
            return f(0)[0] + temp;
        }

        static void Main(string[] args) {
            List<Func<int, int[]>> fList = new List<Func<int, int[]>>();
            fList.Add(n => new int[] { n > 0 ? 2 : 1, 1 });
            fList.Add(n => new int[] { n > 0 ? n : 2, n > 1 ? (n - 1) : 1 });
            fList.Add(n => new int[] { n > 0 ? 6 : 3, (int) Math.Pow(2 * n - 1, 2) });

            foreach (var f in fList) {
                Console.WriteLine(Calc(f, 200));
            }
        }
    }
}
Task: Continued fraction


C++: #include <iomanip>
#include <iostream>
#include <tuple>

typedef std::tuple<double,double> coeff_t; 
typedef coeff_t (*func_t)(int); 

double calc(func_t func, int n)
{
    double a, b, temp = 0;
    for (; n > 0; --n) {
        std::tie(a, b) = func(n);
        temp = b / (a + temp);
    }
    std::tie(a, b) = func(0);
    return a + temp;
}

coeff_t sqrt2(int n)
{
    return coeff_t(n > 0 ? 2 : 1, 1);
}

coeff_t napier(int n)
{
    return coeff_t(n > 0 ? n : 2, n > 1 ? n - 1 : 1);
}

coeff_t pi(int n)
{
    return coeff_t(n > 0 ? 6 : 3, (2 * n - 1) * (2 * n - 1));
}

int main()
{
    std::streamsize old_prec = std::cout.precision(15); 
    std::cout 
        << calc(sqrt2, 20) << '\n'
        << calc(napier, 15) << '\n'
        << calc(pi, 10000) << '\n'
        << std::setprecision(old_prec); 
}
Task: Continued fraction





Java: import static java.lang.Math.pow;
import java.util.*;
import java.util.function.Function;

public class Test {
    static double calc(Function<Integer, Integer[]> f, int n) {
        double temp = 0;

        for (int ni = n; ni >= 1; ni--) {
            Integer[] p = f.apply(ni);
            temp = p[1] / (double) (p[0] + temp);
        }
        return f.apply(0)[0] + temp;
    }

    public static void main(String[] args) {
        List<Function<Integer, Integer[]>> fList = new ArrayList<>();
        fList.add(n -> new Integer[]{n > 0 ? 2 : 1, 1});
        fList.add(n -> new Integer[]{n > 0 ? n : 2, n > 1 ? (n - 1) : 1});
        fList.add(n -> new Integer[]{n > 0 ? 6 : 3, (int) pow(2 * n - 1, 2)});

        for (Function<Integer, Integer[]> f : fList)
            System.out.println(calc(f, 200));
    }
}
Task: Continued fraction







Python: from fractions import Fraction
import itertools
try: zip = itertools.izip
except: pass
 

def CF(a, b, t):
  terms = list(itertools.islice(zip(a, b), t))
  z = Fraction(1,1)
  for a, b in reversed(terms):
    z = a + b / z
  return z
 

def pRes(x, d):
  q, x = divmod(x, 1)
  res = str(q)
  res += "."
  for i in range(d):
    x *= 10
    q, x = divmod(x, 1)
    res += str(q)
  return res
 

def sqrt2_a():
  yield 1
  for x in itertools.repeat(2):
    yield x
 
def sqrt2_b():
  for x in itertools.repeat(1):
    yield x
 
cf = CF(sqrt2_a(), sqrt2_b(), 950)
print(pRes(cf, 200))

 
 

def Napier_a():
  yield 2
  for x in itertools.count(1):
    yield x
 
def Napier_b():
  yield 1
  for x in itertools.count(1):
    yield x
 
cf = CF(Napier_a(), Napier_b(), 950)
print(pRes(cf, 200))

 

def Pi_a():
  yield 3
  for x in itertools.repeat(6):
    yield x
 
def Pi_b():
  for x in itertools.count(1,2):
    yield x*x
 
cf = CF(Pi_a(), Pi_b(), 950)
print(pRes(cf, 10))
Task: Continued fraction








VB: Public Const precision = 10000
Private Function continued_fraction(steps As Integer, rid_a As String, rid_b As String) As Double
    Dim res As Double
    res = 0
    For n = steps To 1 Step -1
       res = Application.Run(rid_b, n) / (Application.Run(rid_a, n) + res)
    Next n
    continued_fraction = Application.Run(rid_a, 0) + res
End Function
     
Function sqr2_a(n As Integer) As Integer
    sqr2_a = IIf(n = 0, 1, 2)
End Function

Function sqr2_b(n As Integer) As Integer
    sqr2_b = 1
End Function
 
Function nap_a(n As Integer) As Integer
    nap_a = IIf(n = 0, 2, n)
End Function

Function nap_b(n As Integer) As Integer
    nap_b = IIf(n = 1, 1, n - 1)
End Function
 
Function pi_a(n As Integer) As Integer
    pi_a = IIf(n = 0, 3, 6)
End Function

Function pi_b(n As Integer) As Long
    pi_b = IIf(n = 1, 1, (2 * n - 1) ^ 2)
End Function

Public Sub main()
    Debug.Print "Precision:", precision
    Debug.Print "Sqr(2):", continued_fraction(precision, "sqr2_a", "sqr2_b")
    Debug.Print "Napier:", continued_fraction(precision, "nap_a", "nap_b")
    Debug.Print "Pi:", continued_fraction(precision, "pi_a", "pi_b")
End Sub
Task: Continued fraction




Go: package main

import "fmt"

type cfTerm struct {
    a, b int
}



type cf []cfTerm

func cfSqrt2(nTerms int) cf {
    f := make(cf, nTerms)
    for n := range f {
        f[n] = cfTerm{2, 1}
    }
    f[0].a = 1
    return f
}

func cfNap(nTerms int) cf {
    f := make(cf, nTerms)
    for n := range f {
        f[n] = cfTerm{n, n - 1}
    }
    f[0].a = 2
    f[1].b = 1
    return f
}

func cfPi(nTerms int) cf {
    f := make(cf, nTerms)
    for n := range f {
        g := 2*n - 1
        f[n] = cfTerm{6, g * g}
    }
    f[0].a = 3
    return f
}

func (f cf) real() (r float64) {
    for n := len(f) - 1; n > 0; n-- {
        r = float64(f[n].b) / (float64(f[n].a) + r)
    }
    return r + float64(f[0].a)
}

func main() {
    fmt.Println("sqrt2:", cfSqrt2(20).real())
    fmt.Println("nap:  ", cfNap(20).real())
    fmt.Println("pi:   ", cfPi(20).real())
}
Task: Continued fraction



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

namespace ContinuedFraction {
    class Program {
        static double Calc(Func<int, int[]> f, int n) {
            double temp = 0.0;
            for (int ni = n; ni >= 1; ni--) {
                int[] p = f(ni);
                temp = p[1] / (p[0] + temp);
            }
            return f(0)[0] + temp;
        }

        static void Main(string[] args) {
            List<Func<int, int[]>> fList = new List<Func<int, int[]>>();
            fList.Add(n => new int[] { n > 0 ? 2 : 1, 1 });
            fList.Add(n => new int[] { n > 0 ? n : 2, n > 1 ? (n - 1) : 1 });
            fList.Add(n => new int[] { n > 0 ? 6 : 3, (int) Math.Pow(2 * n - 1, 2) });

            foreach (var f in fList) {
                Console.WriteLine(Calc(f, 200));
            }
        }
    }
}








Fortran: module continued_fractions
  implicit none
  
  integer, parameter :: long = selected_real_kind(7,99)

  type continued_fraction
    integer                            :: a0, b1
    procedure(series), pointer, nopass :: a, b
  end type

  interface
    pure function series (n)
      integer, intent(in) :: n
      integer             :: series
    end function
  end interface

contains

  pure function define_cf (a0,a,b1,b) result(x)
    integer, intent(in)           :: a0
    procedure(series)             :: a
    integer, intent(in), optional :: b1
    procedure(series),   optional :: b
    type(continued_fraction)      :: x
    x%a0 = a0
    x%a => a
    if ( present(b1) ) then
       x%b1 = b1
    else
       x%b1 = 1
    end if
    if ( present(b) ) then
       x%b => b
    else
       x%b => const_1
    end if
  end function define_cf

  pure integer function const_1(n)
    integer,intent(in) :: n
    const_1 = 1  
  end function

  pure real(kind=long) function output(x,iterations)
    type(continued_fraction), intent(in) :: x
    integer,                  intent(in) :: iterations
    integer                              :: i
    output = x%a(iterations)
    do i = iterations-1,1,-1
      output = x%a(i) + (x%b(i+1) / output)
    end do
    output = x%a0 + (x%b1 / output)
  end function output
  
end module continued_fractions


program examples
  use continued_fractions

  type(continued_fraction) :: sqr2,napier,pi

  sqr2   = define_cf(1,a_sqr2)
  napier = define_cf(2,a_napier,1,b_napier)
  pi     = define_cf(3,a=a_pi,b=b_pi)

  write (*,*) output(sqr2,10000)
  write (*,*) output(napier,10000)
  write (*,*) output(pi,10000)

contains

  pure integer function a_sqr2(n)
    integer,intent(in) :: n
    a_sqr2 = 2
  end function

  pure integer function a_napier(n)
    integer,intent(in) :: n
    a_napier = n
  end function

  pure integer function b_napier(n)
    integer,intent(in) :: n
    b_napier = n-1
  end function

  pure integer function a_pi(n)
    integer,intent(in) :: n
    a_pi = 6
  end function

  pure integer function b_pi(n)
    integer,intent(in) :: n
    b_pi = (2*n-1)*(2*n-1)
  end function

end program examples
Task: Continued fraction


C++: #include <iomanip>
#include <iostream>
#include <tuple>

typedef std::tuple<double,double> coeff_t; 
typedef coeff_t (*func_t)(int); 

double calc(func_t func, int n)
{
    double a, b, temp = 0;
    for (; n > 0; --n) {
        std::tie(a, b) = func(n);
        temp = b / (a + temp);
    }
    std::tie(a, b) = func(0);
    return a + temp;
}

coeff_t sqrt2(int n)
{
    return coeff_t(n > 0 ? 2 : 1, 1);
}

coeff_t napier(int n)
{
    return coeff_t(n > 0 ? n : 2, n > 1 ? n - 1 : 1);
}

coeff_t pi(int n)
{
    return coeff_t(n > 0 ? 6 : 3, (2 * n - 1) * (2 * n - 1));
}

int main()
{
    std::streamsize old_prec = std::cout.precision(15); 
    std::cout 
        << calc(sqrt2, 20) << '\n'
        << calc(napier, 15) << '\n'
        << calc(pi, 10000) << '\n'
        << std::setprecision(old_prec); 
}









Fortran: module continued_fractions
  implicit none
  
  integer, parameter :: long = selected_real_kind(7,99)

  type continued_fraction
    integer                            :: a0, b1
    procedure(series), pointer, nopass :: a, b
  end type

  interface
    pure function series (n)
      integer, intent(in) :: n
      integer             :: series
    end function
  end interface

contains

  pure function define_cf (a0,a,b1,b) result(x)
    integer, intent(in)           :: a0
    procedure(series)             :: a
    integer, intent(in), optional :: b1
    procedure(series),   optional :: b
    type(continued_fraction)      :: x
    x%a0 = a0
    x%a => a
    if ( present(b1) ) then
       x%b1 = b1
    else
       x%b1 = 1
    end if
    if ( present(b) ) then
       x%b => b
    else
       x%b => const_1
    end if
  end function define_cf

  pure integer function const_1(n)
    integer,intent(in) :: n
    const_1 = 1  
  end function

  pure real(kind=long) function output(x,iterations)
    type(continued_fraction), intent(in) :: x
    integer,                  intent(in) :: iterations
    integer                              :: i
    output = x%a(iterations)
    do i = iterations-1,1,-1
      output = x%a(i) + (x%b(i+1) / output)
    end do
    output = x%a0 + (x%b1 / output)
  end function output
  
end module continued_fractions


program examples
  use continued_fractions

  type(continued_fraction) :: sqr2,napier,pi

  sqr2   = define_cf(1,a_sqr2)
  napier = define_cf(2,a_napier,1,b_napier)
  pi     = define_cf(3,a=a_pi,b=b_pi)

  write (*,*) output(sqr2,10000)
  write (*,*) output(napier,10000)
  write (*,*) output(pi,10000)

contains

  pure integer function a_sqr2(n)
    integer,intent(in) :: n
    a_sqr2 = 2
  end function

  pure integer function a_napier(n)
    integer,intent(in) :: n
    a_napier = n
  end function

  pure integer function b_napier(n)
    integer,intent(in) :: n
    b_napier = n-1
  end function

  pure integer function a_pi(n)
    integer,intent(in) :: n
    a_pi = 6
  end function

  pure integer function b_pi(n)
    integer,intent(in) :: n
    b_pi = (2*n-1)*(2*n-1)
  end function

end program examples