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 15:53:11 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

Emanuel Berg wrote:

>>>> (calc-eval "fact(5)") => "120"
>>>> (calc-eval "fact(10)") => "3628800"
>>>
>>> Indeed, however that isn't lispy either ...
>
> 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

Oh, that's right, we didn't think of duplicates. You can
remove them with `cl-remove-duplicates' like this

(cl-remove-duplicates
  (cl-remove-if-not (lambda (w) (spell-word w)) (string-perms str))
    :test #'string=)
                  ^^
( Indeed, Lisp is a fun language! =) )

but to do it without counting I think the formula is

  n!/(n - r)!

where n is the size of the set - here 26, as

  (length (alphabet t)) ; 26 [1]

and r is the number of items to be picked and arranged, or
here the length of the word.

This field of mathematics is called combinatorics BTW, maybe
because you combine a lot of stuff to solve it's problems
LOL :)

[1] https://dataswamp.org/~incal/emacs-init/abc.el

https://www.youtube.com/watch?v=6M3Kg1_5xFw

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




reply via email to

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