fork download
  1. #include <stdio.h>
  2.  
  3. void power3(unsigned long result[], int exponent);
  4. void print_result(unsigned long result[]);
  5.  
  6. int main(void) {
  7. unsigned long result[8]; // 結果の初期化
  8.  
  9. for (int i = 1; i <= 99; i++) {
  10. power3(result, i); // 3のi乗を計算する
  11. printf("3^%d = ", i);
  12. print_result(result);
  13. }
  14.  
  15. return 0;
  16. }
  17.  
  18. void power3(unsigned long result[], int exponent) {
  19. // 結果をゼロにリセット
  20. for (int j = 0; j < 8; j++) {
  21. result[j] = 0;
  22. }
  23. result[0] = 1; // 3の0乗は1
  24.  
  25. for (int times = 1; times <= exponent; times++) {
  26. unsigned long carry = 0;
  27.  
  28. for (int j = 0; j < 8; j++) {
  29. unsigned long tmp = result[j] * 3 + carry;
  30. result[j] = tmp % 10000000; // 基数を直接使用
  31. carry = tmp / 10000000; // 基数を直接使用
  32. }
  33.  
  34. // 残った繰り上がりを処理
  35. if (carry > 0) {
  36. for (int j = 7; j > 0; j--) {
  37. result[j] = result[j - 1];
  38. }
  39. result[0] = carry;
  40. }
  41. }
  42. }
  43.  
  44. void print_result(unsigned long result[]) {
  45. int started = 0; // 先頭のゼロをスキップするためのフラグ
  46. for (int i = 7; i >= 0; i--) {
  47. if (result[i] != 0) {
  48. started = 1; // 最初の非ゼロ桁を見つけた
  49. }
  50. if (started) {
  51. if (i < 7) { // 先頭以外の桁には0を追加
  52. printf("%07lu", result[i]);
  53. } else {
  54. printf("%lu", result[i]); // 先頭の桁はそのまま表示
  55. }
  56. }
  57. }
  58. if (!started) { // すべての桁がゼロの場合
  59. printf("0");
  60. }
  61. printf("\n"); // 改行
  62. }
  63.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
3^1 = 0000003
3^2 = 0000009
3^3 = 0000027
3^4 = 0000081
3^5 = 0000243
3^6 = 0000729
3^7 = 0002187
3^8 = 0006561
3^9 = 0019683
3^10 = 0059049
3^11 = 0177147
3^12 = 0531441
3^13 = 1594323
3^14 = 4782969
3^15 = 00000014348907
3^16 = 00000043046721
3^17 = 00000129140163
3^18 = 00000387420489
3^19 = 00001162261467
3^20 = 00003486784401
3^21 = 00010460353203
3^22 = 00031381059609
3^23 = 00094143178827
3^24 = 00282429536481
3^25 = 00847288609443
3^26 = 02541865828329
3^27 = 07625597484987
3^28 = 22876792454961
3^29 = 68630377364883
3^30 = 000000205891132094649
3^31 = 000000617673396283947
3^32 = 000001853020188851841
3^33 = 000005559060566555523
3^34 = 000016677181699666569
3^35 = 000050031545098999707
3^36 = 000150094635296999121
3^37 = 000450283905890997363
3^38 = 001350851717672992089
3^39 = 004052555153018976267
3^40 = 012157665459056928801
3^41 = 036472996377170786403
3^42 = 109418989131512359209
3^43 = 328256967394537077627
3^44 = 984770902183611232881
3^45 = 0000002954312706550833698643
3^46 = 0000008862938119652501095929
3^47 = 0000026588814358957503287787
3^48 = 0000079766443076872509863361
3^49 = 0000239299329230617529590083
3^50 = 0000717897987691852588770249
3^51 = 0002153693963075557766310747
3^52 = 0006461081889226673298932241
3^53 = 0019383245667680019896796723
3^54 = 0058149737003040059690390169
3^55 = 0174449211009120179071170507
3^56 = 0523347633027360537213511521
3^57 = 1570042899082081611640534563
3^58 = 4710128697246244834921603689
3^59 = 00000014130386091738734504764811067
3^60 = 00000042391158275216203514294433201
3^61 = 00000127173474825648610542883299603
3^62 = 00000381520424476945831628649898809
3^63 = 00001144561273430837494885949696427
3^64 = 00003433683820292512484657849089281
3^65 = 00010301051460877537453973547267843
3^66 = 00030903154382632612361920641803529
3^67 = 00092709463147897837085761925410587
3^68 = 00278128389443693511257285776231761
3^69 = 00834385168331080533771857328695283
3^70 = 02503155504993241601315571986085849
3^71 = 07509466514979724803946715958257547
3^72 = 22528399544939174411840147874772641
3^73 = 67585198634817523235520443624317923
3^74 = 000000202755595904452569706561330872953769
3^75 = 000000608266787713357709119683992618861307
3^76 = 000001824800363140073127359051977856583921
3^77 = 000005474401089420219382077155933569751763
3^78 = 000016423203268260658146231467800709255289
3^79 = 000049269609804781974438694403402127765867
3^80 = 000147808829414345923316083210206383297601
3^81 = 000443426488243037769948249630619149892803
3^82 = 001330279464729113309844748891857449678409
3^83 = 003990838394187339929534246675572349035227
3^84 = 011972515182562019788602740026717047105681
3^85 = 035917545547686059365808220080151141317043
3^86 = 107752636643058178097424660240453423951129
3^87 = 323257909929174534292273980721360271853387
3^88 = 969773729787523602876821942164080815560161
3^89 = 0000002909321189362570808630465826492242446680483
3^90 = 0000008727963568087712425891397479476727340041449
3^91 = 0000026183890704263137277674192438430182020124347
3^92 = 0000078551672112789411833022577315290546060373041
3^93 = 0000235655016338368235499067731945871638181119123
3^94 = 0000706965049015104706497203195837614914543357369
3^95 = 0002120895147045314119491609587512844743630072107
3^96 = 0006362685441135942358474828762538534230890216321
3^97 = 0019088056323407827075424486287615602692670648963
3^98 = 0057264168970223481226273458862846808078011946889
3^99 = 0171792506910670443678820376588540424234035840667