public class Java0101 {
     public static void main(String []args) {
        System.out.println(3 + 5);
        System.out.println(3 - 5);
        System.out.println(3 * 5);
        System.out.println(Math.pow(3, 5));
        System.out.println(5 / 3);
        System.out.println(5.0 / 3);
        System.out.println(5 / 3.0);
        System.out.println(5 % 3);

        System.out.print(3 * 5 + "\n");

        System.out.println(String.format("%3d",     3 * 5));
        System.out.println(String.format("%23.20f", 5 / 3.0));
    }
}
public class Java0102 {
     public static void main(String []args) {
        int i = 3 * 5;
        System.out.println("3 * 5 = " + i);
        System.out.printf("3 * 5 = %d\n", i);
     }
}
public class Java0103 {
     public static void main(String []args) {
        for (int i = 1; i < 10; i++) {
            System.out.print(i + ", ");
        }
        System.out.println();
     }
}
public class Java0104 {
     public static void main(String []args) {
        for (int i = 1; i < 10; i++) {
            if (i % 3 == 0) {
                System.out.print(i + ", ");
            }
        }
        System.out.println();
     }
}
public class Java0105 {
     public static void main(String []args) {
        int sum = 0;
        for (int i = 1; i < 100; i++) {
            if (i % 3 == 0) {
                sum += i;
            }
        }
        System.out.println(sum);
     }
}
public class Java0301 {
     public static void main(String []args) {
        // 3 の倍数の合計
        System.out.println( sn(3, 999) );
     }
    // 初項:a, 公差:a で, 上限:lim の数列の総和を返す関数
    private static int sn(int a, int lim) {
        int n = lim / a;        // 項数:n  =  上限:lim / 公差:a
        int l = n * a;          // 末項:l  =  項数:n   * 公差:a
        return (a + l) * n / 2; // 総和:sn = (初項:a   + 末項:l) * 項数:n / 2
    }
}
public class Java0302{
     public static void main(String []args){
        // 10000 までの 自然数の和
        // 項目数 n = 10000
        int n = 10000;
        System.out.println( n * (n + 1) / 2 );
    }
}
public class Java0303{
     public static void main(String []args){
        // 10000 までの 偶数の和
        // 項目数 n = 5000
        int n = 10000 / 2;
        System.out.println( n * (n + 1) );
    }
}
public class Java0304{
     public static void main(String []args){
        // 10000 までの 奇数の和
        // 項目数 n = 5000
        int n = 10000 / 2;
        System.out.println((int) Math.pow(n, 2) );
    }
}
public class Java0305{
     public static void main(String []args){
        // 1000 までの 自然数の2乗の和
        int n = 1000;
        System.out.println( n * (n + 1) * (2 * n + 1) / 6 );
    }
}
public class Java0306{
     public static void main(String []args){
        // 100 までの 自然数の3乗の和
        int n = 100;
        System.out.println((int) (Math.pow(n, 2) * Math.pow((n + 1), 2) / 4 ) );
    }
}
public class Java0307{
     public static void main(String []args){
        // 初項 2, 公比 3, 項数 10 の等比数列の和
        int n = 10;
        int a = 2;
        int r = 3;
        System.out.println((int) (a * (Math.pow(r, n) - 1)) / (r - 1) );
    }
}
public class Java0401 {
    public static void main(String []args) {
        int a = 5;   // 初項 5
        int d = 3;   // 公差 3
        int n = 10;  // 項数 10
        long p = 1;  // 積

        for (int i = 1; i <= n; i++) {
            int m = a + (d * (i - 1));
            p *= m;
        }
        System.out.println(p);
    }
}
public class Java0402 {
    public static void main(String []args) {
        // 初項 5, 公差 3, 項数 10 の数列の積を表示する
        System.out.println(product(5, 3, 10));
    }
    private static long product(int m, int d, int n) {
        if (n == 0)
            return 1;
        else
            return m * product(m + d, d, n - 1);
    }
}
public class Java0403 {

    // 階乗を求める関数
    private static int Fact(int n) {
        if (n <= 1)
            return 1;
        else
            return n * Fact(n - 1);
    }

     public static void main(String []args) {
        // 10の階乗
        System.out.println(Fact(10));
        System.out.println(10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1);
    }
}
public class Java0404 {
    // 下降階乗冪
    private static int FallingFact(int x, int n) {
        if (n <= 1)
            return x;
        else
            return x * FallingFact(x - 1, n - 1);
    }

     public static void main(String []args) {
        // 10 から 6 までの 総乗
        System.out.println(FallingFact(10, 5));
        System.out.println(10 * 9 * 8 * 7 * 6);
    }
}
public class Java0405 {
    // 上昇階乗冪
    private static int RisingFact(int x, int n) {
        if (n <= 1)
            return x;
        else
            return x * RisingFact(x + 1, n - 1);
    }

     public static void main(String []args) {
        // 10 から 14 までの 総乗
        System.out.println(RisingFact(10, 5));
        System.out.println(10 * 11 * 12 * 13 * 14);
    }
}
public class Java0406 {

    // 階乗
    private static int Fact(int n) {
        if (n <= 1)
            return 1;
        else
            return n * Fact(n - 1);
    }

    // 下降階乗冪
    private static int FallingFact(int x, int n) {
        if (n <= 1)
            return x;
        else
            return x * FallingFact(x - 1, n - 1);
    }

     public static void main(String []args) {
        // 順列 (異なる 10 個のものから 5 個取ってできる順列の総数)
        int n = 10;
        int r = 5;
        System.out.println(Fact(n) / Fact(n - r));
        System.out.println(FallingFact(n, r));
    }
}
public class Java0407 {
     public static void main(String []args) {
        // 重複順列 (異なる 10 個のものから重複を許して 5 個取ってできる順列の総数)
        int n = 10;
        int r = 5;
        System.out.println(Math.pow(n, r));
    }
}
public class Java040101 {

    // 組合せ
    private static int Comb(int n, int r)
    {
        if (r == 0 || r == n)
            return 1;
        else if (r == 1)
            return n;
        else
            return Comb(n - 1, r - 1) + Comb(n - 1, r);
    }

     public static void main(String []args) {
        // 組合せ (異なる 10 個のものから 5 個取ってできる組合せの総数)
        int n = 10;
        int r = 5;
        System.out.println(Comb(n, r));
    }
}
public class Java0409 {

    // 組合せ
    private static int Comb(int n, int r)
    {
        if (r == 0 || r == n)
            return 1;
        else if (r == 1)
            return n;
        elsereturn Comb(n - 1, r - 1) + Comb(n - 1, r);
    }

     public static void main(String []args) {
        // 重複組合せ (異なる 10 個のものから重複を許して 5 個とる組合せの総数)
        int n = 10;
        int r = 5;
        System.out.println(Comb(n + r - 1, r));
    }
}
public class Java0501 {
    public static void main(String []args) {
        for (int degree = 0; degree <= 360; degree += 15) {
            if (degree % 30 == 0 || degree % 45 == 0) {
                double radian = Math.toRadians(degree);
                // 自作の正弦関数
                double d1     = mySin(radian, 1, false, radian, 1.0, radian);
                // 標準の正弦関数
                double d2     = Math.sin(radian);
                // 標準関数との差異
                System.out.println(String.format("%3d : %13.10f - %13.10f = %13.10f", degree, d1, d2, d1 - d2));
            }
        }
    }

