number chain

  1. A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before.
    For example,
    44 32 13 10 1 1
    85 89 145 42 20 4 16 37 58 89
    Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.
    How many starting numbers below 100 will arrive at 89?

// CodeChef.cpp : Defines the entry point for the console application.
//

#include “stdafx.h”

bool check(int n)
{
int rem=0,dup,iternum=n;
while(1)
{
dup=iternum;
iternum=0;
while(dup)
{
rem=dup%10;
iternum+=(rem*rem);
dup=dup/10;
}
if(iternum==89)
return true;
if(iternum==1)
return false;
}
}

int _tmain(int argc, _TCHAR* argv[])
{
int num=2,count=0;
for(num=2;num<100;num++)
{
if(check(num)==true)
count++;
}
printf(" count = %d",count);
return 0;
}

#include <stdio.h>
void main()
{
int a,b,c,s,t,i,z;
int n=64,count=0;
for(i=1;i<=100;i++){
n=i;
while(n!=1 || n!=89){
//z=i;
a=n%10;
s=n/10;
b=s%10;
c=n/100;
n=aa+bb+c*c;
if(n==89){count++;
//printf("%d “,z);}
if(n==89 || n== 1)
break;}}
/printf("%d “,a);
printf(”%d “,b);
printf(”%d ",c);
/
printf(”%d ",count);}