Created
March 7, 2016 16:05
-
-
Save motoso/e30775978d2551d0cff4 to your computer and use it in GitHub Desktop.
ハーディ・ラマヌジャン数を求めるプログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
/*ハーディ・ラマヌジャン数を求めるプログラム | |
以下の条件を満たす4桁の自然数を求める. | |
1.2つの数の立方の和として表す表し方が2通りある数. | |
2.その数の中で最も小さいもの*/ | |
int max_ripp(void); | |
int main(void){ | |
int max,i,j,k,m,flag; | |
int a[4][3]; | |
max = max_ripp(); //立方が1000を超えない最大の自然数 | |
printf("立方が9999を超えない最大の自然数は%d\n",max); | |
printf("%dの立方は%d\n",max,max*max*max); | |
/*1からmax(22)までの範囲で,立方の和としての表し方が2通り有る数を調べる*/ | |
for(m=1000; m < 10000; m++){ | |
flag = 0; | |
for(i=1; i<max; i++){ | |
for(j=1; j<max; j++){ | |
if((m-(i*i*i+j*j*j))==0){//2つの数の立方和ならば | |
flag+=1 ;//そのときの(m,i,j)を配列に数を格納し,flagを1足す. | |
a[k][0]=m; | |
a[k][1]=i; | |
a[k][2]=j; | |
k++; //次のkを準備 | |
if(flag == 4){ | |
//もし4つの組み合わせがあるのなら,それらを出力するが, | |
//[(a^3,b^3),(b^3,a^3),(c^3,d^3),(d^3,c^3)の組み合わせ] | |
//上記のうち後2つの組み合わせは,前2つの組み合わせと等しいので,はじめの2つのみ出力. | |
printf("ハーディー・ラマヌジャン数は:%d=(%d)^3+(%d)^3=(%d)^3+(%d)^3\n",a[0][0],a[0][1],a[0][2],a[1][1],a[1][2]); | |
return(0); | |
} | |
} | |
} | |
} | |
k=0; //自然数mが変わったのなら,配列の格納番号をもとに戻す. | |
} | |
printf("それはお兄ちゃんの妄想……だったんじゃないの?\n"); | |
return(-1); //BAD END | |
} | |
/*立方が1000を超えない最大の自然数を求めるプログラム*/ | |
int max_ripp(void){ | |
int n=0,max=0; | |
while(max < 10000){ | |
++n; | |
max = n; | |
max = max*max*max; | |
} | |
return(n-1); //max>10000となる一つ前のnが必要 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment