Pro81:Palindromic Square

Palindromic Squares</font></b>
Rob Kolstad </center>

Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome. </p>

Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that the square of N is palindromic when expressed in base B; also print the value of that palindromic square. Use the letters 'A', 'B', and so on to represent the digits 10, 11, and so on. </p>

Print both the number and its square in base B. </p>

PROGRAM NAME: palsquare</h3>

INPUT FORMAT</h3> A single line with B, the base (specified in base 10).

SAMPLE INPUT (file palsquare.in) </h3>
10 </pre>

OUTPUT FORMAT</h3> Lines with two integers represented in base B. The first integer is the number whose square is palindromic; the second integer is the square itself.

SAMPLE OUTPUT (file palsquare.out)</h3>
1 1 2 4 3 9 11 121 22 484 26 676 101 10201 111 12321 121 14641 202 40804 212 44944 264 69696 

/*
ID: hippo he
LANG: C
TASK: palsquare
*/
#include
#include
char pals[50][20];
const char scale[]="0123456789ABCDEFGHIJK";
char nin[10];
void input(int *base)
{
FILE *fp=fopen("palsquare.in","r");
fscanf(fp,"%d",base);
}
void transform(int n,int R,char **end)
{
memset(nin,'0',sizeof(nin));
int i=0;
while(n)
{
nin[i]=scale[n%R];
n/=R;
i++;
}
nin[i]=0;
*end=&nin[i-1];
}
int ispal(char* nin,char *end)
{
char* p=nin,*q=end;
while(p {
if(*q!=*p)return 0;
q--;
p++;
}
return 1;
}
void output(int n)
{
int i;
char *p,*q,tmp;
FILE *fp=fopen("palsquare.out","w");
for(i=0;i {
p=pals[i];
for(q=p;*q;q++);q--;
while(q>p)
{
tmp=*p;
*p=*q;
*q=tmp;
p++;q--;
}
fprintf(fp,"%s %sn",pals[i],pals[i+1]);
}
}
int main (int argc, char const* argv[]) {
int n=0,base,i,R;
char *end1=NULL;
char *end2=NULL;
input(&base);
for(i=1;i<=300;i++)
{
transform(i*i,base,&end2);
if(ispal(nin,end2))
{
transform(i,base,&end1);
strcpy(pals[n++],nin);
transform(i*i,base,&end2);
strcpy(pals[n++],nin);
}
}
output(n-1);
return 0;
}
</pre></div>