🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of krunch3r76

krunch3r76's solution

to Difference Of Squares in the Vim script Track

Published at Mar 18 2020 · 0 comments
Instructions
Test suite
Solution

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.

How to run the tests

If you don't know how to run Vader tests, see: Tests.

For general information about the Vim script track, see: Help.

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

"
" Version: 1.2.0
"

Execute (square of sum 1):
  let number = 1
  let expected = 1
  AssertEqual expected, SquareOfSum(number)

Execute (square of sum 5):
  let number = 5
  let expected = 225
  AssertEqual expected, SquareOfSum(number)

Execute (square of sum 100):
  let number = 100
  let expected = 25502500
  AssertEqual expected, SquareOfSum(number)
Execute (sum of squares 1):
  let number = 1
  let expected = 1
  AssertEqual expected, SumOfSquares(number)

Execute (sum of squares 5):
  let number = 5
  let expected = 55
  AssertEqual expected, SumOfSquares(number)

Execute (sum of squares 100):
  let number = 100
  let expected = 338350
  AssertEqual expected, SumOfSquares(number)
Execute (difference of squares 1):
  let number = 1
  let expected = 0
  AssertEqual expected, DifferenceOfSquares(number)

Execute (difference of squares 5):
  let number = 5
  let expected = 170
  AssertEqual expected, DifferenceOfSquares(number)

Execute (difference of squares 100):
  let number = 100
  let expected = 25164150
  AssertEqual expected, DifferenceOfSquares(number)
"
" Find the difference between the square of the sum and the sum of the squares
" of the first N natural numbers.
"
" Examples:
"
"   :echo SquareOfSum(3)
"   36
"   :echo SumOfSquares(3)
"   14
"   :echo DifferenceOfSquares(3)
"   22
"
function! DifferenceOfSquares(number) abort
  " your implemention goes here
  return SquareOfSum(a:number) - SumOfSquares(a:number)
endfunction

function! SquareOfSum(number) abort
  " your implemention goes here
  let l:sum = 0
  for l:nCurrent in range(a:number, 1, -1)
	let l:sum += l:nCurrent
  endfor
  return l:sum * l:sum
endfunction

function! SumOfSquares(number) abort
  " your implemention goes here
  let l:sum = 0
  for l:nCurrent in range(a:number, 1, -1)
	let l:square = l:nCurrent * l:nCurrent
	let l:sum += l:square
  endfor
  return l:sum
endfunction

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?