    // 自作の正弦関数
    private static double mySin(double x, int n, boolean nega, double numerator, double denominator, double y) {
        int m       = 2 * n;
        denominator = denominator * (m + 1) * m;
        numerator   = numerator   * x * x;
        double a    = numerator / denominator;
        // 十分な精度になったら処理を抜ける
        if (a <= 0.00000000001)
            return y;
        else
            return y + mySin(x, ++n, !nega, numerator, denominator, nega ? a : -a);
    }
}
public class Java0502 {
    public static void main(String []args) {
        for (int degree = 0; degree <= 360; degree += 15) {
            if (degree % 30 == 0 || degree % 45 == 0) {
                double radian = Math.toRadians(degree);
                // 自作の余弦関数
                double d1     = myCos(radian, 1, false, 1.0, 1.0, 1.0);
                // 標準の余弦関数
                double d2     = Math.cos(radian);
                // 標準関数との差異
                System.out.println(String.format("%3d : %13.10f - %13.10f = %13.10f", degree, d1, d2, d1 - d2));
            }
        }
    }

    // 自作の余弦関数
    private static double myCos(double x, int n, boolean nega, double numerator, double denominator, double y) {
        int m       = 2 * n;
        denominator = denominator * m * (m - 1);
        numerator   = numerator   * x * x;
        double a    = numerator / denominator;
        // 十分な精度になったら処理を抜ける
        if (a <= 0.00000000001)
            return y;
        else
            return y + myCos(x, ++n, !nega, numerator, denominator, nega ? a : -a);
    }
}
public class Java0503 {
    public static void main(String []args) {
        for (int degree = -90; degree <= 90; degree += 15) {
            if ((degree + 90) % 180 != 0) {
                double radian = Math.toRadians(degree);
                double x2     = radian * radian;
                // 自作の正接関数
                double d1     = myTan(radian, x2, 15, 0.0); // 15:必要な精度が得られる十分大きな奇数
                // 標準の正接関数
                double d2     = Math.tan(radian);
                // 標準関数との差異
                System.out.println(String.format("%3d : %13.10f - %13.10f = %13.10f", degree, d1, d2, d1 - d2));
            }
        }
    }

    // 自作の正接関数
    private static double myTan(double x, double x2, int n, double t) {
        t = x2 / (n - t);
        n -= 2;
        if (n <= 1)
            return x / (1 - t);
        else
            return myTan(x, x2, n, t);
    }
}
public class Java0504 {
    public static void main(String []args) {
        for (int i = -10; i <= 10; i++) {
            double x  = i / 4.0;
            // 標準の指数関数
            double d1 = Math.exp(x);
            // 自作の指数関数
            double d2 = myExp(x, 1, 1.0, 1.0, 1.0);
            // 標準関数との差異
            System.out.println(String.format("%5.2f : %13.10f - %13.10f = %13.10f", x, d1, d2, d1 - d2));
        }
    }

    // 自作の指数関数
    private static double myExp(double x, int n, double numerator, double denominator, double y) {
        denominator = denominator * n;
        numerator   = numerator   * x;
        double a    = numerator / denominator;
        // 十分な精度になったら処理を抜ける
        if (Math.abs(a) <= 0.00000000001)
            return y;
        else
            return y + myExp(x, ++n, numerator, denominator, a);
    }
}
public class Java0505 {
    public static void main(String []args) {
        for (int i = -10; i <= 10; i++) {
            double x  = i / 4.0;
            // 標準の指数関数
            double d1 = Math.exp(x);
            // 自作の指数関数
            double x2 = x * x;
            double d2 = myExp(x, x2, 30, 0.0); // 30:必要な精度が得られるよう, 6から始めて4ずつ増加させる
            // 標準関数との差異
            System.out.println(String.format("%5.2f : %13.10f - %13.10f = %13.10f", x, d1, d2, d1 - d2));
        }
    }

    // 自作の指数関数
    private static double myExp(double x, double x2, int n, double t) {
        t = x2 / (n + t);
        n -= 4;

        if (n < 6)
            return 1 + ((2 * x) / (2 - x + t));
        else
            return myExp(x, x2, n, t);
    }
}
public class Java0506 {
    public static void main(String []args) {
        for (int i = 1; i <= 20; i++) {
            double x  = i / 5.0;
            // 標準の対数関数
            double d1 = Math.log(x);
            // 自作の対数関数
            double x2 = (x - 1) / (x + 1);
            double d2 = 2 * myLog(x2, x2, 1.0, x2);
            // 標準関数との差異
            System.out.println(String.format("%5.2f : %13.10f - %13.10f = %13.10f", x, d1, d2, d1 - d2));
        }
    }

    // 自作の対数関数
    private static double myLog(double x2, double numerator, double denominator, double y) {
        denominator = denominator + 2;
        numerator   = numerator   * x2 * x2;
        double a    = numerator / denominator;
        // 十分な精度になったら処理を抜ける
        if (Math.abs(a) <= 0.00000000001)
            return y;
        else
            return y + myLog(x2, numerator, denominator, a);
    }
}
public class Java0507 {
    public static void main(String []args) {
        for (int i = 1; i <= 20; i++) {
            double x  = i / 5.0;
            // 標準の対数関数
            double d1 = Math.log(x);
            // 自作の対数関数
            double d2 = myLog(x - 1, 27, 0.0); // 27:必要な精度が得られる十分大きな奇数
            // 標準関数との差異
            System.out.println(String.format("%5.2f : %13.10f - %13.10f = %13.10f", x, d1, d2, d1 - d2));
        }
    }

    // 自作の対数関数
    private static double myLog(double x, int n, double t) {
        int    n2 = n;
        double x2 = x;
        if (n > 3) {
            if (n % 2 == 0)
                n2 = 2;
            x2 = x * (n / 2);
        }
        t = x2 / (n2 + t);

        if (n <= 2)
            return x / (1 + t);
        else
            return myLog(x, --n, t);
    }
}
public class Java0508 {
    public static void main(String []args) {
        for (int x = -10; x <= 10; x++) {
            // 自作の双曲線正弦関数
            double d1 = mySinh(x, 1, x, 1.0, x);
            // 標準の双曲線正弦関数
            double d2 = Math.sinh(x);
            // 標準関数との差異
            System.out.println(String.format("%3d : %17.10f - %17.10f = %13.10f", x, d1, d2, d1 - d2));
        }
    }

    // 自作の双曲線正弦関数
    private static double mySinh(double x, int n, double numerator, double denominator, double y) {
        int m       = 2 * n;
        denominator = denominator * (m + 1) * m;
        numerator   = numerator   * x * x;
        double a    = numerator / denominator;
        // 十分な精度になったら処理を抜ける
        if (Math.abs(a) <= 0.00000000001)
            return y;
        else
            return y + mySinh(x, ++n, numerator, denominator, a);
    }
}
public class Java0509 {
    public static void main(String []args) {
        for (int x = -10; x <= 10; x++) {
            // 自作の双曲線余弦関数
            double d1 = myCosh(x, 1, 1.0, 1.0, 1.0);
            // 標準の双曲線余弦関数
            double d2 = Math.cosh(x);
            // 標準関数との差異
            System.out.println(String.format("%3d : %17.10f - %17.10f = %13.10f", x, d1, d2, d1 - d2));
        }
    }

    // 自作の双曲線余弦関数
    private static double myCosh(double x, int n, double numerator, double denominator, double y) {
        int m       = 2 * n;
        denominator = denominator * m * (m - 1);
        numerator   = numerator   * x * x;
        double a    = numerator / denominator;
        // 十分な精度になったら処理を抜ける
        if (Math.abs(a) <= 0.00000000001)
            return y;
        else
            return y + myCosh(x, ++n, numerator, denominator, a);
    }
}
public class Java0601 {

