37bd082d9946

Problem 6
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Sun, 10 Apr 2016 01:39:44 +0000
parents 2e707232cee0
children 3512c67e0138
branches/tags (none)
files euler.lisp

Changes

--- a/euler.lisp	Sun Apr 10 01:27:54 2016 +0000
+++ b/euler.lisp	Sun Apr 10 01:39:44 2016 +0000
@@ -30,6 +30,9 @@
   (loop :for i :from from :below below
         :collect i))
 
+(defun square (n)
+  (* n n))
+
 
 ;;;; Problems
 (defun problem-1 ()
@@ -100,3 +103,24 @@
           :when (every (lambda (n) (dividesp i n))
                        divisors)
           :return i)))
+
+(defun problem-6 ()
+  ;; The sum of the squares of the first ten natural numbers is,
+  ;;   1^2 + 2^2 + ... + 10^2 = 385
+  ;;
+  ;; The square of the sum of the first ten natural numbers is,
+  ;;   (1 + 2 + ... + 10)^2 = 55^2 = 3025
+  ;;
+  ;; Hence the difference between the sum of the squares of the first ten
+  ;; natural numbers and the square of the sum is 3025 − 385 = 2640.
+  ;;
+  ;; Find the difference between the sum of the squares of the first one hundred
+  ;; natural numbers and the square of the sum.
+  (flet ((sum-of-squares (to)
+           (loop :for i :from 1 :to to
+                 :sum (square i)))
+         (square-of-sum (to)
+           (square (loop :for i :from 1 :to to
+                         :sum i))))
+    (abs (- (sum-of-squares 100) ; apparently it wants the absolute value
+            (square-of-sum 100)))))