WeixiangYan/CodeTransOcean huggingface dataset:
Task: Emirp primes







Python: from __future__ import print_function
from prime_decomposition import primes, is_prime
from heapq import *
from itertools import islice

def emirp():
    largest = set()
    emirps = []
    heapify(emirps)
    for pr in primes():
        while emirps and pr > emirps[0]:
            yield heappop(emirps)
        if pr in largest:
            yield pr
        else:
            rp = int(str(pr)[::-1])
            if rp > pr and is_prime(rp):
                heappush(emirps, pr)
                largest.add(rp)

print('First 20:\n  ', list(islice(emirp(), 20)))
print('Between 7700 and 8000:\n  [', end='')
for pr in emirp():
    if pr >= 8000: break
    if pr >= 7700: print(pr, end=', ')
print(']')
print('10000th:\n  ', list(islice(emirp(), 10000-1, 10000)))
Task: Emirp primes




Go: package main

import (
	"flag"
	"fmt"
	"github.com/jbarham/primegen.go" 
	"math"
)



type primeCache struct {
	gen    *primegen.Primegen
	primes []uint64
}

func NewPrimeCache() primeCache {
	g := primegen.New()
	return primeCache{gen: g, primes: []uint64{g.Next()}}
}



func (pc *primeCache) upto(n uint64) []uint64 {
	if p := pc.primes[len(pc.primes)-1]; p <= n {
		for p <= n {
			p = pc.gen.Next()
			pc.primes = append(pc.primes, p)
		}
		return pc.primes[:len(pc.primes)-1]
	}
	for i, p := range pc.primes {
		if p > n {
			return pc.primes[:i]
		}
	}
	panic("not reached")
}

var cache = NewPrimeCache()

func sqrt(x uint64) uint64 { return uint64(math.Sqrt(float64(x))) }



func isprime(n uint64) bool {
	for _, p := range cache.upto(sqrt(n)) {
		if n%p == 0 {
			return false
		}
	}
	return true
}

func reverse(n uint64) (r uint64) {
	for n > 0 {
		r = 10*r + n%10
		n /= 10
	}
	return
}


func isEmirp(n uint64) bool {
	r := reverse(n)
	return r != n && isprime(r)
}


type EmirpGen struct {
	pgen     *primegen.Primegen
	nextn    uint64
	r1l, r1h uint64
	r2l, r2h uint64
	r3l, r3h uint64
}

func NewEmirpGen() *EmirpGen {
	e := &EmirpGen{pgen: primegen.New()}
	e.Reset()
	return e
}

func (e *EmirpGen) Reset() {
	e.pgen.Reset()
	e.nextn = 0
	
	e.r1l, e.r1h = 20, 30
	e.r2l, e.r2h = 40, 70
	e.r3l, e.r3h = 80, 90
}

func (e *EmirpGen) next() (n uint64) {
	for n = e.pgen.Next(); !isEmirp(n); n = e.pgen.Next() {
		
		
		switch {
		case e.r1l <= n && n < e.r1h:
			e.pgen.SkipTo(e.r1h)
		case e.r2l <= n && n < e.r2h:
			e.pgen.SkipTo(e.r2h)
		case e.r3l <= n && n < e.r3h:
			e.pgen.SkipTo(e.r3h)
		case n > e.r3h:
			e.r1l *= 10
			e.r1h *= 10
			e.r2l *= 10
			e.r2h *= 10
			e.r3l *= 10
			e.r3h *= 10
		}
	}
	return
}

func (e *EmirpGen) Next() (n uint64) {
	if n = e.nextn; n != 0 {
		e.nextn = 0
		return
	}
	return e.next()
}

func (e *EmirpGen) Peek() uint64 {
	if e.nextn == 0 {
		e.nextn = e.next()
	}
	return e.nextn
}

func (e *EmirpGen) SkipTo(nn uint64) {
	e.pgen.SkipTo(nn)
	e.nextn = 0
	return
}



type SequenceGen interface {
	Next() uint64
	Peek() uint64
	Reset()
	SkipTo(uint64)
	
}

