2019-10-09 13:13:56 +02:00
|
|
|
;;; series.el --- Hosting common series of numbers -*- lexical-binding: t -*-
|
2020-09-01 11:17:43 +02:00
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
;; Author: William Carroll <wpcarro@gmail.com>
|
2020-09-01 11:17:43 +02:00
|
|
|
;; Version: 0.0.1
|
|
|
|
;; Package-Requires: ((emacs "24"))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; Encoding number series as I learn about them.
|
|
|
|
;;
|
|
|
|
;; These are the following series I'm interested in supporting:
|
|
|
|
;; - Fibonacci
|
|
|
|
;; - Catalan numbers
|
|
|
|
;; - Figurate number series
|
|
|
|
;; - Triangular
|
|
|
|
;; - Square
|
|
|
|
;; - Pentagonal
|
|
|
|
;; - Hexagonal
|
|
|
|
;; - Lazy-caterer
|
|
|
|
;; - Magic square
|
|
|
|
;; - Look-and-say
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2019-12-11 11:31:57 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Dependencies
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2019-10-09 13:13:56 +02:00
|
|
|
(require 'number)
|
|
|
|
|
2019-12-11 11:31:57 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; Library
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun series-range (beg end)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Create a list of numbers from `BEG' to `END'.
|
|
|
|
This is an inclusive number range."
|
|
|
|
(if (< end beg)
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-reverse
|
2019-10-09 13:13:56 +02:00
|
|
|
(number-sequence end beg))
|
|
|
|
(number-sequence beg end)))
|
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun series-fibonacci-number (i)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the number in the fibonacci series at `I'."
|
|
|
|
(cond
|
|
|
|
((= 0 i) 0)
|
|
|
|
((= 1 i) 1)
|
2020-09-01 11:17:43 +02:00
|
|
|
(t (+ (series-fibonacci-number (- i 1))
|
|
|
|
(series-fibonacci-number (- i 2))))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun series-fibonacci (n)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the first `N' numbers of the fibonaccci series starting at zero."
|
|
|
|
(if (= 0 n)
|
|
|
|
'()
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-reverse
|
|
|
|
(list-cons (series-fibonacci-number (number-dec n))
|
|
|
|
(list-reverse
|
|
|
|
(series-fibonacci (number-dec n)))))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;; TODO: Consider memoization.
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun series-triangular-number (i)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the number in the triangular series at `I'."
|
|
|
|
(if (= 0 i)
|
|
|
|
0
|
2020-09-01 11:17:43 +02:00
|
|
|
(+ i (series-triangular-number (number-dec i)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
;; TODO: Improve performance.
|
|
|
|
;; TODO: Consider creating a stream protocol with `stream/next' and implement
|
|
|
|
;; this using that.
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun series-triangular (n)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the first `N' numbers of a triangular series starting at 0."
|
|
|
|
(if (= 0 n)
|
|
|
|
'()
|
2020-09-01 11:17:43 +02:00
|
|
|
(list-reverse
|
|
|
|
(list-cons (series-triangular-number (number-dec n))
|
|
|
|
(list-reverse
|
|
|
|
(series-triangular (number-dec n)))))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun series-catalan-number (i)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the catalan number in the series at `I'."
|
|
|
|
(if (= 0 i)
|
|
|
|
1
|
2020-09-01 11:17:43 +02:00
|
|
|
(/ (number-factorial (* 2 i))
|
|
|
|
(* (number-factorial (number-inc i))
|
|
|
|
(number-factorial i)))))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
2020-09-01 11:17:43 +02:00
|
|
|
(defun series-catalan (n)
|
2019-10-09 13:13:56 +02:00
|
|
|
"Return the first `N' numbers in a catalan series."
|
2020-09-01 11:17:43 +02:00
|
|
|
(->> (series-range 0 (number-dec n))
|
|
|
|
(list-map #'series-catalan-number)))
|
2019-10-09 13:13:56 +02:00
|
|
|
|
|
|
|
(provide 'series)
|
|
|
|
;;; series.el ends here
|