How to resolve the algorithm Continued fraction step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Continued fraction step by step in the Java programming language

Table of Contents

Problem Statement

The task is to write a program which generates such a number and prints a real representation of it. The code should be tested by calculating and printing the square root of 2, Napier's Constant, and Pi, using the following coefficients: For the square root of 2, use

a

0

= 1

{\displaystyle a_{0}=1}

then

a

N

= 2

{\displaystyle a_{N}=2}

.

b

N

{\displaystyle b_{N}}

is always

1

{\displaystyle 1}

. For Napier's Constant, use

a

0

= 2

{\displaystyle a_{0}=2}

, then

a

N

= N

{\displaystyle a_{N}=N}

.

b

1

= 1

{\displaystyle b_{1}=1}

then

b

N

= N − 1

{\displaystyle b_{N}=N-1}

. For Pi, use

a

0

= 3

{\displaystyle a_{0}=3}

then

a

N

= 6

{\displaystyle a_{N}=6}

.

b

N

= ( 2 N − 1

)

2

{\displaystyle b_{N}=(2N-1)^{2}}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Continued fraction step by step in the Java programming language

The provided Java code defines a class Test that includes a static method calc and a main method. It uses lambda expressions to define functions and demonstrates how to calculate values based on these functions.

  • calc method:

    • This method takes a function f that maps an integer to an array of integers and an integer n as input.
    • It initializes a double variable temp to 0.
    • It iterates from n down to 1. For each value of ni, it applies the function f to ni and obtains an array of integers p.
    • It updates temp by dividing p[1] by (p[0] + temp).
    • Finally, it returns f.apply(0)[0] + temp, which effectively calculates a value based on the function f and n.
  • main method:

    • Creates a list fList of functions using lambda expressions. Each lambda expression defines a function that maps an integer to an array of integers.
    • Iterates through the list fList and for each function f, it invokes the calc method with f and 200 as arguments.
    • The result of each calculation is then printed to the console.

In summary, this code demonstrates how to use functions in Java and how to calculate values based on these functions. It uses lambda expressions to define functions, iterates over a list of functions, and performs calculations using a provided integer n.

Source code in the java programming language

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));
    }
}


  

You may also check:How to resolve the algorithm Currying step by step in the Delphi programming language
You may also check:How to resolve the algorithm Least common multiple step by step in the Objeck programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the Brat programming language
You may also check:How to resolve the algorithm Arithmetic/Rational step by step in the PL/I programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Fortran programming language