func main() {
	var start, end uint64
	var n, skip uint
	var oneline, primes bool
	flag.UintVar(&n, "n", math.MaxUint64, "number of emirps to print")
	flag.UintVar(&skip, "skip", 0, "number of emirps to skip")
	flag.Uint64Var(&start, "start", 0, "start at x>=start")
	flag.Uint64Var(&end, "end", math.MaxUint64, "stop at x<=end")
	flag.BoolVar(&oneline, "oneline", false, "output on a single line")
	flag.BoolVar(&primes, "primes", false, "generate primes rather than emirps")
	flag.Parse()

	sep := "\n"
	if oneline {
		sep = " "
	}

	
	var seq SequenceGen
	if primes {
		seq = primegen.New()
	} else {
		seq = NewEmirpGen()
	}

	for seq.Peek() < start {
		seq.Next()
	}
	for ; skip > 0; skip-- {
		seq.Next()
	}
	for ; n > 0 && seq.Peek() <= end; n-- {
		fmt.Print(seq.Next(), sep)
	}
	if oneline {
		fmt.Println()
	}
}
Task: Emirp primes






PHP: <?php

function is_prime($n) {
    if ($n <= 3) {
        return $n > 1;
    } elseif (($n % 2 == 0) or ($n % 3 == 0)) {
        return false;
    }
    $i = 5;
    while ($i * $i <= $n) {
        if ($n % $i == 0) {
            return false;
        }
        $i += 2;
        if ($n % $i == 0) {
            return false;
        }
        $i += 4;
    }
    return true;
}

function is_emirp($n) {
    $r = (int) strrev((string) $n);
    return (($r != $n) and is_prime($r) and is_prime($n));
}

$c = $x = 0;
$first20 = $between = '';
do {
    $x++;
    if (is_emirp($x)) {
        $c++;
        if ($c <= 20) {
            $first20 .= $x . ' ';
        }
        if (7700 <= $x and $x <= 8000) {
            $between .= $x . ' ';
        }
    }
} while ($c < 10000);

echo
    'First twenty emirps :', PHP_EOL, $first20, PHP_EOL,
    'Emirps between 7,700 and 8,000 :', PHP_EOL, $between, PHP_EOL,
    'The 10,000th emirp :', PHP_EOL, $x, PHP_EOL;






Ada: with Ada.Text_IO, Miller_Rabin;

procedure Emirp_Gen is
   
   type Num is range 0 .. 2**63-1; 
   
   MR_Iterations: constant Positive := 25; 
     
     
   
   function Is_Emirp(E: Num) return Boolean is
      package MR is new Miller_Rabin(Num); use MR;
      
      function Rev(E: Num) return Num is
	 N: Num := E;
	 R: Num := 0;
      begin
	 while N > 0 loop
	    R := 10*R + N mod 10; 
	    N := N / 10;          
	 end loop;
	 return R;
      end Rev;
      
      R: Num := Rev(E); 
   begin
      return E /= R and then
	     (Is_Prime(E, MR_Iterations) = Probably_Prime) and then
	     (Is_Prime(R, MR_Iterations) = Probably_Prime);
   end Is_Emirp;
	
   function Next(P: Num) return Num is
      N: Num := P+1;
   begin
      while not (Is_Emirp(N)) Loop
	 N := N + 1;
      end loop;
      return N;
   end Next;
   
   Current: Num;
   Count: Num := 0;
   
