Saturday 5 October 2013

PROJECT EULER SOLUTION # 28



Solution to problem number 28 of Project Euler.
Question # 28
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
21 22 23 24 25
20  7  8  9 10
19  6  1  2 11
18  5  4  3 12
17 16 15 14 13
It can be verified that the sum of the numbers on the diagonals is 101.
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?

Solution # 28
/***************************************************************************************/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>

long fun(int);

int main()
{
       int i;
       long sum=1;
       for(i=1;i<=500;i++)
              sum+=fun(i);
      
       printf("Answer = %ld\n",sum);
 
       printf("\nEXECUTION TIME = %f",clock()/(float)CLK_TCK);
       system("pause");
}

long fun(int n)
{
       long sum1=0,sum2=0,sum3=0,sum4=0;
       int temp;
       temp=2*n+1;
       sum1=pow((double)temp,2);
       sum2=pow((double)temp-2,2)+temp-1;
       sum3=sum2+2*n;
       sum4=sum3+2*n;
       return (sum1+sum2+sum3+sum4);
}

/***************************************************************************************/

No comments:

Post a Comment