This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add difference-of-squares exercise (#122)
- Loading branch information
1 parent
f11c044
commit aed9b4c
Showing
8 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
exercises/practice/difference-of-squares/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# 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. |
19 changes: 19 additions & 0 deletions
19
exercises/practice/difference-of-squares/.meta/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"ErikSchierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/difference_of_squares.cljs" | ||
], | ||
"test": [ | ||
"test/difference_of_squares_test.cljs" | ||
], | ||
"example": [ | ||
".meta/src/example.cljs" | ||
] | ||
}, | ||
"blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.", | ||
"source": "Problem 6 at Project Euler", | ||
"source_url": "https://projecteuler.net/problem=6" | ||
} |
12 changes: 12 additions & 0 deletions
12
exercises/practice/difference-of-squares/.meta/src/example.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
(ns difference-of-squares) | ||
|
||
(defn- sum [xs] (reduce + xs)) | ||
|
||
(defn sum-of-squares [n] | ||
(sum (map #(int (Math/pow % 2)) (range 0 (inc n))))) | ||
|
||
(defn square-of-sum [n] | ||
(int (Math/pow (sum (range 0 (inc n))) 2))) | ||
|
||
(defn difference [x] | ||
(- (square-of-sum x) (sum-of-squares x))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This is an auto-generated file. Regular comments will be removed when this | ||
# file is regenerated. Regenerating will not touch any manually added keys, | ||
# so comments can be added in a "comment" key. | ||
|
||
[e46c542b-31fc-4506-bcae-6b62b3268537] | ||
description = "square of sum 1" | ||
|
||
[9b3f96cb-638d-41ee-99b7-b4f9c0622948] | ||
description = "square of sum 5" | ||
|
||
[54ba043f-3c35-4d43-86ff-3a41625d5e86] | ||
description = "square of sum 100" | ||
|
||
[01d84507-b03e-4238-9395-dd61d03074b5] | ||
description = "sum of squares 1" | ||
|
||
[c93900cd-8cc2-4ca4-917b-dd3027023499] | ||
description = "sum of squares 5" | ||
|
||
[94807386-73e4-4d9e-8dec-69eb135b19e4] | ||
description = "sum of squares 100" | ||
|
||
[44f72ae6-31a7-437f-858d-2c0837adabb6] | ||
description = "difference of squares 1" | ||
|
||
[005cb2bf-a0c8-46f3-ae25-924029f8b00b] | ||
description = "difference of squares 5" | ||
|
||
[b1bf19de-9a16-41c0-a62b-1f02ecc0b036] | ||
description = "difference of squares 100" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{:deps | ||
{org.clojure/clojure {:mvn/version "1.10.1"} | ||
org.clojure/clojurescript {:mvn/version "1.10.773"}} | ||
|
||
:aliases | ||
{:test | ||
{:extra-paths ["test"] | ||
:extra-deps | ||
{olical/cljs-test-runner {:mvn/version "3.8.0"}} | ||
:main-opts ["-m" "cljs-test-runner.main"]}}} |
13 changes: 13 additions & 0 deletions
13
exercises/practice/difference-of-squares/src/difference_of_squares.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(ns difference-of-squares) | ||
|
||
(defn difference [] ;; <- arglist goes here | ||
;; your code goes here | ||
) | ||
|
||
(defn sum-of-squares [] ;; <- arglist goes here | ||
;; your code goes here | ||
) | ||
|
||
(defn square-of-sum [] ;; <- arglist goes here | ||
;; your code goes here | ||
) |
30 changes: 30 additions & 0 deletions
30
exercises/practice/difference-of-squares/test/difference_of_squares_test.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
(ns difference-of-squares-test | ||
(:require [clojure.test :refer [deftest is]] | ||
[difference-of-squares :as dos])) | ||
|
||
(deftest square-of-sum-to-5 | ||
(is (= 225 (dos/square-of-sum 5)))) | ||
|
||
(deftest sum-of-squares-to-5 | ||
(is (= 55 (dos/sum-of-squares 5)))) | ||
|
||
(deftest difference-of-squares-to-5 | ||
(is (= 170 (dos/difference 5)))) | ||
|
||
(deftest square-of-sum-to-10 | ||
(is (= 3025 (dos/square-of-sum 10)))) | ||
|
||
(deftest sum-of-squares-to-10 | ||
(is (= 385 (dos/sum-of-squares 10)))) | ||
|
||
(deftest difference-of-squares-to-10 | ||
(is (= 2640 (dos/difference 10)))) | ||
|
||
(deftest square-of-sum-to-100 | ||
(is (= 25502500 (dos/square-of-sum 100)))) | ||
|
||
(deftest sum-of-squares-to-100 | ||
(is (= 338350 (dos/sum-of-squares 100)))) | ||
|
||
(deftest difference-of-squares-to-100 | ||
(is (= 25164150 (dos/difference 100)))) |