gpt4 book ai didi

c - 通过编码进行优化

转载 作者:行者123 更新时间:2023-11-30 21:20:21 25 4
gpt4 key购买 nike

我有一个素数程序,但我需要优化它。有什么提示吗?

  int num, i, count = 0;

printf("Type a non-negative integer:\n");

scanf("%d", &num);

if((num%2 == 0 )&& (num != 2)||(num == 1)){

printf("%d is not a prime number.\n", num);

} else if(num == 2){

printf("%d is a prime number.\n", num);

} else {

for(i = 3; i < num; i += 2){

if(num%i == 0){

count++;

break;

}

}

if(count == 0){

printf("%d is a prime number.\n", num);

} else {

printf("%d is not a prime number.\n", num);

}

}

}

最佳答案

您只需检查 num 的平方根即可。请参阅testing primes以获得更多提示。

关于c - 通过编码进行优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39582215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com