Skip to content

Commit

Permalink
add raindrops and perfect-numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrat555 committed Aug 19, 2018
1 parent 08cfb7e commit 6fa9fc7
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 0 deletions.
24 changes: 24 additions & 0 deletions perfect-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Perfect Numbers

Determine if a number is perfect, abundant, or deficient based on
Nicomachus' (60 - 120 CE) classification scheme for natural numbers.

The Greek mathematician [Nicomachus](https://en.wikipedia.org/wiki/Nicomachus) devised a classification scheme for natural numbers, identifying each as belonging uniquely to the categories of **perfect**, **abundant**, or **deficient** based on their [aliquot sum](https://en.wikipedia.org/wiki/Aliquot_sum). The aliquot sum is defined as the sum of the factors of a number not including the number itself. For example, the aliquot sum of 15 is (1 + 3 + 5) = 9

- **Perfect**: aliquot sum = number
- 6 is a perfect number because (1 + 2 + 3) = 6
- 28 is a perfect number because (1 + 2 + 4 + 7 + 14) = 28
- **Abundant**: aliquot sum > number
- 12 is an abundant number because (1 + 2 + 3 + 4 + 6) = 16
- 24 is an abundant number because (1 + 2 + 3 + 4 + 6 + 8 + 12) = 36
- **Deficient**: aliquot sum < number
- 8 is a deficient number because (1 + 2 + 4) = 7
- Prime numbers are deficient

Implement a way to determine whether a given number is **perfect**. Depending on your language track, you may also need to implement a way to determine whether a given number is **abundant** or **deficient**.
## Source

Taken from Chapter 2 of Functional Thinking by Neal Ford. [http://shop.oreilly.com/product/0636920029687.do](http://shop.oreilly.com/product/0636920029687.do)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
24 changes: 24 additions & 0 deletions perfect-numbers/perfect-numbers-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
;;; perfect-numbers-test.el --- Tests for perfect-numbers (exercism)

;;; Commentary:

;;; Code:

(load-file "perfect-numbers.el")


(ert-deftest no-perfect-numbers-in-1-to-5 ()
(should (equal (perfect-numbers 5) '())))

(ert-deftest return-one-perfect-number-for-range-1-to-6 ()
(should (equal (perfect-numbers 6) '(6))))

(ert-deftest return-3-perfect-numbers-for-range-1-to-1000 ()
(should (equal (perfect-numbers 1000) '(6 28 496))))

(ert-deftest return-4-perfect-numbers-for-range-1-to-10000 ()
(should (equal (perfect-numbers 10000) '(6 28 496 8128))))


(provide 'perfect-numbers)
;;; perfect-numbers-test.el ends here
30 changes: 30 additions & 0 deletions perfect-numbers/perfect-numbers.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
;;; perfect-numbers.el --- perfect-numbers Exercise (exercism)

;;; Commentary:

;;; Code:

(defun perfect-numbers(x)
"Finds perfect numbers up to a given number"
(let ((result '())
(number x))
(while (>= number 2)
(if (perfect? number)
(setq result (cons number result)))
(setq number (1- number)))
result))

(defun perfect?(x)
"Checks if a number is perfect"
(let ((upper-limit (1+ (/ x 2)))
(count 2)
(factors '(1)))
(while (<= count upper-limit)
(if (= (% x count) 0)
(setq factors (cons count factors)))
(setq count (1+ count)))
(= x (apply '+ factors))))


(provide 'perfect-numbers)
;;; perfect-numbers.el ends here
24 changes: 24 additions & 0 deletions raindrops/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Raindrops

Convert a number to a string, the contents of which depend on the number's factors.

- If the number has 3 as a factor, output 'Pling'.
- If the number has 5 as a factor, output 'Plang'.
- If the number has 7 as a factor, output 'Plong'.
- If the number does not have 3, 5, or 7 as a factor,
just pass the number's digits straight through.

## Examples

- 28's factors are 1, 2, 4, **7**, 14, 28.
- In raindrop-speak, this would be a simple "Plong".
- 30's factors are 1, 2, **3**, **5**, 6, 10, 15, 30.
- In raindrop-speak, this would be a "PlingPlang".
- 34 has four factors: 1, 2, 17, and 34.
- In raindrop-speak, this would be "34".
## Source

A variation on a famous interview question intended to weed out potential candidates. [http://jumpstartlab.com](http://jumpstartlab.com)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
70 changes: 70 additions & 0 deletions raindrops/raindrops-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
;;; raindrops-test.el --- Tests for Raindrops (exercism)

;;; Commentary:

;;; Code:

(load-file "raindrops.el")

(ert-deftest test-1 ()
(should (equal "1"
(convert 1))))

(ert-deftest test-3 ()
(should (equal "Pling"
(convert 3))))

(ert-deftest test-5 ()
(should (equal "Plang"
(convert 5))))

(ert-deftest test-7 ()
(should (equal "Plong"
(convert 7))))

(ert-deftest test-6 ()
(should (equal "Pling"
(convert 6))))

(ert-deftest test-9 ()
(should (equal "Pling"
(convert 9))))

(ert-deftest test-10 ()
(should (equal "Plang"
(convert 10))))

(ert-deftest test-15 ()
(should (equal "PlingPlang"
(convert 15))))

(ert-deftest test-21 ()
(should (equal "PlingPlong"
(convert 21))))

(ert-deftest test-25 ()
(should (equal "Plang"
(convert 25))))

(ert-deftest test-35 ()
(should (equal "PlangPlong"
(convert 35))))

(ert-deftest test-49 ()
(should (equal "Plong"
(convert 49))))

(ert-deftest test-52 ()
(should (equal "52"
(convert 52))))

(ert-deftest test-105 ()
(should (equal "PlingPlangPlong"
(convert 105))))

(ert-deftest test-12121 ()
(should (equal "12121"
(convert 12121))))

(provide 'raindrops-test)
;;; raindrops-test.el ends here
22 changes: 22 additions & 0 deletions raindrops/raindrops.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
;;; raindrops.el --- Raindrops (exercism)

;;; Commentary:

;;; Code:


(defun convert (n)
"Convert integer N to its raindrops string."
(let* ((raindrops '((3 . "Pling") (5 . "Plang") (7 . "Plong")))
(result (mapconcat #'(lambda (x) (let ((factor (car x))
(dropname (cdr x)))
(if (= (% n factor) 0)
dropname
"")))
raindrops "")))
(if (string-equal "" result)
(number-to-string n)
result)))

(provide 'raindrops)
;;; raindrops.el ends here

0 comments on commit 6fa9fc7

Please sign in to comment.