Task: Continued fraction_Arithmetic_Construct from rational number





Java: import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ConstructFromRationalNumber {
    private static class R2cf implements Iterator<Integer> {
        private int num;
        private int den;

        R2cf(int num, int den) {
            this.num = num;
            this.den = den;
        }

        @Override
        public boolean hasNext() {
            return den != 0;
        }

        @Override
        public Integer next() {
            int div = num / den;
            int rem = num % den;
            num = den;
            den = rem;
            return div;
        }
    }

    private static void iterate(R2cf generator) {
        generator.forEachRemaining(n -> System.out.printf("%d ", n));
        System.out.println();
    }

    public static void main(String[] args) {
        List<Map.Entry<Integer, Integer>> fracs = List.of(
                Map.entry(1, 2),
                Map.entry(3, 1),
                Map.entry(23, 8),
                Map.entry(13, 11),
                Map.entry(22, 7),
                Map.entry(-151, 77)
        );
        for (Map.Entry<Integer, Integer> frac : fracs) {
            System.out.printf("%4d / %-2d = ", frac.getKey(), frac.getValue());
            iterate(new R2cf(frac.getKey(), frac.getValue()));
        }

        System.out.println("\nSqrt(2) ->");
        List<Map.Entry<Integer, Integer>> root2 = List.of(
                Map.entry(    14_142,     10_000),
                Map.entry(   141_421,    100_000),
                Map.entry( 1_414_214,  1_000_000),
                Map.entry(14_142_136, 10_000_000)
        );
        for (Map.Entry<Integer, Integer> frac : root2) {
            System.out.printf("%8d / %-8d = ", frac.getKey(), frac.getValue());
            iterate(new R2cf(frac.getKey(), frac.getValue()));
        }

        System.out.println("\nPi ->");
        List<Map.Entry<Integer, Integer>> pi = List.of(
                Map.entry(         31,        10),
                Map.entry(        314,       100),
                Map.entry(      3_142,      1_000),
                Map.entry(     31_428,     10_000),
                Map.entry(    314_285,    100_000),
                Map.entry(  3_142_857,   1_000_000),
                Map.entry( 31_428_571,  10_000_000),
                Map.entry(314_285_714, 100_000_000)
        );
        for (Map.Entry<Integer, Integer> frac : pi) {
            System.out.printf("%9d / %-9d = ", frac.getKey(), frac.getValue());
            iterate(new R2cf(frac.getKey(), frac.getValue()));
        }
    }
}







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;