-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivisibleBy
67 lines (53 loc) · 1.55 KB
/
divisibleBy
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
59
60
61
62
63
64
65
66
67
#+BEGIN_SRC emacs-lisp
(defun is-divisible-by-2 (n)
(let* ((digits-as-string (number-to-string n))
(last-digit (elt digits-as-string (- (length digits-as-string) 1))))
(or (= last-digit ?0)
(= last-digit ?2)
(= last-digit ?4)
(= last-digit ?6)
(= last-digit ?8))))
#+END_SRC
#+BEGIN_SRC emacs-lisp :results silent
(defun sum-of-digits (number)
(apply '+ (mapcar (lambda (d) (- d ?0)) (number-to-string number))))
#+END_SRC
#+BEGIN_SRC emacs-lisp
(sum-of-digits 1598795)
#+END_SRC
#+RESULTS:
: 44
#+BEGIN_SRC emacs-lisp :results silent
(defun is-divisible-by-3 (number)
"Checks divisibility by 3 by a simple rule.
Does not work for negative numbers."
(or (= number 3)
(= number 6)
(= number 9)
(zerop number)
(unless (< number 10)
(is-divisible-by-3 (sum-of-digits number)))))
#+END_SRC
#+BEGIN_SRC emacs-lisp
(is-divisible-by-3 871987398719952888)
#+END_SRC
#+RESULTS:
: t
#+BEGIN_SRC emacs-lisp
(defun is-divisible-by-4 (number)
(let* ((digit-as-string (number-to-string number))
(last-digit (elt digits-as-string (- (length digits-as-string) 2))))
(or (= last-digit ?
#+END_SRC
#+BEGIN_SRC emacs-lisp
(defun is-divisible-by-5(n)
(let* ((digits-as-string (numer-to-string n))
(last-digit (elt digits-as-string (- (length digits-as-string) 1 ))))
(or (= last-digit ?0)
(= last-digit ?5))))
#+END_SRC
#+RESULTS:
: is-divisible-by-5
#+BEGIN_SRC emacs-lisp
(is-divisible-by-5 7827563929293005)
#+END_SRC