    private static double f(double x) {
        return 4 / (1 + x * x);
    }

    public static void main(String []args) {
        final double a = 0;
        final double b = 1;

        // 台形則で積分
        int n = 2;
        for (int j = 1; j <= 10; j++) {
            double h = (b - a) / n;
            double s = 0;
            double x = a;
            for (int i = 1; i <= n - 1; i++) {
                x += h;
                s += f(x);
            }
            s = h * ((f(a) + f(b)) / 2 + s);
            n *= 2;

            // 結果を π と比較
            System.out.println(String.format("%2d : %13.10f, %13.10f", j, s, s - Math.PI));
        }
    }
}
public class Java0602 {

    private static double f(double x) {
        return 4 / (1 + x * x);
    }

    public static void main(String []args) {
        final double a = 0;
        final double b = 1;

        // 中点則で積分
        int n = 2;
        for (int j = 1; j <= 10; j++) {
            double h = (b - a) / n;
            double s = 0;
            double x = a + (h / 2);
            for (int i = 1; i <= n; i++) {
                s += f(x);
                x += h;
            }
            s *= h;
            n *= 2;

            // 結果を π と比較
            System.out.println(String.format("%2d : %13.10f, %13.10f", j, s, s - Math.PI));
        }
    }
}
public class Java0603 {

    private static double f(double x) {
        return 4 / (1 + x * x);
    }

    public static void main(String []args) {
        final double a = 0;
        final double b = 1;

        // Simpson則で積分
        int n = 2;
        for (int j = 1; j <= 5; j++) {
            double h  = (b - a) / n;
            double s2 = 0;
            double s4 = 0;
            double x  = a + h;
            for (int i = 1; i <= n / 2; i++) {
                s4 += f(x);
                x  += h;
                s2 += f(x);
                x  += h;
            }
            s2 = (s2 - f(b)) * 2 + f(a) + f(b);
            s4 *= 4;
            double s = (s2 + s4) * h / 3;
            n *= 2;

            // 結果を π と比較
            System.out.println(String.format("%2d : %13.10f, %13.10f", j, s, s - Math.PI));
        }
    }
}
public class Java0604 {

    private static double f(double x) {
        return 4 / (1 + x * x);
    }

    public static void main(String []args) {
        final double a = 0;
        final double b = 1;

        double[][] t = new double[7][7];

        // 台形則で積分
        int n = 2;
        for (int i = 1; i <= 6; i++) {
            double h = (b - a) / n;
            double s = 0;
            double x = a;
            for (int j = 1; j <= n - 1; j++) {
                x += h;
                s += f(x);
            }
            // 結果を保存
            t[i][1] = h * ((f(a) + f(b)) / 2 + s);
            n *= 2;
        }

        // Richardsonの補外法
        n = 4;
        for (int j = 2; j <= 6; j++) {
            for (int i = j; i <= 6; i++) {
                t[i][j] = t[i][j - 1] + (t[i][j - 1] - t[i - 1][j - 1]) / (n - 1);
                if (i == j) {
                    // 結果を π と比較
                    System.out.println(String.format("%2d : %13.10f, %13.10f", j, t[i][j], t[i][j] - Math.PI));
                }
            }
            n *= 4;
        }
    }
}
public class Java0701 {

    // データ点の数
    private static final int N = 7;

    public static void main(String []args) {
        double[] x = new double[N];
        double[] y = new double[N];

        // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
        for (int i = 0; i < N; i++) {
            double d = i * 1.5 - 4.5;
            x[i] = d;
            y[i] = f(d);
        }

        // 0.5刻みで 与えられていない値を補間
        for (int i = 0; i <= 18; i++) {
            double d  = i * 0.5 - 4.5;
            double d1 = f(d);
            double d2 = lagrange(d, x, y);

            // 元の関数と比較
            System.out.println(String.format("%5.2f\t%8.5f\t%8.5f\t%8.5f", d, d1, d2, d1 - d2));
        }
    }

    // 元の関数
    private static double f(double x) {
        return x - Math.pow(x,3) / (3 * 2) + Math.pow(x,5) / (5 * 4 * 3 * 2);
    }

    // Lagrange (ラグランジュ) 補間
    private static double lagrange(double d, double[] x, double[] y) {
        double sum = 0;
        for (int i = 0; i < N; i++) {
            double prod = y[i];
            for (int j = 0; j < N; j++) {
                if (j != i)
                    prod *= (d - x[j]) / (x[i] - x[j]);
            }
            sum += prod;
        }
        return sum;
    }
}
public class Java0702 {

    // データ点の数
    private static final int N = 7;

    public static void main(String []args) {
        double[] x = new double[N];
        double[] y = new double[N];

        // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
        for (int i = 0; i < N; i++) {
            double d = i * 1.5 - 4.5;
            x[i] = d;
            y[i] = f(d);
        }

        // 0.5刻みで 与えられていない値を補間
        for (int i = 0; i <= 18; i++) {
            double d  = i * 0.5 - 4.5;
            double d1 = f(d);
            double d2 = neville(d, x, y);

            // 元の関数と比較
            System.out.println(String.format("%5.2f\t%8.5f\t%8.5f\t%8.5f", d, d1, d2, d1 - d2));
        }
    }

    // 元の関数
    private static double f(double x) {
        return x - Math.pow(x,3) / (3 * 2) + Math.pow(x,5) / (5 * 4 * 3 * 2);
    }

    // Neville (ネヴィル) 補間
    private static double neville(double d, double[] x, double[] y) {
        double[][] w = new double[N][N];
        for (int i = 0; i < N; i++)
            w[0][i] = y[i];

        for (int j = 1; j < N; j++) {
            for (int i = 0; i < N - j; i++)
                w[j][i] = w[j-1][i+1] + (w[j-1][i+1] - w[j-1][i]) * (d - x[i+j]) / (x[i+j] - x[i]);
        }

        return w[N-1][0];
    }
}
public class Java0703 {

    // データ点の数
    private static final int N = 7;

    public static void main(String []args) {
        double[] x = new double[N];
        double[] y = new double[N];

        // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
        for (int i = 0; i < N; i++) {
            double d1 = i * 1.5 - 4.5;
            x[i] = d1;
            y[i] = f(d1);
        }

        // 差分商の表を作る
        double[][] d = new double[N][N];
        for (int j = 0; j < N; j++)
            d[0][j] = y[j];

        for (int i = 1; i < N; i++) {
            for (int j = 0; j < N - i; j++)
                d[i][j] = (d[i-1][j+1] - d[i-1][j]) / (x[j+i] - x[j]);
        }

        // n階差分商
        double[] a = new double[N];
        for (int j = 0; j < N; j++)
            a[j] = d[j][0];

        // 0.5刻みで 与えられていない値を補間
        for (int i = 0; i <= 18; i++) {
            double d1 = i * 0.5 - 4.5;
            double d2 = f(d1);
            double d3 = newton(d1, x, a);

            // 元の関数と比較
            System.out.println(String.format("%5.2f\t%8.5f\t%8.5f\t%8.5f", d1, d2, d3, d2 - d3));
        }
    }

    // 元の関数
    private static double f(double x) {
        return x - Math.pow(x,3) / (3 * 2) + Math.pow(x,5) / (5 * 4 * 3 * 2);
    }

    // Newton (ニュートン) 補間
    private static double newton(double d, double[] x, double[] a) {
        double sum = a[0];
        for (int i = 1; i < N; i++) {
            double prod = a[i];
            for (int j = 0; j < i; j++)
                prod *= (d - x[j]);
            sum += prod;
        }

        return sum;
    }
}
public class Java0704 {

