#include <stdio.h>
void power3(unsigned long result[]);
void print_result(unsigned long result[]);
int main(void) {
unsigned long result[8] = {0}; // 64ビットの数値を格納するための配列
power3(result);
return 0;
}
void power3(unsigned long result[]) {
for (int i = 1; i <= 99; i++) {
unsigned long long a = 1; // 3の累乗を計算するための変数
// 3のi乗を計算
for (int j = 0; j < i; j++) {
a *= 3;
}
// 結果を8桁ごとに分けて格納
for (int b = 0; b < 8; b++) {
result[b] = 0; // 初期化
}
for (int c = 0; a > 0 && c < 8; c++) {
result[c] = a % 100000000; // 下8桁を取り出す
a /= 100000000; // 次の8桁に移動
}
// 結果を表示
printf("n=%d: 3^%d = ", i
, i
); print_result(result); // 結果を表示
}
}
void print_result(unsigned long result[]) {
// 最上位のゼロを非表示にするためのフラグ
int leading_zero = 1;
// 逆順で表示(上位桁から)
for (int i = 7; i >= 0; i--) {
if (result[i] != 0) {
leading_zero = 0; // 最上位のゼロでなくなる
}
if (!leading_zero) {
printf("%08lu", result
[i
]); // 8桁のゼロ埋めで表示 }
}
// もし全てゼロなら
if (leading_zero) {
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp2b2lkIHBvd2VyMyh1bnNpZ25lZCBsb25nIHJlc3VsdFtdKTsKdm9pZCBwcmludF9yZXN1bHQodW5zaWduZWQgbG9uZyByZXN1bHRbXSk7CgppbnQgbWFpbih2b2lkKSB7CiAgICB1bnNpZ25lZCBsb25nIHJlc3VsdFs4XSA9IHswfTsgLy8gNjTjg5Pjg4Pjg4jjga7mlbDlgKTjgpLmoLzntI3jgZnjgovjgZ/jgoHjga7phY3liJcKICAgIHBvd2VyMyhyZXN1bHQpOwogICAgcmV0dXJuIDA7Cn0KCnZvaWQgcG93ZXIzKHVuc2lnbmVkIGxvbmcgcmVzdWx0W10pIHsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IDk5OyBpKyspIHsKICAgICAgICB1bnNpZ25lZCBsb25nIGxvbmcgYSA9IDE7IC8vIDPjga7ntK/kuZfjgpLoqIjnrpfjgZnjgovjgZ/jgoHjga7lpInmlbAKCiAgICAgICAgLy8gM+OBrmnkuZfjgpLoqIjnrpcKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IGk7IGorKykgewogICAgICAgICAgICBhICo9IDM7CiAgICAgICAgfQoKICAgICAgICAvLyDntZDmnpzjgpI45qGB44GU44Go44Gr5YiG44GR44Gm5qC857SNCiAgICAgICAgZm9yIChpbnQgYiA9IDA7IGIgPCA4OyBiKyspIHsKICAgICAgICAgICAgcmVzdWx0W2JdID0gMDsgLy8g5Yid5pyf5YyWCiAgICAgICAgfQoKICAgICAgICBmb3IgKGludCBjID0gMDsgYSA+IDAgJiYgYyA8IDg7IGMrKykgewogICAgICAgICAgICByZXN1bHRbY10gPSBhICUgMTAwMDAwMDAwOyAvLyDkuIs45qGB44KS5Y+W44KK5Ye644GZCiAgICAgICAgICAgIGEgLz0gMTAwMDAwMDAwOyAvLyDmrKHjga445qGB44Gr56e75YuVCiAgICAgICAgfQoKICAgICAgICAvLyDntZDmnpzjgpLooajnpLoKICAgICAgICBwcmludGYoIm49JWQ6IDNeJWQgPSAiLCBpLCBpKTsKICAgICAgICBwcmludF9yZXN1bHQocmVzdWx0KTsgLy8g57WQ5p6c44KS6KGo56S6CiAgICB9Cn0KCnZvaWQgcHJpbnRfcmVzdWx0KHVuc2lnbmVkIGxvbmcgcmVzdWx0W10pIHsKICAgIC8vIOacgOS4iuS9jeOBruOCvOODreOCkumdnuihqOekuuOBq+OBmeOCi+OBn+OCgeOBruODleODqeOCsAogICAgaW50IGxlYWRpbmdfemVybyA9IDE7CgogICAgLy8g6YCG6aCG44Gn6KGo56S677yI5LiK5L2N5qGB44GL44KJ77yJCiAgICBmb3IgKGludCBpID0gNzsgaSA+PSAwOyBpLS0pIHsKICAgICAgICBpZiAocmVzdWx0W2ldICE9IDApIHsKICAgICAgICAgICAgbGVhZGluZ196ZXJvID0gMDsgLy8g5pyA5LiK5L2N44Gu44K844Ot44Gn44Gq44GP44Gq44KLCiAgICAgICAgfQogICAgICAgIGlmICghbGVhZGluZ196ZXJvKSB7CiAgICAgICAgICAgIHByaW50ZigiJTA4bHUiLCByZXN1bHRbaV0pOyAvLyA45qGB44Gu44K844Ot5Z+L44KB44Gn6KGo56S6CiAgICAgICAgfQogICAgfQoKICAgIC8vIOOCguOBl+WFqOOBpuOCvOODreOBquOCiQogICAgaWYgKGxlYWRpbmdfemVybykgewogICAgICAgIHByaW50ZigiMCIpOwogICAgfQoKICAgIHByaW50ZigiXG4iKTsKfQo=