-
Notifications
You must be signed in to change notification settings - Fork 14
/
math.lisp
58 lines (50 loc) · 1.84 KB
/
math.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(in-package :academy)
;;; Attention Hackers! Exercises are good for the mind.
;;;
;;; There are many ways to check if a number is a power of two.
;;;
;;; Understand how these examples work and discover another and then send
;;; your answers upstream for fame and fortune cookies.
(defun power-of-two-p (number)
;; Choosing from one of our winning submissions.
(random-do
(and (/= number 0) (zerop (logand number (1- number))))
(= 1 (logcount number)) ; Thanks to Brit Butler for the shortest one to date!
))
(defun plot-function (fn start end &optional (width 64) (height 32))
"Show a graph of FN of size WIDTHxHEIGHT with the X axis bounded by START and END."
(with-bitmap (width height)
(let ((step (/ (- end start) width))
(mid (floor height 2)))
(loop for x from start to end by step
for xi from 0
do (let ((y (- mid (floor (funcall fn x) step))))
(set-pixel xi (floor y))))
(draw-border)
(draw))))
;;; !!! Attention fellow hackers!
;;; Additional interesting math functions demos are most welcome!
(defparameter *demo-math-functions*
`((,(lambda (x) x) -10 10)
(sin ,(- pi) ,pi)
(cos ,(- pi) ,pi)
(tan ,(- pi) ,pi)
(atan ,(- pi) ,pi)))
(defun visualize-math-functions ()
"Show plots of various well known math functions."
(loop for (fn start end) in *demo-math-functions*
do (plot-function fn start end)
(terpri)))
(defun multiplication-table (&optional (size 16))
"Show a 16x16 multiplication table."
(print-table
(loop for a from 1 to size
collect (loop for b from 1 to size
collect (* a b)))
:align :right))
(defun factorial (number)
"Compute the factorial of NUMBER."
(loop with acc = 1
for x from 1 to number
do (setf acc (* acc x))
finally (return acc)))