difference_of_squares: iteration 1
This commit is contained in:
parent
14cff1b798
commit
2cfe146e9d
1 changed files with 34 additions and 0 deletions
34
cpp/difference-of-squares/difference_of_squares.h
Normal file
34
cpp/difference-of-squares/difference_of_squares.h
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define EXERCISM_RUN_ALL_TESTS
|
||||||
|
|
||||||
|
|
||||||
|
namespace squares {
|
||||||
|
|
||||||
|
namespace detail {
|
||||||
|
|
||||||
|
constexpr unsigned sum(unsigned n)
|
||||||
|
{
|
||||||
|
return (n*(n+1))/2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
constexpr unsigned square_of_sums(unsigned n)
|
||||||
|
{
|
||||||
|
using detail::sum;
|
||||||
|
return sum(n)*sum(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr unsigned sum_of_squares(unsigned n)
|
||||||
|
{
|
||||||
|
return (n*(n+1)*(2*n+1))/6;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr unsigned difference(unsigned n)
|
||||||
|
{
|
||||||
|
return square_of_sums(n) - sum_of_squares(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue