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

Published on 12 May 2024 09:40 PM

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

Source code in the common programming language

(defun fact (n)
  (reduce #'* (loop for i from 1 to n collect i)))

(defun left-fac (n)
  (reduce #'+ (loop for i below n collect (fact i))))

(format t "0 -> 10~&")
(format t "~a~&" (loop for i upto 10 collect (left-fac i)))
(format t "20 -> 110 by 10~&")
(format t "~{~a~&~}" (loop for i from 20 upto 110 by 10 collect (left-fac i)))
(format t "1000 -> 10000 by 1000~&")
(format t "~{~a digits~&~}" (loop for i from 1000 upto 10000 by 1000 collect (length (format nil "~a" (left-fac i)))))


  

You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the REXX programming language
You may also check:How to resolve the algorithm Binary digits step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Discordian date step by step in the Pike programming language
You may also check:How to resolve the algorithm Huffman coding step by step in the Java programming language
You may also check:How to resolve the algorithm Vogel's approximation method step by step in the Raku programming language