1
0
Fork 0

new solution: emacs-lisp, difference-of-squares.

main
Gökmen Görgen 2 months ago
parent a9ec7a2f64
commit 4cee3fef55
  1. 41
      emacs-lisp/difference-of-squares/HELP.md
  2. 38
      emacs-lisp/difference-of-squares/README.md
  3. 40
      emacs-lisp/difference-of-squares/difference-of-squares-test.el
  4. 21
      emacs-lisp/difference-of-squares/difference-of-squares.el

@ -0,0 +1,41 @@
# Help
## Running the tests
Tests can be run several ways:
1. Interactively and individually, with `M-x ert RET test-name RET`
2. Interactively and all at once, with `M-x ert RET t RET`
3. From the terminal, in batch mode, with `emacs -batch -l ert -l my-test.el -f ert-run-tests-batch-and-exit`
4. Other options can be found in the docs, `C-h i m ert RET`
## Submitting your solution
You can submit your solution using the `exercism submit difference-of-squares.el` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Emacs Lisp track's documentation](https://exercism.org/docs/tracks/emacs-lisp)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [The Emacs Wiki](http://emacswiki.org/) is invaluable. Spend lots of time here.
- [The Emacs Editor](http://www.gnu.org/software/emacs/manual/html_node/emacs/index.html) is the official manual for GNU Emacs.
- IRC - there are [freenode](https://freenode.net/) channels for `#emacs`, `#prelude`, and many Emacs
packages, and many helpful folks around. And with emacs, IRC is as close as
`M-x erc`.
- [Exercism Support](https://gitter.im/exercism/support) Gitter chat is also a good place to get help from the
exercism community.
- [StackOverflow](http://stackoverflow.com/questions/tagged/elisp) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

@ -0,0 +1,38 @@
# Difference Of Squares
Welcome to Difference Of Squares on Exercism's Emacs Lisp Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.
The square of the sum of the first ten natural numbers is
(1 + 2 + ... + 10)² = 55² = 3025.
The sum of the squares of the first ten natural numbers is
1² + 2² + ... + 10² = 385.
Hence the difference between the square of the sum of the first
ten natural numbers and the sum of the squares of the first ten
natural numbers is 3025 - 385 = 2640.
You are not expected to discover an efficient solution to this yourself from
first principles; research is allowed, indeed, encouraged. Finding the best
algorithm for the problem is a key skill in software engineering.
## Source
### Created by
- @canweriotnow
### Contributed to by
- @Scientifica96
- @vermiculus
- @yurrriq
### Based on
Problem 6 at Project Euler - http://projecteuler.net/problem=6

@ -0,0 +1,40 @@
;;; difference-of-squares-test.el --- Tests for difference-of-squares (exercism)
;;; Commentary:
;;; Code:
(load-file "difference-of-squares.el")
(ert-deftest square-of-sum-to-5 ()
(should (= 225 (square-of-sum 5))))
(ert-deftest sum-of-squares-to-5 ()
(should (= 55 (sum-of-squares 5))))
(ert-deftest difference-of-squares-to-5 ()
(should (= 170 (difference 5))))
(ert-deftest square-of-sum-to-10 ()
(should (= 3025 (square-of-sum 10))))
(ert-deftest sum-of-squares-to-10 ()
(should (= 385 (sum-of-squares 10))))
(ert-deftest difference-of-squares-to-10 ()
(should (= 2640 (difference 10))))
(ert-deftest square-of-sum-to-100 ()
(should (= 25502500 (square-of-sum 100))))
(ert-deftest sum-of-squares-to-100 ()
(should (= 338350 (sum-of-squares 100))))
(ert-deftest difference-of-squares-to-100 ()
(should (= 25164150 (difference 100))))
(provide 'difference-of-squares-test)
;;; difference-of-squares-test.el ends here

@ -0,0 +1,21 @@
;;; difference-of-squares.el --- Difference of Squares (exercism)
;;; Commentary:
;;; Code:
(require 'cl-lib)
(defun square-of-sum (num)
"Return the square of the sum of the first N numbers."
(expt (cl-reduce '+ (cl-loop for i from 1 upto num collect i)) 2))
(defun sum-of-squares (num)
"Return the sum of the squares of the first N numbers."
(cl-reduce '+ (cl-loop for i from 1 upto num collect (expt i 2))))
(defun difference (num)
"Return the difference between the square of the sum of the first N numbers and the sum of the squares of the first N numbers."
(- (square-of-sum num) (sum-of-squares num)))
(provide 'difference-of-squares)
;;; difference-of-squares.el ends here
Loading…
Cancel
Save