Avatar of exklamationmark

exklamationmark's solution

to Difference Of Squares in the Go Track

Published at Jul 28 2018 · 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.

Running the tests

To run the tests run the command go test from within the exercise directory.

If the test suite contains benchmarks, you can run these with the --bench and --benchmem flags:

go test -v --bench . --benchmem

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.

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.go

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)
	}
}
package diffsquares

func SquareOfSums(n int) int {
	sum := n * (n + 1) / 2 // overflow?
	return sum * sum       // overflow?
}

func SumOfSquares(n int) int {
	return n * (n + 1) * (2*n + 1) / 6 // overflow?
}

func Difference(n int) int {
	return SquareOfSums(n) - SumOfSquares(n) // overflow?
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?