initial commit
This commit is contained in:
63
bash/difference-of-squares/README.md
Normal file
63
bash/difference-of-squares/README.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# Difference Of Squares
|
||||
|
||||
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.
|
||||
|
||||
|
||||
Run the tests with:
|
||||
|
||||
```bash
|
||||
bats difference_of_squares_test.sh
|
||||
```
|
||||
|
||||
After the first test(s) pass, continue by commenting out or removing the
|
||||
`[[ $BATS_RUN_SKIPPED == true ]] || skip`
|
||||
annotations prepending other tests.
|
||||
|
||||
To run all tests, including the ones with `skip` annotations, run:
|
||||
|
||||
```bash
|
||||
BATS_RUN_SKIPPED=true bats difference_of_squares_test.sh
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
Problem 6 at Project Euler [http://projecteuler.net/problem=6](http://projecteuler.net/problem=6)
|
||||
|
||||
|
||||
## External utilities
|
||||
`Bash` is a language to write "scripts" -- programs that can call
|
||||
external tools, such as
|
||||
[`sed`](https://www.gnu.org/software/sed/),
|
||||
[`awk`](https://www.gnu.org/software/gawk/),
|
||||
[`date`](https://www.gnu.org/software/coreutils/manual/html_node/date-invocation.html)
|
||||
and even programs written in other programming languages,
|
||||
like [`Python`](https://www.python.org/).
|
||||
This track does not restrict the usage of these utilities, and as long
|
||||
as your solution is portable between systems and does not require
|
||||
installation of third party applications, feel free to use them to solve
|
||||
the exercise.
|
||||
|
||||
For an extra challenge, if you would like to have a better understanding
|
||||
of the language, try to re-implement the solution in pure `Bash`,
|
||||
without using any external tools. Note that there are some types of
|
||||
problems that bash cannot solve, such as performing floating point
|
||||
arithmetic and manipulating dates: for those, you must call out to an
|
||||
external tool.
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others
|
||||
have completed the exercise.
|
84
bash/difference-of-squares/difference_of_squares.sh
Executable file
84
bash/difference-of-squares/difference_of_squares.sh
Executable file
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# External tools: none.
|
||||
# subshell: none.
|
||||
#
|
||||
# V1: initial version
|
||||
|
||||
shopt -s extglob
|
||||
|
||||
# set to mask to enable logs. 0: none, 255: all
|
||||
(( debug=2#00000000 ))
|
||||
#(( debug=2#00001111 ))
|
||||
# $1: log level (mask), then strings to display.
|
||||
debug () {
|
||||
(( debug & $1 )) && shift && echo Line ${BASH_LINENO[0]}: "${@}" >&2
|
||||
}
|
||||
|
||||
die () {
|
||||
echo "${@}" >&2
|
||||
exit 1
|
||||
}
|
||||
usage() {
|
||||
die "usage: difference_of_squares.sh command color"
|
||||
}
|
||||
|
||||
debug 1 "#=$#" "arg=$1"
|
||||
(($# != 2)) || [[ $1 != @(square_of_sum|sum_of_squares|difference) ]] || \
|
||||
[[ "$2" != +([[:digit:]]) ]] && usage
|
||||
|
||||
# the 2 next functions will set $1 to the calculated value for $2
|
||||
sum_of_squares () { # set $1 to sum of squares ($2)
|
||||
local -n res=$1
|
||||
local val=$2
|
||||
|
||||
# the sum of 1st n integers squares is:
|
||||
# S = 1² + 2² + 3² ..... + (n-1)² + n²
|
||||
# = [ n * (n+1) * (2n+1) ] / 6
|
||||
# demonstration on:
|
||||
# http://www.takayaiwamoto.com/Sums_and_Series/sumsqr_1.html
|
||||
(( res = val * (val+1) * (2*val + 1) / 6 ))
|
||||
debug 2 "sum_of_squares($val) = $res"
|
||||
}
|
||||
square_of_sum () { # set $1 to square of sum ($2)
|
||||
local -n res=$1
|
||||
local val=$2
|
||||
|
||||
# The sum of n 1st integers is:
|
||||
# S = 1 + 2 + 3 ... + (n-1) + n
|
||||
# = [ n * (n+1) ] / 2
|
||||
# demonstration is trivial for this one.
|
||||
(( res = (val * (val+1) / 2 ) ** 2 ))
|
||||
debug 2 "square_of_sum($val) = $res"
|
||||
}
|
||||
|
||||
action="$1"
|
||||
(( num = $2 ))
|
||||
|
||||
case "$action" in
|
||||
square_of_sum)
|
||||
square_of_sum result "$num"
|
||||
debug 3 "result after calling square_of_sum: $result"
|
||||
;;
|
||||
sum_of_squares)
|
||||
sum_of_squares result "$num"
|
||||
debug 3 "result after calling sum_of_squares: $result"
|
||||
;;
|
||||
difference)
|
||||
square_of_sum SqSum "$num"
|
||||
sum_of_squares SumSq "$num"
|
||||
((result = SqSum - SumSq))
|
||||
debug 3 "result after calling difference: $result"
|
||||
esac
|
||||
|
||||
echo $result
|
||||
exit 0
|
||||
|
||||
# emacs/vim settings.
|
||||
# Local Variables:
|
||||
# sh-basic-offset: 4
|
||||
# indent-tabs-mode: nil
|
||||
# comment-column: 40
|
||||
# fill-column: 80
|
||||
# End:
|
||||
# vim: set tabstop=4 expandtab:
|
Reference in New Issue
Block a user