Challenge
Using the C++ language, have the function
PentagonalNumber(num)
readnum
which will be a positive integer and determine how many dots exist in a pentagonal shape around a center dot on the Nth iteration. For example, in the image below you can see that on the first iteration there is only a single dot, on the second iteration there are 6 dots, on the third there are 16 dots, and on the fourth there are 31 dots.
Your program should return the number of dots that exist in the whole pentagon on the Nth iteration.
Sample Test Cases
Input:2 Output:6 Input:5 Output:51
I found it weird that Coderbyte called this a hard challenge. It works, but is this the right approach?
Header File
#ifndef PentagonalNum_hpp
#define PentagonalNum_hpp
#include <stdio.h>
class PentagonalNum
{
public:
PentagonalNum(int N);
unsigned long long int num_Of_Dots();
private:
int iteration;
};
#endif /* PentagonalNum_hpp */
CPP File
#include "PentagonalNum.hpp"
PentagonalNum::PentagonalNum(int N)
: iteration(N)
{
}
unsigned long long int PentagonalNum::num_Of_Dots()
{
int result = 1;
int num = 5;
for (int i = 1; i < iteration; ++i)
{
result += num;
num += 5;
}
return result;
}