구하려는 꼭지점 수가 일정 간격으로 뛰어 나오고, 뜀 간격은 4번의 뜀마다 늘어나고 있음을 알면 쉽게 풀린다.

/*
   Problem 28 : Number spiral diagonals 
*/

#include <iostream>
#include <ctime>

#define MAX 1001 
//#define MAX 5 

using namespace std;
int main(int argc, char** argv)
{
	clock_t begin = clock();

	/* code */
	int sum = 0;
	
	int cycle = 0;
	int skip = 0;

	int j=0;
	for(int i=2;;i++)
	{
		if (skip >= MAX-2) { cout << "skip=" << skip << endl; break; }

		if (j<=skip) { j++; continue;}
		else {
			j=0;
			sum += i;
			cout << "i=" << i << ", sum=" << sum << endl;
			cycle++;

			if( cycle%4 == 0) {
				skip = skip+2;
				cycle = 0;
			}
		}
	}

	sum = sum+1; // first diagonal number
	std:: cout << "sum=" << sum << endl;
	/* end of code */
	clock_t end = clock();
	std::cout << "elapsed time=" << double(end - begin) / CLOCKS_PER_SEC << std::endl;
	return 0;
}