Sum of odd is square
Art of Code 01
Today, i write a simple program that prints squares in numbers, to prove that the sum of odd numbers are square.
If we have one item and add a odd number of items to it, the items forms a square.
- 2 Square = 4 = 1 + 3
- 4 Square = 16 = 1+3+5+7
- n Square = 1 + odd numbers
The Output of aoc_01-sumofofodd.cpp
1
4
9
16
25
36
49
64
81
A geometrical Prove of the sum of Odd is square:
appendix std::vector, auto and const
std::vector
- Vectors
vector<int> v = {1, 4, 2};
auto
, explicit declared is so nineties ;-)
In “modern” c++, the compiler can decide which type applies.
auto v = {1, 4, 2};
for (auto i: v)
cout << i << "\n";
We may also explicit tell the compiler we read only with const
:
auto v = {1, 4, 2};
for (const int &i: v)
cout << i << "\n";
aoc_01-sumofofodd.cpp
To compile and play with the sum of odds in C++:
g++ aoc_01-sumofofodd.cpp && ./a.out
or
clang++ -std=c++11 -stdlib=libc++ -g aoc_01-sumofofodd.cpp && ./a.out
Written on August 8, 2020