    // データ点の数
    private static final int N   =  7;
    private static final int Nx2 = 14;

    public static void main(String []args) {
        double[] x  = new double[N];
        double[] y  = new double[N];
        double[] yd = new double[N];

        // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
        for (int i = 0; i < N; i++) {
            double d1 = i * 1.5 - 4.5;
            x[i]  = d1;
            y[i]  = f(d1);
            yd[i] = fd(d1);
        }

        // 差分商の表を作る
        double[]  z = new double[Nx2];
        double[][] d = new double[Nx2][Nx2];
        for (int i = 0; i < Nx2; i++) {
            int j   = i / 2;
            z[i]    = x[j];
            d[0][i] = y[j];
        }
        for (int i = 1; i < Nx2; i++) {
            for (int j = 0; j < Nx2 - i; j++)
            {
                if (i == 1 && j % 2 == 0)
                    d[i][j] = yd[j / 2];
                else
                    d[i][j] = (d[i-1][j+1] - d[i-1][j]) / (z[j+i] - z[j]);
            }
        }

        // n階差分商
        double[] a = new double[Nx2];
        for (int j = 0; j < Nx2; j++)
            a[j] = d[j][0];

        // 0.5刻みで 与えられていない値を補間
        for (int i = 0; i <= 18; i++) {
            double d1 = i * 0.5 - 4.5;
            double d2 = f(d1);
            double d3 = hermite(d1, z, a);

            // 元の関数と比較
            System.out.println(String.format("%5.2f\t%8.5f\t%8.5f\t%8.5f", d1, d2, d3, d2 - d3));
        }
    }

    // 元の関数
    private static double f(double x) {
        return x - Math.pow(x,3) / (3 * 2) + Math.pow(x,5) / (5 * 4 * 3 * 2);
    }
    // 導関数
    private static double fd(double x) {
        return 1 - Math.pow(x,2) / 2 + Math.pow(x,4) / (4 * 3 * 2);
    }

    // Hermite (エルミート) 補間
    private static double hermite(double d, double[] z, double[] a) {
        double sum = a[0];
        for (int i = 1; i < Nx2; i++) {
            double prod = a[i];
            for (int j = 0; j < i; j++)
                prod *= (d - z[j]);
            sum += prod;
        }

        return sum;
    }
}
public class Java0705 {

    // データ点の数
    private static final int N = 7;

    public static void main(String []args) {
        double[] x = new double[N];
        double[] y = new double[N];

        // 1.5刻みで -4.5〜4.5 まで, 7点だけ値をセット
        for (int i = 0; i < N; i++) {
            double d1 = i * 1.5 - 4.5;
            x[i] = d1;
            y[i] = f(d1);
        }

        // 3項方程式の係数の表を作る
        double[] a = new double[N];
        double[] b = new double[N];
        double[] c = new double[N];
        double[] d = new double[N];
        for (int i = 1; i < N - 1; i++) {
            a[i] =         x[i]   - x[i-1];
            b[i] = 2.0 *  (x[i+1] - x[i-1]);
            c[i] =         x[i+1] - x[i];
            d[i] = 6.0 * ((y[i+1] - y[i]) / (x[i+1] - x[i]) - (y[i] - y[i-1]) / (x[i] - x[i-1]));
        }
        // 3項方程式を解く (ト−マス法)
        double[] g = new double[N];
        double[] s = new double[N];
        g[1] = b[1];
        s[1] = d[1];
        for (int i = 2; i < N - 1; i++) {
            g[i] = b[i] - a[i] * c[i-1] / g[i-1];
            s[i] = d[i] - a[i] * s[i-1] / g[i-1];
        }
        double[] z = new double[N];
        z[0]   = 0;
        z[N-1] = 0;
        z[N-2] = s[N-2] / g[N-2];
        for (int i = N - 3; i >= 1; i--)
            z[i] = (s[i] - c[i] * z[i+1]) / g[i];

        // 0.5刻みで 与えられていない値を補間
        for (int i = 0; i <= 18; i++) {
            double d1 = i * 0.5 - 4.5;
            double d2 = f(d1);
            double d3 = spline(d1, x, y, z);

            // 元の関数と比較
            System.out.println(String.format("%5.2f\t%8.5f\t%8.5f\t%8.5f", d1, d2, d3, d2 - d3));
        }
    }

    // 元の関数
    private static double f(double x) {
        return x - Math.pow(x,3) / (3 * 2) + Math.pow(x,5) / (5 * 4 * 3 * 2);
    }

    // Spline (スプライン) 補間
    private static double spline(double d, double[] x, double[] y, double[] z) {
        // 補間関数値がどの区間にあるか
        int k = -1;
        for (int i = 1; i < N; i++) {
            if (d <= x[i]) {
                k = i - 1;
                break;
            }
        }
        if (k < 0) k = N - 1;

        double d1 = x[k+1] - d;
        double d2 = d      - x[k];
        double d3 = x[k+1] - x[k];
        return      (z[k] * Math.pow(d1,3) + z[k+1] * Math.pow(d2,3)) / (6.0 * d3)
                  + (y[k]   / d3 - z[k]   * d3 / 6.0) * d1
                  + (y[k+1] / d3 - z[k+1] * d3 / 6.0) * d2;
    }
}
import static java.lang.System.out;

public class Java0801 {

    // 重力加速度
    private static final double g = -9.8;
    // 空気抵抗係数
    private static final double k = -0.01;
    // 時間間隔(秒)
    private static final double h = 0.01;

    public static void main(String []args) {
        // 角度
        double degree = 45;
        double radian = degree * Math.PI / 180.0;
        // 初速 250 km/h -> 秒速に変換
        double v = 250 * 1000 / 3600;
        // 水平方向の速度
        double[] vx = new double[2];
        vx[0] = v * Math.cos(radian);
        // 鉛直方向の速度
        double[] vy = new double[2];
        vy[0] = v * Math.sin(radian);
        // 経過秒数
        double t = 0.0;
        // 位置
        double x = 0.0;
        double y = 0.0;

        // Euler法
        for (int i = 1; y >= 0.0; i++) {
            // 経過秒数
            t = i * h;

            // 位置
            x += h * vx[0];
            y += h * vy[0];
            out.println(String.format("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%8.5f", t, vx[0], vy[0], x, y));

            // 速度
            vx[1] = vx[0] + h * fx(vx[0], vy[0]);
            vy[1] = vy[0] + h * fy(vx[0], vy[0]);
            vx[0] = vx[1];
            vy[0] = vy[1];
        }
    }

    // 空気抵抗による水平方向の減速分
    private static double fx(double vx, double vy) {
        return k * Math.sqrt(vx * vx + vy * vy) * vx;
    }
    // 重力と空気抵抗による鉛直方向の減速分
    private static double fy(double vx, double vy) {
        return g + (k * Math.sqrt(vx * vx + vy * vy) * vy);
    }
}
import static java.lang.System.out;

public class Java0802 {

    // 重力加速度
    private static final double g = -9.8;
    // 空気抵抗係数
    private static final double k = -0.01;
    // 時間間隔(秒)
    private static final double h = 0.01;

