限定ビット幅に対する決定性素数性テスト ノート

限定ビット幅に対する決定性素数性テスト

問題:32ビットの数が素数かどうか(決定論的に)判定する 解答:(C++で) テストする基地。最初の4基数を使うことで、すべての32ビット整数に対して判定性 // になります。https://oeis.org/A014233 をご覧ください。int64_t基数[] = {2, 3, 5, 7};inline int countTrailingZeros(uint64_t n) { (n == 0) 64 返却 __builtin_ctzll(n); } int64_t modularExponentiation(int64_t 底、int64_t指数、int64_t modulus) { int64_t res = 1; int64_t b = 底数%のモジュラス;int64_t e = 指数; while (e > 0) { if (e & 1) { // 32ビット整数入力を仮定しているためオーバーフローしません res = (res * b) % modulus; } b = (b * b) % modulus; e >>= 1; } res 返却; } bool isPrime(int64_t n) { (n < 2) 返すならば false; if (n < 4) true 返す; (!