Created
December 30, 2010 04:08
-
-
Save dydx/759438 to your computer and use it in GitHub Desktop.
excerpt from "nifty-funs.lisp"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun euler-totient (n) | |
| "Returns the euler-totient, and the coprimes that count the totient | |
| for any number n" | |
| (let ((coprimes (loop for x from 1 to n when (= 1 (gcd x n)) collect x))) | |
| (values (length coprimes) coprimes))) | |
| (defun part-k-n (k n) | |
| (let ((cache (make-hash-table :test #'equal))) | |
| (labels ((aux (k n cache) | |
| (multiple-value-bind (val win) (gethash (list k n) cache) | |
| (if win | |
| val | |
| (setf (gethash (list k n) cache) | |
| (cond ((> k n) 0) | |
| ((= k n) 1) | |
| (t (+ (part-k-n (+ k 1) n) (part-k-n k (- n k)))))))))) | |
| (aux k n cache)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment