forked from exercism/idris
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (exercism#152)
- Loading branch information
1 parent
1ddee7a
commit 296bc9e
Showing
11 changed files
with
159 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": [ | ||
"keiravillekode" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/DifferenceOfSquares.idr" | ||
], | ||
"test": [ | ||
"test/src/Main.idr" | ||
], | ||
"example": [ | ||
"example/DifferenceOfSquares.idr" | ||
] | ||
}, | ||
"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" | ||
} |
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,37 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[e46c542b-31fc-4506-bcae-6b62b3268537] | ||
description = "Square the sum of the numbers up to the given number -> square of sum 1" | ||
|
||
[9b3f96cb-638d-41ee-99b7-b4f9c0622948] | ||
description = "Square the sum of the numbers up to the given number -> square of sum 5" | ||
|
||
[54ba043f-3c35-4d43-86ff-3a41625d5e86] | ||
description = "Square the sum of the numbers up to the given number -> square of sum 100" | ||
|
||
[01d84507-b03e-4238-9395-dd61d03074b5] | ||
description = "Sum the squares of the numbers up to the given number -> sum of squares 1" | ||
|
||
[c93900cd-8cc2-4ca4-917b-dd3027023499] | ||
description = "Sum the squares of the numbers up to the given number -> sum of squares 5" | ||
|
||
[94807386-73e4-4d9e-8dec-69eb135b19e4] | ||
description = "Sum the squares of the numbers up to the given number -> sum of squares 100" | ||
|
||
[44f72ae6-31a7-437f-858d-2c0837adabb6] | ||
description = "Subtract sum of squares from square of sums -> difference of squares 1" | ||
|
||
[005cb2bf-a0c8-46f3-ae25-924029f8b00b] | ||
description = "Subtract sum of squares from square of sums -> difference of squares 5" | ||
|
||
[b1bf19de-9a16-41c0-a62b-1f02ecc0b036] | ||
description = "Subtract sum of squares from square of sums -> difference of squares 100" |
3 changes: 3 additions & 0 deletions
3
exercises/practice/difference-of-squares/difference-of-squares.ipkg
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,3 @@ | ||
package difference-of-squares | ||
modules = DifferenceOfSquares | ||
sourcedir = "src" |
15 changes: 15 additions & 0 deletions
15
exercises/practice/difference-of-squares/example/DifferenceOfSquares.idr
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,15 @@ | ||
module DifferenceOfSquares | ||
|
||
export | ||
squareOfSum : Integer -> Integer | ||
squareOfSum number = square sum | ||
where sum = div (number * (number + 1)) 2 | ||
square n = n * n | ||
|
||
export | ||
sumOfSquares : Integer -> Integer | ||
sumOfSquares number = div (number * (number + 1) * (number + number + 1)) 6 | ||
|
||
export | ||
differenceOfSquares : Integer -> Integer | ||
differenceOfSquares number = (squareOfSum number) - (sumOfSquares number) |
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 @@ | ||
[custom.all.difference-of-squares] | ||
type = "local" | ||
path = "." | ||
ipkg = "difference-of-squares.ipkg" | ||
test = "test/test.ipkg" | ||
|
||
[custom.all.difference-of-squares-test] | ||
type = "local" | ||
path = "test" | ||
ipkg = "test.ipkg" |
13 changes: 13 additions & 0 deletions
13
exercises/practice/difference-of-squares/src/DifferenceOfSquares.idr
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 @@ | ||
module DifferenceOfSquares | ||
|
||
export | ||
squareOfSum : Integer -> Integer | ||
squareOfSum number = ?squareOfSum_rhs | ||
|
||
export | ||
sumOfSquares : Integer -> Integer | ||
sumOfSquares number = ?sumOfSquares_rhs | ||
|
||
export | ||
differenceOfSquares : Integer -> Integer | ||
differenceOfSquares number = ?differenceOfSquares_rhs |
28 changes: 28 additions & 0 deletions
28
exercises/practice/difference-of-squares/test/src/Main.idr
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,28 @@ | ||
module Main | ||
|
||
import System | ||
import Tester | ||
import Tester.Runner | ||
|
||
import DifferenceOfSquares | ||
|
||
tests : List Test | ||
tests = | ||
[ test "square of sum 1" (assertEq (squareOfSum 1) 1) | ||
, test "square of sum 5" (assertEq (squareOfSum 5) 225) | ||
, test "square of sum 100" (assertEq (squareOfSum 100) 25502500) | ||
, test "sum of squares 1" (assertEq (sumOfSquares 1) 1) | ||
, test "sum of squares 5" (assertEq (sumOfSquares 5) 55) | ||
, test "sum of squares 100" (assertEq (sumOfSquares 100) 338350) | ||
, test "difference of squares 1" (assertEq (differenceOfSquares 1) 0) | ||
, test "difference of squares 5" (assertEq (differenceOfSquares 5) 170) | ||
, test "difference of squares 100" (assertEq (differenceOfSquares 100) 25164150) | ||
] | ||
|
||
export | ||
main : IO () | ||
main = do | ||
success <- runTests tests | ||
if success | ||
then putStrLn "All tests passed" | ||
else exitFailure |
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,6 @@ | ||
package difference-of-squares-test | ||
depends = difference-of-squares | ||
, tester | ||
main = Main | ||
executable = "difference-of-squares-test" | ||
sourcedir = "src" |
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,6 @@ | ||
|
||
def generate_test(case): | ||
property = case["property"] | ||
expected = case["expected"] | ||
number = case["input"]["number"] | ||
return f'assertEq ({property} {number}) {expected}' |