help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: puzzle with string permutations [photo]


From: Emanuel Berg
Subject: Re: puzzle with string permutations [photo]
Date: Tue, 07 Jun 2022 10:08:53 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

>>> (calc-eval "fact(5)") => "120"
>>> (calc-eval "fact(10)") => "3628800"
>>
>> Indeed, however that isn't lispy either ...
>
> (defun faculty (n)
>   (if (> n 1)
>       (* n (faculty (1- n)))
>     1) )
> ;; (faculty  5) ;       120
> ;; (faculty 10) ; 3 628 800

Perhaps better, as no recursion.

(require 'cl-lib)

(defun cl-faculty (n)
  (cl-loop with prod = 1
    for i from 2 to n do
    (setq prod (* i prod))
    finally return prod) )
;; (cl-faculty  5) ;       120
;; (cl-faculty 10) ; 3 628 800

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]