begin
   
   Ada.Text_IO.Put("First 20 emirps:");
   Current := 1;
   for I in 1 .. 20 loop
      Current := Next(Current);
      Ada.Text_IO.Put(Num'Image(Current));
   end loop;
   Ada.Text_IO.New_Line;
   
   
   Ada.Text_IO.Put("Emirps between 7700 and 8000:");
   Current := 7699;
   loop
      Current := Next(Current);
      exit when Current > 8000;
       Ada.Text_IO.Put(Num'Image(Current));
   end loop;
   
   
   Ada.Text_IO.Put("The 10_000'th emirp:");
   for I in 1 .. 10_000 loop
      Current := Next(Current);
   end loop;
   Ada.Text_IO.Put_Line(Num'Image(Current));
end Emirp_Gen;
Task: Emirp primes






PHP: <?php

function is_prime($n) {
    if ($n <= 3) {
        return $n > 1;
    } elseif (($n % 2 == 0) or ($n % 3 == 0)) {
        return false;
    }
    $i = 5;
    while ($i * $i <= $n) {
        if ($n % $i == 0) {
            return false;
        }
        $i += 2;
        if ($n % $i == 0) {
            return false;
        }
        $i += 4;
    }
    return true;
}

function is_emirp($n) {
    $r = (int) strrev((string) $n);
    return (($r != $n) and is_prime($r) and is_prime($n));
}

$c = $x = 0;
$first20 = $between = '';
do {
    $x++;
    if (is_emirp($x)) {
        $c++;
        if ($c <= 20) {
            $first20 .= $x . ' ';
        }
        if (7700 <= $x and $x <= 8000) {
            $between .= $x . ' ';
        }
    }
} while ($c < 10000);

echo
    'First twenty emirps :', PHP_EOL, $first20, PHP_EOL,
    'Emirps between 7,700 and 8,000 :', PHP_EOL, $between, PHP_EOL,
    'The 10,000th emirp :', PHP_EOL, $x, PHP_EOL;
Task: Continued fraction_Arithmetic_Construct from rational number



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

class Program
{
    static IEnumerable<int> r2cf(int n1, int n2)
    {
        while (Math.Abs(n2) > 0)
        {
            int t1 = n1 / n2;
            int t2 = n2;
            n2 = n1 - t1 * n2;
            n1 = t2;
            yield return t1;
        }
    }

    static void spit(IEnumerable<int> f)
    {
        foreach (int n in f) Console.Write(" {0}", n);
        Console.WriteLine();
    }

    static void Main(string[] args)
    {
        spit(r2cf(1, 2));
        spit(r2cf(3, 1));
        spit(r2cf(23, 8));
        spit(r2cf(13, 11));
        spit(r2cf(22, 7));
        spit(r2cf(-151, 77));
        for (int scale = 10; scale <= 10000000; scale *= 10)
        {
            spit(r2cf((int)(Math.Sqrt(2) * scale), scale));
        }
        spit(r2cf(31, 10));
        spit(r2cf(314, 100)); 
        spit(r2cf(3142, 1000));
        spit(r2cf(31428, 10000));
        spit(r2cf(314285, 100000));
        spit(r2cf(3142857, 1000000));
        spit(r2cf(31428571, 10000000));
        spit(r2cf(314285714, 100000000));
    }
}









Ada: with ada.text_io; use ada.text_io;
with ada.strings; use ada.strings;
with ada.strings.fixed; use ada.strings.fixed;

procedure continued_fraction_from_rational is

  
  
  function r2cf (N1 : in out integer;
                 N2 : in out integer)
    return integer
  is
    q, r : integer;
  begin
    
    
    
    r := N1 mod N2;
    q := (N1 - r) / N2;

    N1 := N2;
    N2 := r;

    return q;
  end r2cf;

  procedure write_r2cf (N1 : in integer;
                        N2 : in integer)
  is
    M1, M2 : integer;
    digit : integer;
    sep : integer;
  begin
    put (trim (integer'image (N1), left));
    put ("/");
    put (trim (integer'image (N2), left));
    put (" => ");

    M1 := N1;
    M2 := N2;
    sep := 0;
    while M2 /= 0 loop
      digit := r2cf (M1, M2);
      if sep = 0 then
        put ("[");
        sep := 1;
      elsif sep = 1 then
        put ("; ");
        sep := 2;
      else
        put (", ");
      end if;
      put (trim (integer'image (digit), left));
    end loop;
    put_line ("]");
  end write_r2cf;

begin

  write_r2cf (1, 2);
  write_r2cf (3, 1);
  write_r2cf (23, 8);
  write_r2cf (13, 11);
  write_r2cf (22, 7);
  write_r2cf (-151, 77);

  write_r2cf (14142, 10000);
  write_r2cf (141421, 100000);
  write_r2cf (1414214, 1000000);
  write_r2cf (14142136, 10000000);

  write_r2cf (31, 10);
  write_r2cf (314, 100);
  write_r2cf (3142, 1000);
  write_r2cf (31428, 10000);
  write_r2cf (314285, 100000);
  write_r2cf (3142857, 1000000);
  write_r2cf (31428571, 10000000);
  write_r2cf (314285714, 100000000);

end continued_fraction_from_rational;
Task: Continued fraction_Arithmetic_Construct from rational number

C: #include<stdio.h>

typedef struct{
	int num,den;
	}fraction;

fraction examples[] = {{1,2}, {3,1}, {23,8}, {13,11}, {22,7}, {-151,77}}; 
fraction sqrt2[] = {{14142,10000}, {141421,100000}, {1414214,1000000}, {14142136,10000000}};
fraction pi[] = {{31,10}, {314,100}, {3142,1000}, {31428,10000}, {314285,100000}, {3142857,1000000}, {31428571,10000000}, {314285714,100000000}};

int r2cf(int *numerator,int *denominator)
{
	int quotient=0,temp;
	
	if(denominator != 0)
	{
		quotient = *numerator / *denominator;
		
		temp = *numerator;
		
		*numerator = *denominator;
		
		*denominator = temp % *denominator;
	}
	
	return quotient;
}

int main()
{
	int i;
	
	printf("Running the examples :");
	
	for(i=0;i<sizeof(examples)/sizeof(fraction);i++)
	{
		printf("\nFor N = %d, D = %d :",examples[i].num,examples[i].den);
		
		while(examples[i].den != 0){
			printf(" %d ",r2cf(&examples[i].num,&examples[i].den));
			}
	}
	
	printf("\n\nRunning for %c2 :",251); 
	
	for(i=0;i<sizeof(sqrt2)/sizeof(fraction);i++)
	{
		printf("\nFor N = %d, D = %d :",sqrt2[i].num,sqrt2[i].den);
		
		while(sqrt2[i].den != 0){
			printf(" %d ",r2cf(&sqrt2[i].num,&sqrt2[i].den));
			}
	}
	
	printf("\n\nRunning for %c :",227); 
	
	for(i=0;i<sizeof(pi)/sizeof(fraction);i++)
	{
		printf("\nFor N = %d, D = %d :",pi[i].num,pi[i].den);
		
		while(pi[i].den != 0){
			printf(" %d ",r2cf(&pi[i].num,&pi[i].den));
			}
	}
	
	
	
	return 0;
}











Ada: with ada.text_io; use ada.text_io;
with ada.strings; use ada.strings;
with ada.strings.fixed; use ada.strings.fixed;

procedure continued_fraction_from_rational is

  
  
  function r2cf (N1 : in out integer;
                 N2 : in out integer)
    return integer
  is
    q, r : integer;
  begin
    
    
    
    r := N1 mod N2;
    q := (N1 - r) / N2;

    N1 := N2;
    N2 := r;

    return q;
  end r2cf;

  procedure write_r2cf (N1 : in integer;
                        N2 : in integer)
  is
    M1, M2 : integer;
    digit : integer;
    sep : integer;
  begin
    put (trim (integer'image (N1), left));
    put ("/");
    put (trim (integer'image (N2), left));
    put (" => ");

    M1 := N1;
    M2 := N2;
    sep := 0;
    while M2 /= 0 loop
      digit := r2cf (M1, M2);
      if sep = 0 then
        put ("[");
        sep := 1;
      elsif sep = 1 then
        put ("; ");
        sep := 2;
      else
        put (", ");
      end if;
      put (trim (integer'image (digit), left));
    end loop;
    put_line ("]");
  end write_r2cf;

begin

  write_r2cf (1, 2);
  write_r2cf (3, 1);
  write_r2cf (23, 8);
  write_r2cf (13, 11);
  write_r2cf (22, 7);
  write_r2cf (-151, 77);

  write_r2cf (14142, 10000);
  write_r2cf (141421, 100000);
  write_r2cf (1414214, 1000000);
  write_r2cf (14142136, 10000000);

  write_r2cf (31, 10);
  write_r2cf (314, 100);
  write_r2cf (3142, 1000);
  write_r2cf (31428, 10000);
  write_r2cf (314285, 100000);
  write_r2cf (3142857, 1000000);
  write_r2cf (31428571, 10000000);
  write_r2cf (314285714, 100000000);

end continued_fraction_from_rational;
Task: Continued fraction_Arithmetic_Construct from rational number


C++: #include <iostream>

class ContinuedFraction {
	public:
	virtual const int nextTerm(){};
	virtual const bool moreTerms(){};
};

class r2cf : public ContinuedFraction {
	private: int n1, n2;
	public:
	r2cf(const int numerator, const int denominator): n1(numerator), n2(denominator){}
	const int nextTerm() {
		const int thisTerm = n1/n2;
		const int t2 = n2; n2 = n1 - thisTerm * n2; n1 = t2;
		return thisTerm;
	}
	const bool moreTerms() {return fabs(n2) > 0;}
};

class SQRT2 : public ContinuedFraction {
	private: bool first=true;
	public:
	const int nextTerm() {if (first) {first = false; return 1;} else return 2;}
	const bool moreTerms() {return true;}
};










Ada: with ada.text_io; use ada.text_io;
with ada.strings; use ada.strings;
with ada.strings.fixed; use ada.strings.fixed;

procedure continued_fraction_from_rational is

  
  
  function r2cf (N1 : in out integer;
                 N2 : in out integer)
    return integer
  is
    q, r : integer;
  begin
    
    
    
    r := N1 mod N2;
    q := (N1 - r) / N2;

    N1 := N2;
    N2 := r;

    return q;
  end r2cf;

  procedure write_r2cf (N1 : in integer;
                        N2 : in integer)
  is
    M1, M2 : integer;
    digit : integer;
    sep : integer;
  begin
    put (trim (integer'image (N1), left));
    put ("/");
    put (trim (integer'image (N2), left));
    put (" => ");

    M1 := N1;
    M2 := N2;
    sep := 0;
    while M2 /= 0 loop
      digit := r2cf (M1, M2);
      if sep = 0 then
        put ("[");
        sep := 1;
      elsif sep = 1 then
        put ("; ");
        sep := 2;
      else
        put (", ");
      end if;
      put (trim (integer'image (digit), left));
    end loop;
    put_line ("]");
  end write_r2cf;

begin

  write_r2cf (1, 2);
  write_r2cf (3, 1);
  write_r2cf (23, 8);
  write_r2cf (13, 11);
  write_r2cf (22, 7);
  write_r2cf (-151, 77);

  write_r2cf (14142, 10000);
  write_r2cf (141421, 100000);
  write_r2cf (1414214, 1000000);
  write_r2cf (14142136, 10000000);

  write_r2cf (31, 10);
  write_r2cf (314, 100);
  write_r2cf (3142, 1000);
  write_r2cf (31428, 10000);
  write_r2cf (314285, 100000);
  write_r2cf (3142857, 1000000);
  write_r2cf (31428571, 10000000);
  write_r2cf (314285714, 100000000);

end continued_fraction_from_rational;
Task: Continued fraction_Arithmetic_Construct from rational number




Go: package cf

import (
	"fmt"
	"strings"
)


type ContinuedFraction func() NextFn



type NextFn func() (term int64, ok bool)




func (cf ContinuedFraction) String() string {
	var buf strings.Builder
	buf.WriteByte('[')
	sep := "; "
	const maxTerms = 20
	next := cf()
	for n := 0; ; n++ {
		t, ok := next()
		if !ok {
			break
		}
		if n > 0 {
			buf.WriteString(sep)
			sep = ", "
		}
		if n >= maxTerms {
			buf.WriteString("...")
			break
		}
		fmt.Fprint(&buf, t)
	}
	buf.WriteByte(']')
	return buf.String()
}


func Sqrt2() NextFn {
	first := true
	return func() (int64, bool) {
		if first {
			first = false
			return 1, true
		}
		return 2, true
	}
}


func Phi() NextFn {
	return func() (int64, bool) { return 1, true }
}



func E() NextFn {
	var i int
	return func() (int64, bool) {
		i++
		switch {
		case i == 1:
			return 2, true
		case i%3 == 0:
			return int64(i/3) * 2, true
		default:
			return 1, true
		}
	}
}








Ada: with ada.text_io; use ada.text_io;
with ada.strings; use ada.strings;
with ada.strings.fixed; use ada.strings.fixed;

procedure continued_fraction_from_rational is

  
  
  function r2cf (N1 : in out integer;
                 N2 : in out integer)
    return integer
  is
    q, r : integer;
  begin
    
    
    
    r := N1 mod N2;
    q := (N1 - r) / N2;

    N1 := N2;
    N2 := r;

    return q;
  end r2cf;

  procedure write_r2cf (N1 : in integer;
                        N2 : in integer)
  is
    M1, M2 : integer;
    digit : integer;
    sep : integer;
  begin
    put (trim (integer'image (N1), left));
    put ("/");
    put (trim (integer'image (N2), left));
    put (" => ");

    M1 := N1;
    M2 := N2;
    sep := 0;
    while M2 /= 0 loop
      digit := r2cf (M1, M2);
      if sep = 0 then
        put ("[");
        sep := 1;
      elsif sep = 1 then
        put ("; ");
        sep := 2;
      else
        put (", ");
      end if;
      put (trim (integer'image (digit), left));
    end loop;
    put_line ("]");
  end write_r2cf;

begin

  write_r2cf (1, 2);
  write_r2cf (3, 1);
  write_r2cf (23, 8);
  write_r2cf (13, 11);
  write_r2cf (22, 7);
  write_r2cf (-151, 77);

  write_r2cf (14142, 10000);
  write_r2cf (141421, 100000);
  write_r2cf (1414214, 1000000);
  write_r2cf (14142136, 10000000);

  write_r2cf (31, 10);
  write_r2cf (314, 100);
  write_r2cf (3142, 1000);
  write_r2cf (31428, 10000);
  write_r2cf (314285, 100000);
  write_r2cf (3142857, 1000000);
  write_r2cf (31428571, 10000000);
  write_r2cf (314285714, 100000000);

end continued_fraction_from_rational;
Task: Continued fraction_Arithmetic_Construct from rational number







Python: def r2cf(n1,n2):
  while n2:
    n1, (t1, n2) = n2, divmod(n1, n2)
    yield t1

print(list(r2cf(1,2)))    
print(list(r2cf(3,1)))    
print(list(r2cf(23,8)))    
print(list(r2cf(13,11)))    
print(list(r2cf(22,7)))    
print(list(r2cf(14142,10000)))    
print(list(r2cf(141421,100000)))    
print(list(r2cf(1414214,1000000)))    
print(list(r2cf(14142136,10000000)))    





Ada: with ada.text_io; use ada.text_io;
with ada.strings; use ada.strings;
with ada.strings.fixed; use ada.strings.fixed;

procedure continued_fraction_from_rational is

  
  
  function r2cf (N1 : in out integer;
                 N2 : in out integer)
    return integer
  is
    q, r : integer;
  begin
    
    
    
    r := N1 mod N2;
    q := (N1 - r) / N2;

    N1 := N2;
    N2 := r;

    return q;
  end r2cf;

  procedure write_r2cf (N1 : in integer;
                        N2 : in integer)
  is
    M1, M2 : integer;
    digit : integer;
    sep : integer;
  begin
    put (trim (integer'image (N1), left));
    put ("/");
    put (trim (integer'image (N2), left));
    put (" => ");

    M1 := N1;
    M2 := N2;
    sep := 0;
    while M2 /= 0 loop
      digit := r2cf (M1, M2);
      if sep = 0 then
        put ("[");
        sep := 1;
      elsif sep = 1 then
        put ("; ");
        sep := 2;
      else
        put (", ");
      end if;
      put (trim (integer'image (digit), left));
    end loop;
    put_line ("]");
  end write_r2cf;

begin

  write_r2cf (1, 2);
  write_r2cf (3, 1);
  write_r2cf (23, 8);
  write_r2cf (13, 11);
  write_r2cf (22, 7);
  write_r2cf (-151, 77);

  write_r2cf (14142, 10000);
  write_r2cf (141421, 100000);
  write_r2cf (1414214, 1000000);
  write_r2cf (14142136, 10000000);

  write_r2cf (31, 10);
  write_r2cf (314, 100);
  write_r2cf (3142, 1000);
  write_r2cf (31428, 10000);
  write_r2cf (314285, 100000);
  write_r2cf (3142857, 1000000);
  write_r2cf (31428571, 10000000);
  write_r2cf (314285714, 100000000);

end continued_fraction_from_rational;
Task: Continued fraction_Arithmetic_Construct from rational number

C: #include<stdio.h>

typedef struct{
	int num,den;
	}fraction;

fraction examples[] = {{1,2}, {3,1}, {23,8}, {13,11}, {22,7}, {-151,77}}; 
fraction sqrt2[] = {{14142,10000}, {141421,100000}, {1414214,1000000}, {14142136,10000000}};
fraction pi[] = {{31,10}, {314,100}, {3142,1000}, {31428,10000}, {314285,100000}, {3142857,1000000}, {31428571,10000000}, {314285714,100000000}};

int r2cf(int *numerator,int *denominator)
{
	int quotient=0,temp;
	
	if(denominator != 0)
	{
		quotient = *numerator / *denominator;
		
		temp = *numerator;
		
		*numerator = *denominator;
		
		*denominator = temp % *denominator;
	}
	
	return quotient;
}

int main()
{
	int i;
	
	printf("Running the examples :");
	
	for(i=0;i<sizeof(examples)/sizeof(fraction);i++)
	{
		printf("\nFor N = %d, D = %d :",examples[i].num,examples[i].den);
		
		while(examples[i].den != 0){
			printf(" %d ",r2cf(&examples[i].num,&examples[i].den));
			}
	}
	
	printf("\n\nRunning for %c2 :",251); 
	
	for(i=0;i<sizeof(sqrt2)/sizeof(fraction);i++)
	{
		printf("\nFor N = %d, D = %d :",sqrt2[i].num,sqrt2[i].den);
		
		while(sqrt2[i].den != 0){
			printf(" %d ",r2cf(&sqrt2[i].num,&sqrt2[i].den));
			}
	}
	
	printf("\n\nRunning for %c :",227); 
	
	for(i=0;i<sizeof(pi)/sizeof(fraction);i++)
	{
		printf("\nFor N = %d, D = %d :",pi[i].num,pi[i].den);
		
		while(pi[i].den != 0){
			printf(" %d ",r2cf(&pi[i].num,&pi[i].den));
			}
	}
	
	
	
	return 0;
}
Task: Continued fraction_Arithmetic_Construct from rational number



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

class Program
{
    static IEnumerable<int> r2cf(int n1, int n2)
    {
        while (Math.Abs(n2) > 0)
        {
            int t1 = n1 / n2;
            int t2 = n2;
            n2 = n1 - t1 * n2;
            n1 = t2;
            yield return t1;
        }
    }

    static void spit(IEnumerable<int> f)
    {
        foreach (int n in f) Console.Write(" {0}", n);
        Console.WriteLine();
    }

    static void Main(string[] args)
    {
        spit(r2cf(1, 2));
        spit(r2cf(3, 1));
        spit(r2cf(23, 8));
        spit(r2cf(13, 11));
        spit(r2cf(22, 7));
        spit(r2cf(-151, 77));
        for (int scale = 10; scale <= 10000000; scale *= 10)
        {
            spit(r2cf((int)(Math.Sqrt(2) * scale), scale));
        }
        spit(r2cf(31, 10));
        spit(r2cf(314, 100)); 
        spit(r2cf(3142, 1000));
        spit(r2cf(31428, 10000));
        spit(r2cf(314285, 100000));
        spit(r2cf(3142857, 1000000));
        spit(r2cf(31428571, 10000000));
        spit(r2cf(314285714, 100000000));
    }
}
Task: Continued fraction_Arithmetic_Construct from rational number


C++: #include <iostream>

class ContinuedFraction {
	public:
	virtual const int nextTerm(){};
	virtual const bool moreTerms(){};
};

class r2cf : public ContinuedFraction {
	private: int n1, n2;
	public:
	r2cf(const int numerator, const int denominator): n1(numerator), n2(denominator){}
	const int nextTerm() {
		const int thisTerm = n1/n2;
		const int t2 = n2; n2 = n1 - thisTerm * n2; n1 = t2;
		return thisTerm;
	}
	const bool moreTerms() {return fabs(n2) > 0;}
};

class SQRT2 : public ContinuedFraction {
	private: bool first=true;
	public:
	const int nextTerm() {if (first) {first = false; return 1;} else return 2;}
	const bool moreTerms() {return true;}
};