1. Erste Version: C, Evgeni Pavlidis


/**
* ACM programming Contest WS 08/09
* UVa Status: accepted
* Run Time: 0.100
* @author Evgeni Pavlidis evgenipavlidis@yahoo.de
*/
#include <stdio.h>

int memory[40000];

int main()
{
int n,i,j,c=1,found;
int start, end;
scanf("%d\n", &n);

for(i =0; i < n; i++)
{
if(i != 0)
printf("\n");

found = 0;
scanf("%d %d\n", &start, &end);

if(start > end)
{
int tmp = start;
start = end;
end = tmp;
}

printf("case #%d\n",c++);
for(j=start; j <= end; j++)
if(isKaprekar(j))
{
printf("%d\n", j);
found = 1;
}

if(!found)
printf("no kaprekar numbers\n");

}

return 0;

}

int isKaprekar(int n)
{
if(memory[n] == 1)
return 1;

if(memory[n] == -1)
return 0;

int square = n*n;
int divisor = 10;
int part1, part2;

while( (part1 = square % divisor) != square)
{
part2 = square / divisor;
if(n == part1 + part2 && part1 > 0 && part2 > 0)
{
memory[n] = 1;
return 1;
}
divisor *= 10;
}

memory[n] = -1;
return 0;
}