Given a pattern, observe it and find the sum of first N terms of the series.
You were given a pattern, which goes like this: 1, 6, 12, 18, 24 …
This is moreover an AP when it starts from the second element.
You just need to find out the sum of AP.
We can write the series as : 1 + 6*(1 + 2 + 3 + .... + Z) … (Equation 1)
Z will be (total number of concentric hexagons - 1) which is N/6.
Sum of the first Z elements can be written as : (Z * (Z + 1) )/2 … (Equation 2)
Now, just replace the value from Equation 2 in Equation 1,
1 + 6*( ( Z * ( Z + 1 ) ) / 2)
where Z = N/6
using namespace std;
long long n;
long long z = n/6, ans=1+ 6*((z*(z+1))/2);