    public static void main(String []args) {
        // 角度
        double degree = 45;
        double radian = degree * Math.PI / 180.0;
        // 初速 250 km/h -> 秒速に変換
        double v = 250 * 1000 / 3600;
        // 水平方向の速度
        double[] vx = new double[3];
        vx[0] = v * Math.cos(radian);
        // 鉛直方向の速度
        double[] vy = new double[3];
        vy[0] = v * Math.sin(radian);
        // 経過秒数
        double t = 0.0;
        // 位置
        double[] x = new double[3];
        x[0] = 0.0;
        double[] y = new double[3];
        y[0] = 0.0;

        // Heun法
        for (int i = 1; y[0] >= 0.0; i++) {
            // 経過秒数
            t = i * h;

            // 位置・速度
            x[1]  =  x[0] + h *    vx[0];
            y[1]  =  y[0] + h *    vy[0];
            vx[1] = vx[0] + h * fx(vx[0], vy[0]);
            vy[1] = vy[0] + h * fy(vx[0], vy[0]);

            x[2]  =  x[0] + h * (  vx[0]          +    vx[1]        ) / 2;
            y[2]  =  y[0] + h * (  vy[0]          +    vy[1]        ) / 2;
            vx[2] = vx[0] + h * (fx(vx[0], vy[0]) + fx(vx[1], vy[1])) / 2;
            vy[2] = vy[0] + h * (fy(vx[0], vy[0]) + fy(vx[1], vy[1])) / 2;

            x[0]  =  x[2];
            y[0]  =  y[2];
            vx[0] = vx[2];
            vy[0] = vy[2];

            out.println(String.format("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%8.5f", t, vx[0], vy[0], x[0], y[0]));
        }
    }

    // 空気抵抗による水平方向の減速分
    private static double fx(double vx, double vy) {
        return k * Math.sqrt(vx * vx + vy * vy) * vx;
    }
    // 重力と空気抵抗による鉛直方向の減速分
    private static double fy(double vx, double vy) {
        return g + (k * Math.sqrt(vx * vx + vy * vy) * vy);
    }
}
import static java.lang.System.out;

public class Java0803 {

    // 重力加速度
    private static final double g = -9.8;
    // 空気抵抗係数
    private static final double k = -0.01;
    // 時間間隔(秒)
    private static final double h = 0.01;

    public static void main(String []args) {
        // 角度
        double degree = 45;
        double radian = degree * Math.PI / 180.0;
        // 初速 250 km/h -> 秒速に変換
        double v = 250 * 1000 / 3600;
        // 水平方向の速度
        double[] vx = new double[2];
        vx[0] = v * Math.cos(radian);
        // 鉛直方向の速度
        double[] vy = new double[2];
        vy[0] = v * Math.sin(radian);
        // 経過秒数
        double t = 0.0;
        // 位置
        double[] x = new double[2];
        x[0] = 0.0;
        double[] y = new double[2];
        y[0] = 0.0;

        // 中点法
        for (int i = 1; y[0] >= 0.0; i++) {
            // 経過秒数
            t = i * h;

            // 位置・速度
            vx[1] = h * fx(vx[0], vy[0]);
            vy[1] = h * fy(vx[0], vy[0]);

            double wx = vx[0] + vx[1] / 2;
            double wy = vy[0] + vy[1] / 2;
            vx[0]     = vx[0] + h * fx(wx, wy);
            vy[0]     = vy[0] + h * fy(wx, wy);
            x[0]      =  x[0] + h *    wx;
            y[0]      =  y[0] + h *    wy;

            out.println(String.format("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%8.5f", t, vx[0], vy[0], x[0], y[0]));
        }
    }

    // 空気抵抗による水平方向の減速分
    private static double fx(double vx, double vy) {
        return k * Math.sqrt(vx * vx + vy * vy) * vx;
    }
    // 重力と空気抵抗による鉛直方向の減速分
    private static double fy(double vx, double vy) {
        return g + (k * Math.sqrt(vx * vx + vy * vy) * vy);
    }
}
import static java.lang.System.out;

public class Java0804 {

    // 重力加速度
    private static final double g = -9.8;
    // 空気抵抗係数
    private static final double k = -0.01;
    // 時間間隔(秒)
    private static final double h = 0.01;

    public static void main(String []args) {
        // 角度
        double degree = 45;
        double radian = degree * Math.PI / 180.0;
        // 初速 250 km/h -> 秒速に変換
        double v = 250 * 1000 / 3600;
        // 水平方向の速度
        double[] vx = new double[5];
        vx[0] = v * Math.cos(radian);
        // 鉛直方向の速度
        double[] vy = new double[5];
        vy[0] = v * Math.sin(radian);
        // 経過秒数
        double t = 0.0;
        // 位置
        double[] x = new double[5];
        x[0] = 0.0;
        double[] y = new double[5];
        y[0] = 0.0;

        // Runge-Kutta法
        for (int i = 1; y[0] >= 0.0; i++) {
            // 経過秒数
            t = i * h;

            // 位置・速度
            x[1]  = h *    vx[0];
            y[1]  = h *    vy[0];
            vx[1] = h * fx(vx[0], vy[0]);
            vy[1] = h * fy(vx[0], vy[0]);

            double wx = vx[0] + vx[1] / 2;
            double wy = vy[0] + vy[1] / 2;
            x[2]  = h *    wx;
            y[2]  = h *    wy;
            vx[2] = h * fx(wx, wy);
            vy[2] = h * fy(wx, wy);

            wx    = vx[0] + vx[2] / 2;
            wy    = vy[0] + vy[2] / 2;
            x[3]  = h *    wx;
            y[3]  = h *    wy;
            vx[3] = h * fx(wx, wy);
            vy[3] = h * fy(wx, wy);

            wx    = vx[0] + vx[3];
            wy    = vy[0] + vy[3];
            x[4]  = h *    wx;
            y[4]  = h *    wy;
            vx[4] = h * fx(wx, wy);
            vy[4] = h * fy(wx, wy);

            x[0]  += ( x[1] +  x[2] * 2 +  x[3] * 2 +  x[4]) / 6;
            y[0]  += ( y[1] +  y[2] * 2 +  y[3] * 2 +  y[4]) / 6;
            vx[0] += (vx[1] + vx[2] * 2 + vx[3] * 2 + vx[4]) / 6;
            vy[0] += (vy[1] + vy[2] * 2 + vy[3] * 2 + vy[4]) / 6;

            out.println(String.format("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%8.5f", t, vx[0], vy[0], x[0], y[0]));
        }
    }

    // 空気抵抗による水平方向の減速分
    private static double fx(double vx, double vy) {
        return k * Math.sqrt(vx * vx + vy * vy) * vx;
    }
    // 重力と空気抵抗による鉛直方向の減速分
    private static double fy(double vx, double vy) {
        return g + (k * Math.sqrt(vx * vx + vy * vy) * vy);
    }
}
import static java.lang.System.out;

public class Java0805 {

    // 重力加速度
    private static final double g = -9.8;
    // 空気抵抗係数
    private static final double k = -0.01;
    // 時間間隔(秒)
    private static final double h = 0.01;

