C++ 数论算法详解:素数判断、最大公约数、快速幂
C++ \u6570\u8bba\u7b97\u6cd5\u4ecb\u7ecd\n\n\u6570\u8bba\u662f\u7814\u7a76\u6574\u6570\u53ca\u5176\u6027\u8d28\u7684\u4e00\u652f\u5206\u79d1\u7684\u5b66\u79d1\uff0c\u5b83\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002C++ \u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u6570\u8bba\u7b97\u6cd5\u7684\u5b9e\u73b0\u3002\u672c\u7bc7\u535a\u5ba2\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684 C++ \u6570\u8bba\u7b97\u6cd5\uff0c\u4ee5\u5e2e\u52a9\u8bfb\u8005\u66f4\u597d\u7684\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u7b97\u6cd5\u3002\n\n1. \u7c7b\u6570\u5224\u65ad\u7b97\u6cd5\n\n\u5224\u65ad\u4e00\u4e2a\u6570\u662f\u5426\u4e3a\u7c7b\u6570\u662f\u6570\u8bba\u4e2d\u7684\u57fa\u672c\u95ee\u9898\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u7c7b\u6570\u5224\u65ad\u7b97\u6cd5\u662f\u904d\u5386\u4ece 2 \u5230 n-1 \u7684\u6240\u6709\u6570\uff0c\u5224\u65ad\u662f\u5426\u6709\u56e0\u5b50\u80fd\u6574\u9664 n\u3002\u4f46\u662f\u8fd9\u79cd\u7b97\u6cd5\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u8f83\u9ad8\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7b97\u6cd5\u6765\u89e3\u51b3\u3002\n\n\u4e00\u79cd\u5e38\u7528\u7684\u7b97\u6cd5\u662f\u8bd5\u9664\u6cd5\u3002\u5bf9\u4e8e\u4e00\u4e2a\u6570 n\uff0c\u4ece 2 \u5f00\u59cb\uff0c\u4e0d\u65ad\u8bd5\u9664 n\u7684\u6240\u6709\u53ef\u80fd\u56e0\u5b50\uff0c\u5982\u679c\u627e\u5230\u4e00\u4e2a\u80fd\u6574\u9664 n\u7684\u56e0\u5b50\uff0c\u5219 n \u4e0d\u662f\u7c7b\u6570\uff1b\u5982\u679c\u8bd5\u9664\u5230 sqrt(n) \u90fd\u6ca1\u6709\u627e\u5230\u80fd\u6574\u9664 n\u7684\u56e0\u5b50\uff0c\u5219 n \u662f\u7c7b\u6570\u3002\n\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u7c7b\u6570\u5224\u65ad\u7b97\u6cd5\u7684 C++ \u4ee3\u7801\uff1a\n\n```cppbool isPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true;}
\n\n2. \u6c42\u6700\u5927\u516c\u7ea6\u6570\u7b97\u6cd5\n\n\u6700\u5927\u516c\u7ea6\u6570\uff08GCD\uff09\u662f\u6570\u8bba\u4e2d\u7684\u53e6\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\uff0c\u5b83\u8868\u793a\u4e24\u4e2a\u6570\u4e2d\u80fd\u540c\u65f6\u6574\u9664\u7684\u6700\u5927\u7684\u6b63\u6574\u6570\u3002\u6c42\u4e24\u4e2a\u6570\u7684\u6700\u5927\u516c\u7ea6\u6570\u6709\u591a\u79cd\u7b97\u6cd5\uff0c\u5176\u4e2d\u4e00\u79cd\u5e38\u7528\u7684\u7b97\u6cd5\u662f\u6b27\u6d32\u7eb3\u7b97\u6cd5\u3002\n\n\u6b27\u6d32\u7eb3\u7b97\u6cd5\u7684\u57fa\u672c\u601d\u60f3\u662f\u901a\u8fc7\u4e0d\u65ad\u53d6\u4f59\u7684\u65b9\u5f0f\uff0c\u5c06\u4e24\u4e2a\u6570\u4e4b\u95f4\u7684\u6bd4\u8f83\u5927\u7684\u6570\u66ff\u6362\u4e3a\u4e24\u6570\u7684\u4f59\u6570\uff0c\u76f4\u5230\u4f59\u6570\u4e3a 0\uff0c\u65f6\u5019\u6bd4\u8f83\u5c0f\u7684\u6570\u5c31\u662f\u6700\u5927\u516c\u7ea6\u6570\u3002\n\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u6700\u5927\u516c\u7ea6\u6570\u7b97\u6cd5\u7684 C++ \u4ee3\u7801\uff1a\n\n```cppint gcd(int a, int b) { while (b != 0) { int temp = a % b; a = b; b = temp; } return a;}
\n\n3. \u5feb\u901f\u6743\u7b97\u7b97\u6cd5\n\n\u5feb\u901f\u6743\u7b97\u7b97\u6cd5\u662f\u8ba1\u7b97\u4e00\u4e2a\u6570\u7684\u6743\u7684\u9ad8\u6548\u7b97\u6cd5\u3002\u5b83\u7684\u57fa\u672c\u601d\u60f3\u662f\u5229\u7528\u6307\u6570\u7684\u4e8c\u8fdb\u5236\u8868\u793a\u6765\u8fdb\u884c\u8ba1\u7b97\u3002\n\n\u5177\u4f53\u505a\u6cd5\u662f\u5c06\u6307\u6570\u8868\u793a\u4e3a\u4e8c\u8fdb\u5236\uff0c\u4ece\u6700\u4f4e\u4f4d\u5f00\u59cb\uff0c\u5982\u679c\u67d0\u4f4d\u4e3a 1\uff0c\u5219\u5c06\u7ed3\u679c\u4e58\u4e0a\u5e95\u6570\u7684\u76f8\u5e94\u6b21\u6743\uff0c\u7136\u540e\u5c06\u5e95\u6570\u81ea\u4e58\uff0c\u7ee7\u7eed\u5904\u7406\u4e0b\u4e00\u4f4d\u3002\u6700\u540e\u5f97\u5230\u7684\u7ed3\u679c\u5c31\u662f\u6307\u6570\u7684\u6743\u3002\n\n\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u73b0\u4e86\u5feb\u901f\u6743\u7b97\u7b97\u6cd5\u7684 C++ \u4ee3\u7801\uff1a\n\n```cppint fastPow(int x, int n) { int res = 1; while (n > 0) { if (n % 2 == 1) { res *= x; } x *= x; n /= 2; } return res;}
\n\n\u603b\u7ed3\n\n\u672c\u7bc7\u535a\u5ba2\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u5e38\u7528\u7684 C++ \u6570\u8bba\u7b97\u6cd5\uff0c\u5305\u62ec\u7c7b\u6570\u5224\u65ad\u3001\u6700\u5927\u516c\u7ea6\u6570\u548c\u5feb\u901f\u6743\u7b97\u7b97\u6cd5\u3002\u8fd9\u4e9b\u7b97\u6cd5\u5728\u8ba1\u7b97\u673a\u79d1\u5b66\u4e2d\u5177\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u5e0c\u671b\u8bfb\u8005\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\u80fd\u591f\u66f4\u597d\u7684\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u7b97\u6cd5\u3002\u5982\u679c\u5bf9\u4e8e\u6570\u8bba\u7b97\u6cd5\u8fd8\u6709\u5174\u8da3\u7684\u8bdd\uff0c\u8fd8\u53ef\u4ee5\u7ee7\u7eed\u6df1\u5165\u5b66\u4e60\u548c\u7814\u7a76\u3002
原文地址: https://www.cveoy.top/t/topic/pE1n 著作权归作者所有。请勿转载和采集!