aboutsummaryrefslogtreecommitdiff
path: root/difference-of-squares/src
diff options
context:
space:
mode:
Diffstat (limited to 'difference-of-squares/src')
-rw-r--r--difference-of-squares/src/lib.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/difference-of-squares/src/lib.rs b/difference-of-squares/src/lib.rs
new file mode 100644
index 0000000..4599733
--- /dev/null
+++ b/difference-of-squares/src/lib.rs
@@ -0,0 +1,14 @@
1/// Implements square of Gauss' formula
2pub fn square_of_sum(n: u32) -> u32 {
3 (n * (n + 1) / 2).pow(2)
4}
5
6/// Implements the formula reported in Wolfram|Alpha at this
7/// [link](https://www.wolframalpha.com/input/?i=sum+of+squares&assumption=%7B%22F%22%2C+%22SumOfSquaresOfIntegers%22%2C+%22sumlowerlimit%22%7D+-%3E%221%22&assumption=%7B%22F%22%2C+%22SumOfSquaresOfIntegers%22%2C+%22sumupperlimit2%22%7D+-%3E%22n%22)
8pub fn sum_of_squares(n: u32) -> u32 {
9 n * (1 + n) * (1 + 2 * n) / 6
10}
11
12pub fn difference(n: u32) -> u32 {
13 square_of_sum(n) - sum_of_squares(n)
14}