    public static void main(String []args) {
        // 角度
        double degree = 45;
        double radian = degree * Math.PI / 180.0;
        // 初速 250 km/h -> 秒速に変換
        double v = 250 * 1000 / 3600;
        // 水平方向の速度
        double[] vx = new double[5];
        vx[0] = v * Math.cos(radian);
        // 鉛直方向の速度
        double[] vy = new double[5];
        vy[0] = v * Math.sin(radian);
        // 経過秒数
        double t = 0.0;
        // 位置
        double[] x = new double[5];
        x[0] = 0.0;
        double[] y = new double[5];
        y[0] = 0.0;

        // Runge-Kutta-Gill法
        for (int i = 1; y[0] >= 0.0; i++) {
            // 経過秒数
            t = i * h;

            // 位置・速度
            x[1]  = h *    vx[0];
            y[1]  = h *    vy[0];
            vx[1] = h * fx(vx[0], vy[0]);
            vy[1] = h * fy(vx[0], vy[0]);

            double wx = vx[0] + vx[1] / 2;
            double wy = vy[0] + vy[1] / 2;
            x[2]  = h *    wx;
            y[2]  = h *    wy;
            vx[2] = h * fx(wx, wy);
            vy[2] = h * fy(wx, wy);

            wx    = vx[0] + vx[1] * ((Math.sqrt(2.0) - 1) / 2) + vx[2] * (1 - 1 / Math.sqrt(2.0));
            wy    = vy[0] + vy[1] * ((Math.sqrt(2.0) - 1) / 2) + vy[2] * (1 - 1 / Math.sqrt(2.0));
            x[3]  = h *    wx;
            y[3]  = h *    wy;
            vx[3] = h * fx(wx, wy);
            vy[3] = h * fy(wx, wy);

            wx    = vx[0] - vx[2] / Math.sqrt(2.0) + vx[3] * (1 + 1 / Math.sqrt(2.0));
            wy    = vy[0] - vy[2] / Math.sqrt(2.0) + vy[3] * (1 + 1 / Math.sqrt(2.0));
            x[4]  = h *    wx;
            y[4]  = h *    wy;
            vx[4] = h * fx(wx, wy);
            vy[4] = h * fy(wx, wy);

            x[0]  += ( x[1] +  x[2] * (2 - Math.sqrt(2.0)) +  x[3] * (2 + Math.sqrt(2.0)) +  x[4]) / 6;
            y[0]  += ( y[1] +  y[2] * (2 - Math.sqrt(2.0)) +  y[3] * (2 + Math.sqrt(2.0)) +  y[4]) / 6;
            vx[0] += (vx[1] + vx[2] * (2 - Math.sqrt(2.0)) + vx[3] * (2 + Math.sqrt(2.0)) + vx[4]) / 6;
            vy[0] += (vy[1] + vy[2] * (2 - Math.sqrt(2.0)) + vy[3] * (2 + Math.sqrt(2.0)) + vy[4]) / 6;

            out.println(String.format("%4.2f\t%8.5f\t%9.5f\t%9.5f\t%8.5f", t, vx[0], vy[0], x[0], y[0]));
        }
    }

    // 空気抵抗による水平方向の減速分
    private static double fx(double vx, double vy) {
        return k * Math.sqrt(vx * vx + vy * vy) * vx;
    }
    // 重力と空気抵抗による鉛直方向の減速分
    private static double fy(double vx, double vy) {
        return g + (k * Math.sqrt(vx * vx + vy * vy) * vy);
    }
}
import static java.lang.System.out;

public class Java0901 {

    public static void main(String []args) {
        double a = 1;
        double b = 2;
        out.println(String.format("%12.10f", bisection(a, b)));
    }

    private static double bisection(double a, double b) {
        double c;
        while (true) {
            // 区間 (a, b) の中点 c = (a + b) / 2
            c = (a + b) / 2;
            out.println(String.format("%12.10f\t%13.10f", c, c - Math.sqrt(2)));

            double fc = f(c);
            if (Math.abs(fc) < 0.0000000001) break;

            if (fc < 0){
                // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
                a = c;
            } else {
                // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
                b = c;
            }
        }
        return c;
    }

    private static double f(double x) {
        return x * x - 2;
    }
}
import static java.lang.System.out;

public class Java0902 {

    public static void main(String []args) {
        double a = 1;
        double b = 2;
        out.println(String.format("%12.10f", falseposition(a, b)));
    }

    private static double falseposition(double a, double b) {
        double c;
        while (true) {
            // 点 (a,f(a)) と 点 (b,f(b)) を結ぶ直線と x軸の交点
            c = (a * f(b) - b * f(a)) / (f(b) - f(a));
            out.println(String.format("%12.10f\t%13.10f", c, c - Math.sqrt(2)));

            double fc = f(c);
            if (Math.abs(fc) < 0.0000000001) break;

            if (fc < 0){
                // f(c) < 0 であれば, 解は区間 (c, b) の中に存在
                a = c;
            } else {
                // f(c) > 0 であれば, 解は区間 (a, c) の中に存在
                b = c;
            }
        }
        return c;
    }

    private static double f(double x) {
        return x * x - 2;
    }
}
import static java.lang.System.out;

public class Java0903 {

    public static void main(String []args) {
        double x = 1;
        out.println(String.format("%12.10f", iterative(x)));
    }

    private static double iterative(double x0) {
        double x1;
        while (true) {
            x1 = g(x0);
            out.println(String.format("%12.10f\t%13.10f", x1, x1 - Math.sqrt(2)));

            if (Math.abs(x1 - x0) < 0.0000000001) break;
            x0 = x1;
        }
        return x1;
    }

    private static double g(double x) {
        return (x / 2) + (1 / x);
    }
}
import static java.lang.System.out;

public class Java0904 {

    public static void main(String []args) {
        double x = 2;
        out.println(String.format("%12.10f", newton(x)));
    }

    private static double newton(double x0) {
        double x1;
        while (true) {
            x1 = x0 - (f0(x0) / f1(x0));
            out.println(String.format("%12.10f\t%13.10f", x1, x1 - Math.sqrt(2)));

            if (Math.abs(x1 - x0) < 0.0000000001) break;
            x0 = x1;
        }
        return x1;
    }

    private static double f0(double x) {
        return x * x - 2;
    }
    private static double f1(double x) {
        return 2 * x;
    }
}
import static java.lang.System.out;

public class Java0905 {

    public static void main(String []args) {
        double x = 2;
        out.println(String.format("%12.10f", bailey(x)));
    }

    private static double bailey(double x0) {
        double x1;
        while (true) {
            x1 = x0 - (f0(x0) / (f1(x0) - (f0(x0) * f2(x0) / (2 * f1(x0)))));
            out.println(String.format("%12.10f\t%13.10f", x1, x1 - Math.sqrt(2)));

            if (Math.abs(x1 - x0) < 0.0000000001) break;
            x0 = x1;
        }
        return x1;
    }

    private static double f0(double x) {
        return x * x - 2;
    }
    private static double f1(double x) {
        return 2 * x;
    }
    private static double f2(double x) {
        return 2;
    }
}
import static java.lang.System.out;

public class Java0906 {

    public static void main(String []args) {
        double x0 = 1;
        double x1 = 2;
        out.println(String.format("%12.10f", secant(x0, x1)));
    }

    private static double secant(double x0, double x1) {
        double x2;
        while (true) {
            x2 = x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0));
            out.println(String.format("%12.10f\t%13.10f", x2, x2 - Math.sqrt(2)));

            if (Math.abs(x2 - x1) < 0.0000000001) break;
            x0 = x1;
            x1 = x2;
        }
        return x2;
    }

    private static double f(double x) {
        return x * x - 2;
    }
}
import java.lang.*;

public class Java1001 {

    private static final int N = 4;

    public static void main(String []args) {
        double[][] a = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}};
        double[]   b = {20,16,8,17};
        double[]   c = {0,0,0,0};

        // ヤコビの反復法
        jacobi(a,b,c);

        System.out.println("解");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", c[i]));
        System.out.println();
    }

    // ヤコビの反復法
    private static void jacobi(double[][] a, double[] b, double[] x0) {
        while (true) {
            double[] x1 = new double[N];
            boolean finish = true;
            for (int i = 0; i < N; i++) {
                x1[i] = 0;
                for (int j = 0; j < N; j++)
                    if (j != i)
                        x1[i] += a[i][j] * x0[j];

                x1[i] = (b[i] - x1[i]) / a[i][i];
                if (Math.abs(x1[i] - x0[i]) > 0.0000000001) finish = false;
            }

            for (int i = 0; i < N; i++) {
                x0[i] = x1[i];
                System.out.print(String.format("%14.10f\t", x0[i]));
            }
            System.out.println();
            if (finish) return;
        }
    }
}
import java.lang.*;

