Thank you leppyr64 for trying to help me. Below is the code I have currently.
With Best Regards,
SRK
**************** Code below**************
include
include
include
define MAX_VAL 19500000
/*unsigned int readint()
{
int cc = getc(stdin);
for (;cc < '0' || cc > '9'
cc = getc(stdin);
int ret = 0;
for (;cc >= '0' && cc <= '9'
{
ret = ret * 10 + cc - '0';
cc = getc(stdin);
}
return ret;
}
*/
unsigned int readint()
{
unsigned int x;
char c;
for (x = 0; (c = getchar_unlocked()) >= '0'; x = x*10+c-'0'); // reads int to x
return x;
}
int count =0;
int calval[MAX_VAL][2];
unsigned int sum_digits_squared(unsigned int n)
{
unsigned int sum = 0;
unsigned int tmp = 0;
while ( n > 0)
{
tmp=n%10;
n/=10;
//sum+=(int)pow(tmp,2);
sum+=(tmp*tmp);
}
return sum;
}
unsigned int is_happy(unsigned int n)
{
unsigned int n0=n;
unsigned int n1=n;
while (n > 999)
{
n = sum_digits_squared(n);
count++;
}
n0=n;
n1=n;
do {
n0 = sum_digits_squared(n0);
n1 = sum_digits_squared(sum_digits_squared(n1));
//n1 = sum_digits_squared(n1);
//n1 = sum_digits_squared(n1);
count++;
if (n1 == 1) return 1;
} while (n0 != n1);
//if (n0 == 1)
//{
//printf("4 n0 = %d, count = %d\n",n1,count);
//return 1;
//}
//else
//{
//printf("5 n0 = %d, count = %d\n",n1,count);
// return 0;
// }
return 0;
}
int main()
{
unsigned int T,K,tccount,num;
//scanf("%d",&K);
//initialzize the array to zero
for (tccount = 0; tccount < MAX_VAL;tccount++)
{
calval[tccount][0] = 0;
calval[tccount][1] = 0;
}
K = readint();
for (tccount=0; tccount<K; tccount++)
{
//scanf("%d",&T);
//T = fastI();
T = readint();
num = T;
count = 0;
if ( (num < MAX_VAL) && ((calval[num][0] == 1) || (calval[num][0] == 2)) )
{
if ( calval[num][0] == 1 )
{
printf("%d\n",calval[num][1]);
continue;
}
else
{
printf("%d\n",-1);
continue;
}
}
if (is_happy(num) == 1 )
{
if (num < MAX_VAL )
{
calval[num][0] = 1;
calval[num][1] = count;
}
printf("%d\n",count);
}
else
{
if (num < MAX_VAL )
calval[num][0] = 2;
printf("%d\n",-1);
}
}
return 0;
}