同構數

同構數

正整數n若是它平方數的尾部,則稱n為同構數。 例如:5的平方數是25,且5出現在25的右側,那么5就是一個同構數。

基本概念

同構數是會出現在它的平方的右邊的數

如5×5=25,6×6=36。

十進制同構數

以6開頭的同構數有:

6^2=36

76^2=5776

376^2=141376

9376^2=87909376

109376^2=11963109376

7109376^2=50543227109376

87109376^2=7588043387109376

787109376^2=619541169787109376

1787109376^2=3193759921787109376

81787109376^2=6689131260081787109376

40081787109376^2=1606549657881340081787109376

算法程式

找出1至10000之間的全部同構數的C語言程式

例如:5是25右邊的數,25是625右邊的數,5和25都是同構數數*/

方法一:

#include<stdio.h>

main()

{ long i,j,k;

k=10;

for (i=1;i<=10000;i++){

if (i==k) k*=10;

j=i*i;

if(j%k==i) printf("%ld\t%ld\n",i,j);

}

}

方法二:

#include "stdio.h"

void main( )

{ long int m,m1,n,a,b;

scanf("%ld",&m);

m1=m;

n=m*m1;

while(m1)

{ a=m1%10; b=n%10;

if(a!=b) break;

m1=m1/10; n=n/10;

}

if(m1!=0) printf("%d不是一個同構數");

else printf("%d是一個同構數");

}

方法三:

#include "stdio.h"

int main()

{

long a,b;

for(a=1,b=0;a<10000;a++)

{

b=a*a;

if(b%10==a||b%100==a||b%1000==a||b%10000==a)

printf("%d ",a);

}

}

{找出1~10000之間同構數的PASCAL程式}

program exp;

var i,j,k:longint;

begin

k:=10;

for i:=1 to 10000 do

begin

if i=k then k:=k*10;

j:=i*i;

if j mod k=i then writeln(i:8,j:8);

end;

end.

1~1000之間的同構數有下面這七個(第二列是它的平方):

1 1

5 25

6 36

25 625

76 5776

376 141376

625 390625

快速算法

//由CMInverse提供

#include "stdafx.h"

#include <iostream>

#include <conio.h>

#define BASE 10 //decimal

int _tmain(int argc, _TCHAR* argv[])

{

char homepage();

int select=0,max=1000;

long i,j,now=0;

long temp=0;

//------------------------------------------------------------------------------開始界面

//homepage();

system("CLS");

system("color 4F");

//------------------------------------------------------------------------------模式選擇

while(select-53 && select-54)

{

printf("5 or 6 ?\n");

fflush(stdin);

select=getch();

}

do

{

printf("how many numbers ?\n");

fflush(stdin);

scanf("%d",&max);

}while(max>=65536 || max<0);

if(max)

{

char *outcome=new char [max];

outcome[now]=select-48;

//------------------------------------------------------------------------------核心代碼

//------------------------------------------------------------------------------限長度輸出

while(now<max)

{

temp+=outcome[0]*outcome[now]<<1;

if(!now)

temp>>=1;

now++;

temp/=BASE;

for(i=1,j=now-1;i<j;i++,j--)

{

temp+=outcome[i]*outcome[j]<<1;

}

if(i==j)

temp+=outcome[i]*outcome[j];

outcome[now]=temp%BASE;

if(outcome[now] && select-53)

outcome[now]=BASE-outcome[now];

//printf("%d\t",temp);

}

//------------------------------------------------------------------------------輸出

if(max)

for(now=max;now;now--)

printf("%u",outcome[now-1]);}

//------------------------------------------------------------------------------不限長度輸出

else

{

char *outcome=new char [65536];

outcome[now]=select-48;

while(1)

{

temp+=outcome[0]*outcome[now]<<1;

if(!now)

temp>>=1;

now++;

temp/=BASE;

for(i=1,j=now-1;i<j;i++,j--)

{

temp+=outcome[i]*outcome[j]<<1;

}

if(i==j)

temp+=outcome[i]*outcome[j];

outcome[now]=temp%BASE;

if(outcome[now] && select-53)

outcome[now]=BASE-outcome[now];

//------------------------------------------------------------------------------輸出

printf("%u",outcome[now-1]);

}

}

//------------------------------------------------------------------------------暫停觀察結果

printf("\n");

system("pause");

return 0;

}

相關詞條

熱門詞條

聯絡我們