Initial Commit
This commit is contained in:
30
go/difference-of-squares/README.md
Normal file
30
go/difference-of-squares/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Difference Of Squares
|
||||
|
||||
Find the difference between the sum of the squares and the square of the sums of the first N natural numbers.
|
||||
|
||||
The square of the sum of the first ten natural numbers is,
|
||||
|
||||
(1 + 2 + ... + 10)**2 = 55**2 = 3025
|
||||
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
|
||||
1**2 + 2**2 + ... + 10**2 = 385
|
||||
|
||||
Hence the difference between the square of the sum of the first
|
||||
ten natural numbers and the sum of the squares is 2640:
|
||||
|
||||
3025 - 385 = 2640
|
||||
|
||||
To run the tests simply run the command `go test` in the exercise directory.
|
||||
|
||||
If the test suite contains benchmarks, you can run these with the `-bench`
|
||||
flag:
|
||||
|
||||
go test -bench .
|
||||
|
||||
For more detailed info about the Go track see the [help
|
||||
page](http://help.exercism.io/getting-started-with-go.html).
|
||||
|
||||
## Source
|
||||
|
||||
Problem 6 at Project Euler [view source](http://projecteuler.net/problem=6)
|
32
go/difference-of-squares/difference_of_squares.go
Normal file
32
go/difference-of-squares/difference_of_squares.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package diffsquares
|
||||
|
||||
import "math"
|
||||
|
||||
// SquareOfSums calculates the Square of the sums
|
||||
// of the first 'num' natural numbers
|
||||
func SquareOfSums(num int) int {
|
||||
ret := 0
|
||||
for num > 0 {
|
||||
ret += num
|
||||
num--
|
||||
}
|
||||
return int(math.Pow(float64(ret), 2))
|
||||
}
|
||||
|
||||
// SumOfSquares calculates the Sum of the Squares
|
||||
// of the first 'num' natural numbers
|
||||
func SumOfSquares(num int) int {
|
||||
ret := 0
|
||||
for num > 0 {
|
||||
ret += int(math.Pow(float64(num), 2))
|
||||
num--
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Difference calculates the difference between
|
||||
// The SquareOfSums and the SumOfSquares for the
|
||||
// first 'num' natural numbers
|
||||
func Difference(num int) int {
|
||||
return SquareOfSums(num) - SumOfSquares(num)
|
||||
}
|
54
go/difference-of-squares/difference_of_squares_test.go
Normal file
54
go/difference-of-squares/difference_of_squares_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package diffsquares
|
||||
|
||||
import "testing"
|
||||
|
||||
var tests = []struct{ n, sqOfSums, sumOfSq int }{
|
||||
{5, 225, 55},
|
||||
{10, 3025, 385},
|
||||
{100, 25502500, 338350},
|
||||
}
|
||||
|
||||
func TestSquareOfSums(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
if s := SquareOfSums(test.n); s != test.sqOfSums {
|
||||
t.Fatalf("SquareOfSums(%d) = %d, want %d", test.n, s, test.sqOfSums)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSumOfSquares(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
if s := SumOfSquares(test.n); s != test.sumOfSq {
|
||||
t.Fatalf("SumOfSquares(%d) = %d, want %d", test.n, s, test.sumOfSq)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDifference(t *testing.T) {
|
||||
for _, test := range tests {
|
||||
want := test.sqOfSums - test.sumOfSq
|
||||
if s := Difference(test.n); s != want {
|
||||
t.Fatalf("Difference(%d) = %d, want %d", test.n, s, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark functions on just a single number (100, from the original PE problem)
|
||||
// to avoid overhead of iterating over tests.
|
||||
func BenchmarkSquareOfSums(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SquareOfSums(100)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkSumOfSquares(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
SumOfSquares(100)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDifference(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Difference(100)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user