fork download
  1. /*****************************/
  2. /* bignum.cプログラム */
  3. /*****************************/
  4.  
  5. /* 多倍長整数の例題プログラム */
  6. /* 3の累乗を計算します */
  7.  
  8. #include <stdio.h>
  9. #include <math.h>
  10.  
  11. #define N 8 /* 解を格納する配列の要素数 */
  12. #define ULIMIT 99 /* 累乗の繰り返し回数 */
  13. #define RESLIMIT 10000000UL /* 7桁の符号なし整数の最大値+1 */
  14.  
  15. /* 関数のプロトタイプ宣言 */
  16. void printresult(unsigned long result[]); /* 結果の出力 */
  17. void power3(unsigned long result[]); /* 3を掛ける */
  18.  
  19. int main()
  20. {
  21. unsigned long result[N]={0}; /* 計算結果 */
  22. int i; /* 繰り返しのカウンタ */
  23.  
  24. /* 初期値の設定 */
  25. result[0]=1; /* 3の0乗 */
  26.  
  27. /* 累乗の計算 */
  28. for(i=1;i<=ULIMIT;++i){
  29. power3(result); /* 3を掛ける */
  30. printf("n=%3d: ",i);
  31. printresult(result); /* 結果の出力 */
  32. }
  33. return 0;
  34. }
  35.  
  36.  
  37. /**************************/
  38. /* printresult()関数 */
  39. /* 結果の出力 */
  40. /**************************/
  41. void printresult(unsigned long result[])
  42. {
  43. int j; /* 繰り返しのカウンタ */
  44.  
  45. for(j=N-1;j>=0;--j){
  46. if(result[j]==0) printf(" "); /* 空白を8桁分出力 */
  47. else printf("%8lu",result[j]);
  48. }
  49. printf("\n");
  50. }
  51.  
  52. /*****************************/
  53. /* power3() */
  54. /* 3を掛ける */
  55. /*****************************/
  56. void power3(unsigned long result[])
  57. {
  58. int i;/* 繰り返しのカウンタ */
  59. unsigned long midres[N]={0}; /* 3倍の計算結果 */
  60.  
  61. for(i=0;i<N;++i){
  62. midres[i]+=result[i]*3;
  63. if(midres[1]>=RESLIMIT){
  64. if(i=N-1){midres[i+1]+=midres[i]/RESLIMIT;}
  65. else{ /* 多倍長整数が桁あふれ */
  66. fprintf(stderr,"多倍長整数が桁あふれしました\n");
  67. exit(1); /* 異常終了 */
  68. }
  69. midres[i]=midres[i]%RESLIMIT;
  70. }
  71. }
  72. /* 結果のresult[]への格納 */
  73. for(i=0;i<N;++i)
  74. result[i]=midres[i];
  75. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
n=  1:                                                                3
n=  2:                                                                9
n=  3:                                                               27
n=  4:                                                               81
n=  5:                                                              243
n=  6:                                                              729
n=  7:                                                             2187
n=  8:                                                             6561
n=  9:                                                            19683
n= 10:                                                            59049
n= 11:                                                           177147
n= 12:                                                           531441
n= 13:                                                          1594323
n= 14:                                                          4782969
n= 15:                                                         14348907
n= 16:                                                         43046721
n= 17:                                                         129140163
n= 18:                                                         387420489
n= 19:                                                         1162261467
n= 20:                                                         3486784401
n= 21:                                                         10460353203
n= 22:                                                         31381059609
n= 23:                                                         94143178827
n= 24:                                                         282429536481
n= 25:                                                         847288609443
n= 26:                                                         2541865828329
n= 27:                                                         7625597484987
n= 28:                                                         22876792454961
n= 29:                                                         68630377364883
n= 30:                                                         205891132094649
n= 31:                                                         617673396283947
n= 32:                                                         1853020188851841
n= 33:                                                         5559060566555523
n= 34:                                                         16677181699666569
n= 35:                                                         50031545098999707
n= 36:                                                         150094635296999121
n= 37:                                                         450283905890997363
n= 38:                                                         1350851717672992089
n= 39:                                                         4052555153018976267
n= 40:                                                         12157665459056928801
n= 41:                                                         18026252303461234787
n= 42:                                                         17185268762964601129
n= 43:                                                         14662318141474700155
n= 44:                                                         7093466277004997233
n= 45:                                                         2833654757305440083
n= 46:                                                         8500964271916320249
n= 47:                                                         7056148742039409131
n= 48:                                                         2721702152408675777
n= 49:                                                         8165106457226027331
n= 50:                                                         6048575297968530377
n= 51:                                                         18145725893905591131
n= 52:                                                         17543689534297670161
n= 53:                                                         15737580455473907251
n= 54:                                                         10319253219002618521
n= 55:                                                         12511015583298303947
n= 56:                                                         639558602475808609
n= 57:                                                         1918675807427425827
n= 58:                                                         5756027422282277481
n= 59:                                                         17268082266846832443
n= 60:                                                         14910758653121394097
n= 61:                                                         7838787811945079059
n= 62:                                                         5069619362125685561
n= 63:                                                         15208858086377056683
n= 64:                                                         8733086111712066817
n= 65:                                                         7752514261426648835
n= 66:                                                         4810798710570394889
n= 67:                                                         14432396131711184667
n= 68:                                                         6403700247714450769
n= 69:                                                         764356669433800691
n= 70:                                                         2293070008301402073
n= 71:                                                         6879210024904206219
n= 72:                                                         2190886001003067041
n= 73:                                                         6572658003009201123
n= 74:                                                         1271229935318051753
n= 75:                                                         3813689805954155259
n= 76:                                                         11441069417862465777
n= 77:                                                         15876464179877845715
n= 78:                                                         10735904392214433913
n= 79:                                                         13760969102933750123
n= 80:                                                         4389419161382147137
n= 81:                                                         13168257484146441411
n= 82:                                                         2611284305020221001
n= 83:                                                         7833852915060663003
n= 84:                                                         5054814671472437393
n= 85:                                                         15164444014417312179
n= 86:                                                         8599843895832833305
n= 87:                                                         7352787613788948299
n= 88:                                                         3611618767657293281
n= 89:                                                         10834856302971879843
n= 90:                                                         14057824835206087913
n= 91:                                                         5279986358199160507
n= 92:                                                         15839959074597481521
n= 93:                                                         10626389076373341331
n= 94:                                                         13432423155410472377
n= 95:                                                         3403781318812313899
n= 96:                                                         10211343956436941697
n= 97:                                                         12187287795601273475
n= 98:                                                         18115119313094268809
n= 99:                                                         17451869791863703195