public class Java1002 {

    private static final int N = 4;

    public static void main(String []args) {
        double[][] a = {{9,2,1,1},{2,8,-2,1},{-1,-2,7,-2},{1,-1,-2,6}};
        double[]   b = {20,16,8,17};
        double[]   c = {0,0,0,0};

        // ガウス・ザイデル法
        gauss(a,b,c);

        System.out.println("解");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", c[i]));
        System.out.println();
    }

    // ガウス・ザイデル法
    private static void gauss(double[][] a, double[] b, double[] x0) {
        while (true) {
            double x1;
            boolean finish = true;
            for (int i = 0; i < N; i++) {
                x1 = 0;
                for (int j = 0; j < N; j++)
                    if (j != i)
                        x1 += a[i][j] * x0[j];

                x1 = (b[i] - x1) / a[i][i];
                if (Math.abs(x1 - x0[i]) > 0.0000000001) finish = false;
                x0[i] = x1;
            }

            for (int i = 0; i < N; i++)
                System.out.print(String.format("%14.10f\t", x0[i]));
            System.out.println();
            if (finish) return;
        }
    }
}
import java.lang.*;

public class Java1003 {

    private static final int N = 4;

    // ガウスの消去法
    public static void main(String []args) {
        double[][] a = {{-1,-2,7,-2},{1,-1,-2,6},{9,2,1,1},{2,8,-2,1}};
        double[]   b = {8,17,20,16};

        // ピボット選択
        pivoting(a,b);
        System.out.println("ピボット選択後");
        disp_progress(a,b);

        // 前進消去
        forward_elimination(a,b);
        System.out.println("前進消去後");
        disp_progress(a,b);

        // 後退代入
        backward_substitution(a,b);

        System.out.println("解");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", b[i]));
        System.out.println();
    }
    // 前進消去
    private static void forward_elimination(double[][] a, double[] b) {
        for (int pivot = 0; pivot < N - 1; pivot++) {
            for (int row = pivot + 1; row < N; row++) {
                double  s   = a[row][pivot] / a[pivot][pivot];
                for (int col = pivot; col < N; col++)
                    a[row][col] -= a[pivot][col]    * s;
                b[row]          -= b[pivot]         * s;
            }
        }
    }
    // 後退代入
    private static void backward_substitution(double[][] a, double[] b) {
        for (int row = N - 1; row >= 0; row--) {
            for (int col = N - 1; col > row; col--)
                b[row] -= a[row][col] * b[col];
            b[row] /= a[row][row];
        }
    }
    // ピボット選択
    private static void pivoting(double[][] a, double[] b) {
        for(int pivot = 0; pivot < N; pivot++) {
            // 各列で 一番値が大きい行を 探す
            int     max_row =   pivot;
            double  max_val =   0;
            for (int row = pivot; row < N; row++) {
                if (Math.abs(a[row][pivot]) > max_val) {
                    // 一番値が大きい行
                    max_val =   Math.abs(a[row][pivot]);
                    max_row =   row;
                }
            }

            // 一番値が大きい行と入れ替え
            if (max_row != pivot) {
                double tmp;
                for (int col = 0; col < N; col++) {
                    tmp             =   a[max_row][col];
                    a[max_row][col] =   a[pivot][col];
                    a[pivot][col]   =   tmp;
                }
                tmp         =   b[max_row];
                b[max_row]  =   b[pivot];
                b[pivot]    =   tmp;
            }
        }
    }
    // 状態を確認
    private static void disp_progress(double[][] a, double[] b) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                System.out.print(String.format("%14.10f\t", a[i][j]));
            System.out.println(String.format("%14.10f\t", b[i]));
        }
        System.out.println();
    }
}
import java.lang.*;

public class Java1004 {

    private static final int N = 4;

    // ガウス・ジョルダン法
    public static void main(String []args) {
        double[][] a = {{-1,-2,7,-2},{1,-1,-2,6},{9,2,1,1},{2,8,-2,1}};
        double[]   b = {8,17,20,16};

        // ピボット選択
        pivoting(a,b);
        System.out.println("ピボット選択後");
        disp_progress(a,b);

        // 前進消去
        forward_elimination(a,b);
        System.out.println("前進消去後");
        disp_progress(a,b);

        // 後退代入
        backward_substitution(a,b);

        System.out.println("解");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", b[i]));
        System.out.println();
    }
    // 前進消去
    private static void forward_elimination(double[][] a, double[] b) {
        // 対角線上の係数を 1 にする
        for (int pivot = 0; pivot < N; pivot++) {
            double s  = a[pivot][pivot];
            for (int col = 0; col < N; col++)
                a[pivot][col] /= s;
             b[pivot] /= s;
        }
        System.out.println("対角線上の係数を 1 にする");
        disp_progress(a,b);

        // 対角行列にする
        for (int pivot = 0; pivot < N; pivot++) {
            for (int row = 0; row < N; row++) {
                if (row == pivot) continue;

                double  s   = a[row][pivot] / a[pivot][pivot];
                for (int col = pivot; col < N; col++)
                    a[row][col] -= a[pivot][col]    * s;
                b[row]          -= b[pivot]         * s;
            }
        }
    }
    // 後退代入
    private static void backward_substitution(double[][] a, double[] b) {
        for (int pivot = 0; pivot < N; pivot++)
            b[pivot]  /= a[pivot][pivot];
    }
    // ピボット選択
    private static void pivoting(double[][] a, double[] b) {
        for(int pivot = 0; pivot < N; pivot++) {
            // 各列で 一番値が大きい行を 探す
            int     max_row =   pivot;
            double  max_val =   0;
            for (int row = pivot; row < N; row++) {
                if (Math.abs(a[row][pivot]) > max_val) {
                    // 一番値が大きい行
                    max_val =   Math.abs(a[row][pivot]);
                    max_row =   row;
                }
            }

            // 一番値が大きい行と入れ替え
            if (max_row != pivot) {
                double tmp;
                for (int col = 0; col < N; col++) {
                    tmp             =   a[max_row][col];
                    a[max_row][col] =   a[pivot][col];
                    a[pivot][col]   =   tmp;
                }
                tmp         =   b[max_row];
                b[max_row]  =   b[pivot];
                b[pivot]    =   tmp;
            }
        }
    }
    // 状態を確認
    private static void disp_progress(double[][] a, double[] b) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                System.out.print(String.format("%14.10f\t", a[i][j]));
            System.out.println(String.format("%14.10f\t", b[i]));
        }
        System.out.println();
    }
}
import java.lang.*;

public class Java1005 {

    private static final int N = 4;

