#include <stdio.h>
#define MAX_DIGITS 56 // 7桁 x 8要素 (最大56桁)
void power3(unsigned long result[]);
void print_result(unsigned long result[]);
int main() {
unsigned long result[8] = {0}; // 配列の要素数は8で、各要素に最大7桁の数値を保持
result[0] = 1; // 3の0乗は1
for (int i = 1; i <= 99; i++) {
power3(result); // 3の累乗を計算
print_result(result); // 結果を表示
}
return 0;
}
// 3の累乗を計算して、結果をresultに格納する
void power3(unsigned long result[]) {
unsigned long carry = 0; // 繰り上げ用の変数
for (int i = 0; i < 8; i++) {
unsigned long temp = result[i] * 3 + carry;
result[i] = temp % 10000000; // 7桁保持
carry = temp / 10000000; // 繰り上げを次の要素に加算
}
}
// result配列に格納された数値を表示する
void print_result(unsigned long result[]) {
int start_print = 0; // 最初に0でない桁が見つかったかを確認するフラグ
for (int i = 7; i >= 0; i--) {
if (result[i] != 0 || start_print) {
if (!start_print) {
printf("%lu", result
[i
]); // 最初に0でない桁はそのまま表示 start_print = 1;
} else {
printf("%07lu", result
[i
]); // 残りは7桁でゼロ埋め }
}
}
if (!start_print) {
}
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIE1BWF9ESUdJVFMgNTYgIC8vIDfmoYEgeCA46KaB57SgICjmnIDlpKc1NuahgSkKCnZvaWQgcG93ZXIzKHVuc2lnbmVkIGxvbmcgcmVzdWx0W10pOwp2b2lkIHByaW50X3Jlc3VsdCh1bnNpZ25lZCBsb25nIHJlc3VsdFtdKTsKCmludCBtYWluKCkgewogICAgdW5zaWduZWQgbG9uZyByZXN1bHRbOF0gPSB7MH07ICAvLyDphY3liJfjga7opoHntKDmlbDjga8444Gn44CB5ZCE6KaB57Sg44Gr5pyA5aSnN+ahgeOBruaVsOWApOOCkuS/neaMgQogICAgcmVzdWx0WzBdID0gMTsgIC8vIDPjga4w5LmX44GvMQoKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IDk5OyBpKyspIHsKICAgICAgICBwb3dlcjMocmVzdWx0KTsgICAgICAgLy8gM+OBrue0r+S5l+OCkuioiOeulwogICAgICAgIHByaW50ZigiJWTkuZfnm646ICIsIGkpOwogICAgICAgIHByaW50X3Jlc3VsdChyZXN1bHQpOyAvLyDntZDmnpzjgpLooajnpLoKICAgIH0KCiAgICByZXR1cm4gMDsKfQoKLy8gM+OBrue0r+S5l+OCkuioiOeul+OBl+OBpuOAgee1kOaenOOCknJlc3VsdOOBq+agvOe0jeOBmeOCiwp2b2lkIHBvd2VyMyh1bnNpZ25lZCBsb25nIHJlc3VsdFtdKSB7CiAgICB1bnNpZ25lZCBsb25nIGNhcnJ5ID0gMDsgIC8vIOe5sOOCiuS4iuOBkueUqOOBruWkieaVsAoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgODsgaSsrKSB7CiAgICAgICAgdW5zaWduZWQgbG9uZyB0ZW1wID0gcmVzdWx0W2ldICogMyArIGNhcnJ5OwogICAgICAgIHJlc3VsdFtpXSA9IHRlbXAgJSAxMDAwMDAwMDsgIC8vIDfmoYHkv53mjIEKICAgICAgICBjYXJyeSA9IHRlbXAgLyAxMDAwMDAwMDsgICAgICAvLyDnubDjgorkuIrjgZLjgpLmrKHjga7opoHntKDjgavliqDnrpcKICAgIH0KfQoKLy8gcmVzdWx06YWN5YiX44Gr5qC857SN44GV44KM44Gf5pWw5YCk44KS6KGo56S644GZ44KLCnZvaWQgcHJpbnRfcmVzdWx0KHVuc2lnbmVkIGxvbmcgcmVzdWx0W10pIHsKICAgIGludCBzdGFydF9wcmludCA9IDA7ICAvLyDmnIDliJ3jgasw44Gn44Gq44GE5qGB44GM6KaL44Gk44GL44Gj44Gf44GL44KS56K66KqN44GZ44KL44OV44Op44KwCgogICAgZm9yIChpbnQgaSA9IDc7IGkgPj0gMDsgaS0tKSB7CiAgICAgICAgaWYgKHJlc3VsdFtpXSAhPSAwIHx8IHN0YXJ0X3ByaW50KSB7CiAgICAgICAgICAgIGlmICghc3RhcnRfcHJpbnQpIHsKICAgICAgICAgICAgICAgIHByaW50ZigiJWx1IiwgcmVzdWx0W2ldKTsgIC8vIOacgOWIneOBqzDjgafjgarjgYTmoYHjga/jgZ3jga7jgb7jgb7ooajnpLoKICAgICAgICAgICAgICAgIHN0YXJ0X3ByaW50ID0gMTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgIHByaW50ZigiJTA3bHUiLCByZXN1bHRbaV0pOyAgLy8g5q6L44KK44GvN+ahgeOBp+OCvOODreWfi+OCgQogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQoKICAgIGlmICghc3RhcnRfcHJpbnQpIHsKICAgICAgICBwcmludGYoIjAiKTsgIC8vIOWFqOOBpjDjgaDjgaPjgZ/loLTlkIjjga8w44KS6KGo56S6CiAgICB9CgogICAgcHJpbnRmKCJcbiIpOwp9