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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Left factorials step by step in the Clojure 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 Clojure programming language

Source code in the clojure programming language

(ns left-factorial
  (:gen-class))

(defn left-factorial [n]
  " Compute by updating the state [fact summ] for each k, where k equals 1 to n
    Update is next state is [k*fact (summ+k)"
  (second
    (reduce (fn [[fact summ] k]
              [(*' fact k) (+ summ fact)])
            [1 0] (range 1 (inc n)))))

(doseq [n (range 11)]
  (println (format "!%-3d = %5d" n (left-factorial n))))

(doseq [n (range 20 111 10)]
(println (format "!%-3d = %5d" n (biginteger (left-factorial n)))))

(doseq [n (range 1000 10001 1000)]
  (println (format "!%-5d has %5d digits" n (count (str (biginteger (left-factorial n)))))))


  

You may also check:How to resolve the algorithm AKS test for primes step by step in the R programming language
You may also check:How to resolve the algorithm Almost prime step by step in the PHP programming language
You may also check:How to resolve the algorithm Colour bars/Display step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Loops/For step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Jensen's Device step by step in the JavaScript programming language