Ç - 确定一个数是素 [英] C - determine if a number is prime

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

问题描述

我想拿出这需要一个整数,并返回一个布尔说,如果该数是素与否,我不知道很多℃的方法;会有人关心给我一些指点?

基本上,我会做这在C#这样的:

 静态布尔IsPrime(INT数)
{
    的for(int i = 2; I<数;我++)
    {
        如果(编号%我== 0安培;&安培;!I =号)
            返回false;
    }
    返回true;
}


解决方案

好了,忘了C.假设我给你一个号码,请你确定它是否是素数。你是怎么做到的呢?写清楚走下台阶,的然后的担心将其转化为code。

一旦你的算法来确定,这将是更容易为你找出如何写一个程序,并为别人帮你吧。

编辑:这里的C#code你贴:

 静态布尔IsPrime(INT编号){
    的for(int i = 2; I<数;我++){
        如果(编号%我== 0安培;&安培;!I =号)返回false;
    }
    返回true;
}

这是的非常接近的有效C作为的;有没有布尔键入C,并没有真正,所以你需要修改它一点点(编辑:Kristopher约翰逊正确地指出C99增加了stdbool.h头)。由于一些人没有访问C99环境(!但你应该使用一个),让我们做出很小的变化:

  INT IsPrime(INT编号){
    INT I;
    对于(i = 2; I<数;我++){
        如果(编号%我== 0安培;&安培; I =号!)返回0;
    }
    返回1;
}

这是一个完全有效的C程序,你想要做什么。我们可以提高一点点没有太多精力。首先,注意 I 总是比数量较少,所以检查我!=号总是成功;我们可以摆脱它。

另外,你实际上并不需要最多尝试除数一路 - 1 ;当你到达的sqrt(数字)可以停止检查。由于开方是一个浮点运算,并带来微妙一大堆,我们实际上并不会计算的sqrt(数字)。相反,我们可以只检查 I * I< =号

  INT IsPrime(INT编号){
    INT I;
    对于(i = 2; I * I< =数字;我++){
        如果(编号%我== 0)返回0;
    }
    返回1;
}

最后一件事,虽然,有你的原始算法个小bug!如果数量为负数或零,或一,这个功能会声称数是素数。你可能想处理的得当,你可能想使数量无符号,因为你很可能会关心正值只有:

  INT IsPrime(无符号整数号){
    如果(数字< = 1)返回0; //零和一不素
    无符号整型我;
    对于(i = 2; I * I< =数字;我++){
        如果(编号%我== 0)返回0;
    }
    返回1;
}

这绝对不是检查一个数是素以最快的方式,但它的作品,它的pretty简单。我们几乎不得不修改所有的code!

I am trying to come up with a method that takes an integer and returns a boolean to say if the number is prime or not and I don't know much C; would anyone care to give me some pointers?

Basically, I would do this in C# like this:

static bool IsPrime(int number)
{
    for (int i = 2; i < number; i++)
    {
        if (number % i == 0 && i != number)
            return false;
    }
    return true;
}

解决方案

OK, so forget about C. Suppose I give you a number and ask you to determine if it's prime. How do you do it? Write down the steps clearly, then worry about translating them into code.

Once you have the algorithm determined, it will be much easier for you to figure out how to write a program, and for others to help you with it.

edit: Here's the C# code you posted:

static bool IsPrime(int number) {
    for (int i = 2; i < number; i++) {
        if (number % i == 0 && i != number) return false;
    }
    return true;
}

This is very nearly valid C as is; there's no bool type in C, and no true or false, so you need to modify it a little bit (edit: Kristopher Johnson correctly points out that C99 added the stdbool.h header). Since some people don't have access to a C99 environment (but you should use one!), let's make that very minor change:

int IsPrime(int number) {
    int i;
    for (i=2; i<number; i++) {
        if (number % i == 0 && i != number) return 0;
    }
    return 1;
}

This is a perfectly valid C program that does what you want. We can improve it a little bit without too much effort. First, note that i is always less than number, so the check that i != number always succeeds; we can get rid of it.

Also, you don't actually need to try divisors all the way up to number - 1; you can stop checking when you reach sqrt(number). Since sqrt is a floating-point operation and that brings a whole pile of subtleties, we won't actually compute sqrt(number). Instead, we can just check that i*i <= number:

int IsPrime(int number) {
    int i;
    for (i=2; i*i<=number; i++) {
        if (number % i == 0) return 0;
    }
    return 1;
}

One last thing, though; there was a small bug in your original algorithm! If number is negative, or zero, or one, this function will claim that the number is prime. You likely want to handle that properly, and you may want to make number be unsigned, since you're more likely to care about positive values only:

int IsPrime(unsigned int number) {
    if (number <= 1) return 0; // zero and one are not prime
    unsigned int i;
    for (i=2; i*i<=number; i++) {
        if (number % i == 0) return 0;
    }
    return 1;
}

This definitely isn't the fastest way to check if a number is prime, but it works, and it's pretty straightforward. We barely had to modify your code at all!

这篇关于Ç - 确定一个数是素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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