素数检查 C++ 函数输出非素数的数字
prime checking c++ function outputs numbers that aren't prime
我正在制作一个C++程序,它允许您输入一个数字并检查它是否为素数。但它说,像9、15和21这样的数字是素数。我能帮忙吗?这很令人困惑。这是我的函数,检查它是否是素数:
bool isPrime(int num) {
int w = 2;
while (w <= num) {
if (w % num == 0) {
return false;
}
else if (w < num){
w = w + 1;
}
if (w == num) {
w = 0;
return true;
}
}
}
当你意识到所有大于3的素数都可以写为6n+1或6n+5,对于自然n时,可以获得Aconcagua的额外加速解。或者更进一步,所有大于5的素数都可以写成30n+m,其中m{1,7,11,13,17,19,23,29}。这就是所谓的车轮分解。
这可以简单地理解为:
- 2的轮因子分解(cfr.Aconcagua(:如果n不能被2整除,那么n就不能被2的任何倍数整除
- 6=2x3的轮因子分解:如果n不可被2整除,则n不能被2的任何倍数整除,如果n不能被3整除,那么n-也不能被3的任何倍数所整除
- 30=2x3x5的车轮因数分解:见上文
因此实现6的Wheel因子分解,快速给出:
if (num == 1) return false;
if (num < 4) return true;
if (num % 2 == 0) return false;
if (num % 3 == 0) return false;
int w = 5;
while (w*w <= num)
{
if(num % (w-2) == 0) return false;
if(num % w == 0) return false;
w += 6;
}
return true;
该算法的运行速度应为Aconcagua求解速度的2/3。
备注:30的轮分解只会产生较小的加速,因为它只消除了序列30n+25,该序列也被6的轮分解覆盖为6*(5*n+4(+1。
备注:这仍然测试不应该测试的数字,例如(w=25而我们已经知道w-2=5被测试,35,49同上,…(
如果你想变得更健壮一点并使用一点内存,你可能会对Eratosthenes筛感兴趣。
其他有用的信息可以在这里找到:primes
已经发现了实际的错误(w % num
而不是num % w
(,只需要一些额外的提示:
你的代码太复杂了!
while (w <= num) // why <=? w == num is irrelevant, in worst
// case, it will lead to false negatives (num % num == 0)!
{
if (num % w == 0) // (already fixed!)
{
return false;
}
else if (w < num)
{
w = w + 1;
}
if (w == num) // as you increment by 1, this will always be false unless
// previous test failed - so simply use else instead
{
w = 0;
return true;
}
}
第一步:
while (w < num)
{
if (w % num == 0)
{
return false;
}
/*else*/ if (w < num) // however, this check is repeated in the while
// loop anyway; no need to do the work twice
{
++w; // shorter...
}
else
{
// w = 0; // obsolete, we will be destroyed afterwards anyway...
return true;
}
}
第二步:
while (w < num)
{
if (w % num == 0)
{
return false;
}
++w; // at some point, will reach w == num and the loop won't be re-entered
}
// we did not leave the loop prematurely (-> non-prime), so we are prime:
return true;
优化:
- 如果
num == n * m
和n
大于sqrt(num)
,则m
较小!因此,当检查m时,n
已经被捕获,所以您不必检查大于平方根的值。这将排除大量的数字 - 如果
n
不设计num
,那么k * m
也不会。对于2的倍数,不从中获利太简单了(尽管仍然很容易,但考虑到3、5…的倍数,它会变得更复杂(
应用这些:
if(num % 2 == 0)
return false;
int w = 3;
while (w*w <= num) // be aware that I had an error here in my comment
// to the question - cannot fix it any more, though...
{
if(num % w == 0)
return false;
w += 2;
}
return true;
我相信你想要if(num % w == 0)
不if(w % num == 0)
这里的代码可能有助于`bool isPrime(int num(
int w = 2;
while (w <= num) {
if (num % w == 0) {
return false;
}
else if (w < num){
w = w + 1;
}
if (w == num) {
w = 0;
return true;
}
}
`
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 函数在许多数字上转换为基数 1 时减去 2?
- 函数如何使用引用返回所需的数字?
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 如何在 setup() 函数中使用数字引脚转动电机?
- C++ python 中的函数返回数字而不是字符串
- 如何在不使用 C++ 中的数组或函数的情况下查找 N 位数字的所有排列
- 将 C 函数转换为 C++ 以检查数字是否有效
- 在C++中编程,将 3 个数字发送到一个函数,然后计算这 3 个数字的平均函数
- 是否有一个 std::set 函数来确定不超过数字 x 的最大元素?
- 在 c++ 中将数字转换为矢量数组的函数
- 使用带有一个参数函数的递归找到数字的平方
- 我想使用 std::stoi 函数在 cpp 中将字符串转换为整数,因为我想在字符串中找到不同数字的总和(在下级酶中)
- 为什么当我在 c++ 中运行函数时,我的代码显示数字 53
- 我有模板功能.通过使用函数std::for_each,用这个容器中的最大数字替换每个正数
- 如何用不同的参数类型和数字回调函数
- 我必须更改我的数字最后一个数字和第一个数字,但不要使用仅带有整数或循环的函数.例如从 12345 到 52341
- 素数检查 C++ 函数输出非素数的数字
- C++ 在地板函数之后以十六进制打印数字