unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: puzzle with string permutations [photo]
Date: Tue, 07 Jun 2022 15:53:11 +0200	[thread overview]
Message-ID: <87bkv4r35k.fsf@dataswamp.org> (raw)
In-Reply-To: 87bkv4sxnu.fsf@dataswamp.org

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	other threads:[~2022-06-07 13:53 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-07  6:39 puzzle with string permutations [photo] Emanuel Berg
2022-06-07  7:12 ` Marcin Borkowski
2022-06-07  7:54   ` Emanuel Berg
2022-06-07  7:58     ` Emanuel Berg
2022-06-07  8:08       ` Emanuel Berg
2022-06-07 13:53         ` Emanuel Berg [this message]
2022-06-07 14:18           ` Emanuel Berg
2022-06-07 16:09             ` Yuri Khan
2022-06-07 22:04               ` Emanuel Berg
2022-06-08  0:17               ` Emanuel Berg
2022-06-08  2:52                 ` missing Lisp world (was: Re: puzzle with string permutations [photo]) Emanuel Berg
2022-06-07  8:37   ` puzzle with string permutations [photo] Emanuel Berg
2022-06-07 13:28     ` Emanuel Berg
2022-08-01  6:39 ` Marcin Borkowski
2022-08-01  7:39   ` Emanuel Berg
2022-08-02 12:21   ` Jean Louis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bkv4r35k.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).