ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# alyssap's solution

## to Difference Of Squares in the Scheme Track

Published at Sep 30 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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.

## Source

Problem 6 at Project Euler http://projecteuler.net/problem=6

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### difference-of-squares-test.scm

``````;; Load SRFI-64 lightweight testing specification
(use-modules (srfi srfi-64))

;; Suppress log file output. To write logs, comment out the following line:
(module-define! (resolve-module '(srfi srfi-64)) 'test-log-to-file #f)

;; Require module
(use-modules (squares))

(test-begin "difference-of-squares")

(test-eqv "square-of-sum-to-5"
225
(square-of-sum 5))
(test-eqv "sum-of-squares-to-5"
55
(sum-of-squares 5))
(test-eqv "difference-of-squares-to-5"
170
(difference 5))

(test-eqv "square-of-sum-to-10"
3025
(square-of-sum 10))
(test-eqv "sum-of-squares-to-10"
385
(sum-of-squares 10))
(test-eqv "difference-of-squares-to-10"
2640
(difference 10))

(test-eqv "square-of-sum-to-100"
25502500
(square-of-sum 100))
(test-eqv "sum-of-squares-to-100"
338350
(sum-of-squares 100))
(test-eqv "difference-of-squares-to-100"
25164150
(difference 100))

(test-end "difference-of-squares")``````
``````(define-module (squares)
#:export (sum-of-squares
square-of-sum
difference)

(define (sum-of-squares n)
(apply + (map (lambda (x) (* x x)) (iota (+ 1 n)))))

(define (square-of-sum n)
(let ((sum (apply + (iota (+ 1 n)))))
(* sum sum)))

(define (difference n)
(- (square-of-sum n) (sum-of-squares n)))``````