看到了一个很神的模板,具体什么意思还不理解,先马了再研究~
1 bool prime (long long num) 2 { 3 if (num == 2 || num == 3 || num == 5) 4 return true; 5 if (num % 2 == 0 || num % 3 == 0 || num % 5 == 0 || num == 1) 6 return false; 7 8 long long c = 7; 9 int maxc = (int)(sqrt (num)); 10 while (c <= maxc) 11 { 12 if (num % c == 0) 13 return false; 14 c += 4; 15 if (num % c == 0) 16 return false; 17 c += 2; 18 if (num % c == 0) 19 return false; 20 c += 4; 21 if (num % c == 0) 22 return false; 23 c += 2; 24 if (num % c == 0) 25 return false; 26 c += 4; 27 if (num % c == 0) 28 return false; 29 c += 6; 30 if (num % c == 0) 31 return false; 32 c += 2; 33 if (num % c == 0) 34 return false; 35 c += 6; 36 } 37 return true; 38 }
时间: 2024-10-29 00:28:45