How to resolve the algorithm Left factorials step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Left factorials step by step in the Java programming language

Table of Contents

Problem Statement

Left factorials,   !n,   may refer to either   subfactorials   or to   factorial sums; the same notation can be confusingly seen being used for the two different definitions. Sometimes,   subfactorials   (also known as derangements)   may use any of the notations:

(It may not be visually obvious, but the last example uses an upside-down exclamation mark.)

This Rosetta Code task will be using this formula   (factorial sums)   for   left factorial:

Display the left factorials for:

Display the length (in decimal digits) of the left factorials for:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Left factorials step by step in the Java programming language

Explanation:

1. Factorial Computation:

  • factorial(BigInteger n): Calculates the factorial of n.
  • It starts with ans as 1 and iterates from 1 to n, multiplying ans by x at each step.
  • The result is the factorial of n, represented as a BigInteger object.

2. Left Factorial:

  • leftFact(BigInteger n): Calculates the "left factorial" of n.
  • It starts with ans as 0 and iterates from 0 to n-1.
  • For each k, it calls factorial(k) and adds the result to ans.
  • The result is the sum of factorials from 0 to n-1, which is known as the left factorial of n.

3. Main Method:

  • Tests the leftFact function for various inputs:
    • Prints the left factorials of numbers from 0 to 10.
    • Prints the left factorials of numbers from 20 to 110 in steps of 10.
    • Prints the number of digits in the left factorials of numbers from 1000 to 10000 in steps of 1000.

Note: The program uses BigInteger objects to handle large factorials that cannot fit into standard data types.

Source code in the java programming language

import java.math.BigInteger;

public class LeftFac{
	public static BigInteger factorial(BigInteger n){
		BigInteger ans = BigInteger.ONE;
		for(BigInteger x = BigInteger.ONE; x.compareTo(n) <= 0; x = x.add(BigInteger.ONE)){
			ans = ans.multiply(x);
		}
		return ans;
	}
	
	public static BigInteger leftFact(BigInteger n){
		BigInteger ans = BigInteger.ZERO;
		for(BigInteger k = BigInteger.ZERO; k.compareTo(n.subtract(BigInteger.ONE)) <= 0; k = k.add(BigInteger.ONE)){
			ans = ans.add(factorial(k));
		}
		return ans;
	}
	
	public static void main(String[] args){
		for(int i = 0; i <= 10; i++){
			System.out.println("!" + i + " = " + leftFact(BigInteger.valueOf(i)));
		}
		
		for(int i = 20; i <= 110; i += 10){
			System.out.println("!" + i + " = " + leftFact(BigInteger.valueOf(i)));
		}
		
		for(int i = 1000; i <= 10000; i += 1000){
			System.out.println("!" + i + " has " + leftFact(BigInteger.valueOf(i)).toString().length() + " digits");
		}
	}
}


  

You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the Perl programming language
You may also check:How to resolve the algorithm Faulhaber's formula step by step in the C programming language
You may also check:How to resolve the algorithm Last Friday of each month step by step in the Swift programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Nim programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Seed7 programming language