C中的素数算法 [英] Prime number algorithm in C

查看:59
本文介绍了C中的素数算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,


这是测试数字是否为素数的解决方案:


/ *

*是素数?

*返回TRUE(1):n是素数

*返回FALSE(0):n是a *不是*素数

* /

int is_prime_number(int n)

{

long c;


if(n< 2)返回FALSE;


for(c = 2; c< n; c ++)

{

如果((n%c)== 0)返回FALSE;

}

返回TRUE;

}


Tia,


Chris



is this is this solution to test if a number is a prime number or not:

/*
* Is n a prime number?
* Return TRUE (1): n is a prime number
* Return FALSE (0): n is a *not* a prime number
*/
int is_prime_number(int n)
{
long c;

if (n < 2) return FALSE;

for (c = 2; c < n; c++)
{
if ((n % c) == 0) return FALSE;
}
return TRUE;
}

Tia,

Chris

推荐答案

2003年11月17日星期一16:28:10 +0000,Cmorriskuerten写道:
On Mon, 17 Nov 2003 16:28:10 +0000, Cmorriskuerten wrote:
HI,

这是这个解决方案测试数字是否为素数:

/ *
*是素数?
*返回TRUE(1):n是素数
*返回FALSE(0):n是*不是*素数
* /
int is_prime_ number(int n)
{
long c;

if(n< 2)返回FALSE;

for(c = 2; c< n; c ++)
{
if((n%c)== 0)return FALSE;
}
返回TRUE;
}


is this is this solution to test if a number is a prime number or not:

/*
* Is n a prime number?
* Return TRUE (1): n is a prime number
* Return FALSE (0): n is a *not* a prime number
*/
int is_prime_number(int n)
{
long c;

if (n < 2) return FALSE;

for (c = 2; c < n; c++)
{
if ((n % c) == 0) return FALSE;
}
return TRUE;
}




在comp.programming中提出这样的问题会更好

下次。


但是,看起来该功能确实有效。不过,它会很慢。



It would be better to ask questions like this in comp.programming
next time.

However, it does appear that the function would work. It would be
quite slow, though.


Cmorriskuerten写道:
Cmorriskuerten writes:
这是一个测试数字是否为素数的解决方案:

/ *
*是素数?
*返回TRUE(1):n是素数
*返回FALSE(0):n是*不是素数
* /
int is_prime_number(int n)

long c ;

if(n< 2)返回FALSE;

for(c = 2; c< n; c ++)
{
if ((n%c)== 0)返回FALSE;
}
返回TRUE;
}
is this is this solution to test if a number is a prime number or not:

/*
* Is n a prime number?
* Return TRUE (1): n is a prime number
* Return FALSE (0): n is a *not* a prime number
*/
int is_prime_number(int n)
{
long c;

if (n < 2) return FALSE;

for (c = 2; c < n; c++)
{
if ((n % c) == 0) return FALSE;
}
return TRUE;
}




逻辑看起来没问题对我而言,酸测试是尝试它。我提到逻辑

,因为我不知道在这个

片段中TRUE和FALSE是如何获得它们的值的。我不喜欢int和long的混合。我不喜欢这个名字。

我们已经*知道*这是一个数字。怎么样is_prime?为什么''c''?候选人?


你可以通过避免测试偶数,除了2来改变增量来改善它。此外,一旦你达到n的平方根,

任何进一步的测试都注定要失败,所以你不妨停在那里。

但是如果插入平方根以一种显而易见的方式测试你在编译器上依赖

,以便足够智能地优化它。所以*可能*

实际上变慢了。 :-(



The logic looks OK to me, but the acid test is to try it. I mention logic
because I don''t know how TRUE and FALSE obtained their values in this
snippet. I am not fond of the mix of int and long. I don''t like the name.
We already *know* it''s a number. How about is_prime? Why ''c''? Candidate?

You could improve it a lot by avoiding the test for even numbers except 2 by
changing the increment. Also, once you have reached the square root of n,
any further testing is doomed to failure so you might as well stop there.
But if you insert the square root test in the obvious way you are depending
on the compiler to be smart enough to optimize it away. So it *could*
actually get slower. :-(


osmium< r1 ******** @ comcast.net>这样说:
osmium <r1********@comcast.net> spoke thus:
你可以通过避免测试偶数除了2来改变增量来改善它。同样,一旦你达到n的平方根,
任何进一步的测试注定要失败所以你可能会也就是停在那里。
但是如果你以明显的方式插入平方根测试,那么你对编译器的依赖是足够智能的,以便将它优化掉。所以它*可能*
实际上慢一点。: - (
You could improve it a lot by avoiding the test for even numbers except 2 by
changing the increment. Also, once you have reached the square root of n,
any further testing is doomed to failure so you might as well stop there.
But if you insert the square root test in the obvious way you are depending
on the compiler to be smart enough to optimize it away. So it *could*
actually get slower. :-(




他可以停在n / 2并且仍然将运行时间缩短一半...


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org | don'我需要知道。火焰欢迎。



He could stop at n/2 and still cut the run time in half...

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


这篇关于C中的素数算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