    // LU分解
    public static void main(String []args) {
        double[][] a = {{-1,-2,7,-2},{1,-1,-2,6},{9,2,1,1},{2,8,-2,1}};
        double[]   b = {8,17,20,16};

        // ピボット選択
        pivoting(a,b);
        System.out.println("ピボット選択後");
        disp_progress(a,b);

        // LU分解
        double[] x = {0,0,0,0};
        decomp(a,b,x);
        System.out.println("X");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", x[i]));
        System.out.println();
    }
    // LU分解
    private static void decomp(double[][] a, double[] b, double[] x) {
        // 前進消去
        forward_elimination(a,b);
        // 下三角行列を確認
        System.out.println("L");
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                if (i > j)
                    System.out.print(String.format("%14.10f\t", a[i][j]));
                else if (i == j)
                    System.out.print(String.format("%14.10f\t", 1.0));
                else
                    System.out.print(String.format("%14.10f\t", 0.0));
            System.out.println();
        }
        System.out.println();
        // 上三角行列を確認
        System.out.println("U");
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                if (i <= j)
                    System.out.print(String.format("%14.10f\t", a[i][j]));
                else
                    System.out.print(String.format("%14.10f\t", 0.0));
            System.out.println();
        }
        System.out.println();

        // Ly=b から y を求める (前進代入)
        double y[] = {0,0,0,0};
        for (int row = 0; row < N; row++) {
            for (int col = 0; col < row; col++)
                b[row] -= a[row][col] * y[col];
            y[row] = b[row];
        }
        // y の 値 を確認
        System.out.println("Y");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", y[i]));
        System.out.println("\n");

        // Ux=y から x を求める (後退代入)
        for (int row = N - 1; row >= 0; row--)
        {
            for (int col = N - 1; col > row; col--)
                y[row] -= a[row][col] * x[col];
            x[row] = y[row] / a[row][row];
        }
    }
    // 前進消去
    private static void forward_elimination(double[][] a, double[] b) {
        for (int pivot = 0; pivot < N - 1; pivot++) {
            for (int row = pivot + 1; row < N; row++) {
                double s = a[row][pivot] / a[pivot][pivot];
                for (int col = pivot; col < N; col++)
                    a[row][col] -= a[pivot][col] * s; // これが 上三角行列
                a[row][pivot] = s;                    // これが 下三角行列
                // b[row]    -= b[pivot] * s;         // この値は変更しない
            }
        }
    }
    // ピボット選択
    private static void pivoting(double[][] a, double[] b) {
        for(int pivot = 0; pivot < N; pivot++) {
            // 各列で 一番値が大きい行を 探す
            int     max_row =   pivot;
            double  max_val =   0;
            for (int row = pivot; row < N; row++) {
                if (Math.abs(a[row][pivot]) > max_val) {
                    // 一番値が大きい行
                    max_val =   Math.abs(a[row][pivot]);
                    max_row =   row;
                }
            }

            // 一番値が大きい行と入れ替え
            if (max_row != pivot) {
                double tmp;
                for (int col = 0; col < N; col++) {
                    tmp             =   a[max_row][col];
                    a[max_row][col] =   a[pivot][col];
                    a[pivot][col]   =   tmp;
                }
                tmp         =   b[max_row];
                b[max_row]  =   b[pivot];
                b[pivot]    =   tmp;
            }
        }
    }
    // 状態を確認
    private static void disp_progress(double[][] a, double[] b) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                System.out.print(String.format("%14.10f\t", a[i][j]));
            System.out.println(String.format("%14.10f\t", b[i]));
        }
        System.out.println();
    }
}
public class Java1006 {

    private static final int N = 4;

    // コレスキー法
    public static void main(String []args) {
        double[][] a = {{5,2,3,4},{2,10,6,7},{3,6,15,9},{4,7,9,20}};
        double[]   b = {34,68,96,125};
        System.out.println("A");
        disp_progress(a);

        // LL^T分解
        double[] x = {0,0,0,0};
        decomp(a,b,x);
        System.out.println("X");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", x[i]));
        System.out.println();
    }
    // LL^T分解
    private static void decomp(double[][] a, double[] b, double[] x) {
        // 前進消去
        forward_elimination(a,b);
        System.out.println("L と L^T");
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                if (j <= i)
                    System.out.print(String.format("%14.10f\t", a[i][j]));
                else
                    System.out.print(String.format("%14.10f\t", a[j][i]));
            System.out.println();
        }
        System.out.println();

        // Ly=b から y を求める (前進代入)
        double[] y = {0,0,0,0};
        for (int row = 0; row < N; row++) {
            for (int col = 0; col < row; col++)
                b[row] -= a[row][col] * y[col];
            y[row] = b[row] / a[row][row];
        }

        // y の 値 を確認
        System.out.println("Y");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", y[i]));
        System.out.println("\n");

        // Ux=y から x を求める (後退代入)
        for (int row = N - 1; row >= 0; row--) {
            for (int col = N - 1; col > row; col--)
                y[row] -= a[col][row] * x[col];
            x[row] = y[row] / a[row][row];
        }
    }
    // 前進消去
    private static void forward_elimination(double[][] a, double[] b) {
        for (int pivot = 0; pivot < N; pivot++) {
            double s = 0;
            for (int col = 0; col < pivot; col++)
                s += a[pivot][col] * a[pivot][col];
            // ここで根号の中が負の値になると計算できない!
            a[pivot][pivot] = Math.sqrt(a[pivot][pivot] - s);

            for (int row = pivot + 1; row < N; row++) {
                s = 0;
                for (int col = 0; col < pivot; col++)
                    s += a[row][col] * a[pivot][col];
                a[row][pivot] =  (a[row][pivot] - s) / a[pivot][pivot];
            }
        }
    }
    // 状態を確認
    private static void disp_progress(double[][] a) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                System.out.print(String.format("%14.10f\t", a[i][j]));
            System.out.println();
        }
        System.out.println();
    }
}
public class Java1007 {

    private static final int N = 4;

    // 修正コレスキー法
    public static void main(String []args) {
        double[][] a = {{5,2,3,4},{2,10,6,7},{3,6,15,9},{4,7,9,20}};
        double[]   b = {34,68,96,125};
        System.out.println("A");
        disp_progress(a);

        // LDL^T分解
        double[] x = {0,0,0,0};
        decomp(a,b,x);
        System.out.println("X");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", x[i]));
        System.out.println();
    }
    // LDL^T分解
    private static void decomp(double[][] a, double[] b, double[] x) {
        // 前進消去
        forward_elimination(a,b);
        System.out.println("L と D");
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                if (j <= i)
                    System.out.print(String.format("%14.10f\t", a[i][j]));
                else
                    System.out.print(String.format("%14.10f\t", a[j][i]));
            System.out.println();
        }
        System.out.println();

        // Ly=b から y を求める (前進代入)
        double[] y = {0,0,0,0};
        for (int row = 0; row < N; row++) {
            for (int col = 0; col < row; col++)
                b[row] -= a[row][col] * y[col];
            y[row] = b[row];
        }

        // y の 値 を確認
        System.out.println("Y");
        for (int i = 0; i < N; i++)
            System.out.print(String.format("%14.10f\t", y[i]));
        System.out.println("\n");

        // DL^Tx=y から x を求める (後退代入)
        for (int row = N - 1; row >= 0; row--) {
            for (int col = N - 1; col > row; col--)
                y[row] -= a[col][row] * a[row][row] * x[col];
            x[row] = y[row] / a[row][row];
        }
    }
    // 前進消去
    private static void forward_elimination(double[][] a, double[] b) {
        for (int pivot = 0; pivot < N; pivot++) {
            double s;

            // pivot < k の場合
            for (int col = 0; col < pivot; col++) {
                s = a[pivot][col];
                for (int k = 0; k < col; k++)
                    s -= a[pivot][k] * a[col][k] * a[k][k];
                a[pivot][col] = s / a[col][col];
            }

            // pivot == k の場合
            s = a[pivot][pivot];
            for (int k = 0; k < pivot; k++)
                s -= a[pivot][k] * a[pivot][k] * a[k][k];
            a[pivot][pivot] = s;
        }
    }
    // 状態を確認
    private static void disp_progress(double[][] a) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                System.out.print(String.format("%14.10f\t", a[i][j]));
            System.out.println();
        }
        System.out.println();
    }
}
inserted by FC2 system