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.
While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.
Use the following commands to ensure VIM uses only spaces for indentation:
:set tabstop=2 :set shiftwidth=2 :set expandtab
(or as a oneliner
:set tabstop=2 shiftwidth=2 expandtab). This can
be added to your
~/.vimrc file to use it all the time.
Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:
(setq-default indent-tabs-mode nil)
This can be placed in your
order to have it set whenever Emacs is launched.
One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.
Problem 6 at Project Euler http://projecteuler.net/problem=6
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
(ql:quickload "lisp-unit") #-xlisp-test (load "difference-of-squares") (defpackage #:squares-test (:use #:cl #:lisp-unit)) (in-package #:squares-test) (define-test square-of-sum-to-5 (assert-equal 225 (squares:square-of-sum 5))) (define-test sum-of-squares-to-5 (assert-equal 55 (squares:sum-of-squares 5))) (define-test difference-of-sums-to-5 (assert-equal 170 (squares:difference 5))) (define-test square-of-sum-to-10 (assert-equal 3025 (squares:square-of-sum 10))) (define-test sum-of-squares-to-10 (assert-equal 385 (squares:sum-of-squares 10))) (define-test difference-of-sums-to-10 (assert-equal 2640 (squares:difference 10))) (define-test square-of-sum-to-100 (assert-equal 25502500 (squares:square-of-sum 100))) (define-test sum-of-squares-to-100 (assert-equal 338350 (squares:sum-of-squares 100))) (define-test difference-of-sums-to-100 (assert-equal 25164150 (squares:difference 100))) #-xlisp-test (let ((*print-errors* t) (*print-failures* t)) (run-tests :all :squares-test))
(defpackage #:squares (:use #:cl) (:export #:sum-of-squares #:square-of-sum #:difference)) (in-package #:squares) (defun range (end) (loop for n from 1 below (1+ end) by 1 collect n)) (defun square-of-sum (n) (let ((sum (reduce #'+ (range n)))) (expt sum 2))) (defun sum-of-squares (n) (reduce #'+ (map 'list #'(lambda (x) (expt x 2)) (range n)))) (defun difference (n) (abs (- (sum-of-squares n) (square-of-sum n))))
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.