整型长整型和除法

发布于 2024-09-06 19:52:39 字数 1296 浏览 5 评论 0原文

我只是编写一个将无符号整数分解为素数的过程。如果我将数据类型定义为“int”,它会正常工作,如果我将其更改为“long”,结果将是错误的。我不知道为什么。

顺便说一句,我使用 Win-TC 作为我的编译器。

代码如下:

#include "stdio.h"
#define True    0xff
#define False   0x00
char DividerIsPrime(unsigned long data);
void CheckIfDataCanBeExtracted(unsigned long data);
main()
{
    unsigned long data;
    printf("please input data:");
    scanf("%d",&data);
    printf("\n%d=",data);
    CheckIfDataCanBeExtracted(data);
//    printf("%d",sizeof(short));
    getch();
}

void CheckIfDataCanBeExtracted(unsigned long data)
{
    unsigned long divider,temp,data1;
    data1=data;
    for(divider=2;divider<=data;divider++)
    {
         temp=data1%divider;
         if(temp) {continue;  }
         if(DividerIsPrime(divider)) {
        data1 = data1/divider;
        printf("%d",divider);
        if(data1==1) break;

        else {printf("*");  divider--;}


      }
    }
    return;

}

/* Check if this number is a prime number */
char DividerIsPrime(unsigned long data)
{
    unsigned long divider;
    char    status=True;
    for(divider=2;divider<data;divider++)
    {
        if(data%divider) status=True;
        else status=False;
    }
    return status;
}

感谢Paul的帮助,我知道哪里错了。 %d 应替换为 %ld。

I just write a procedure to decompose an unsigned integer to prime numbers. it will work normally if I define the data type as "int", if I change it to "long", result will be wrong. I don't know why.

BTW, I used Win-TC as my compiler.

Code as below:

#include "stdio.h"
#define True    0xff
#define False   0x00
char DividerIsPrime(unsigned long data);
void CheckIfDataCanBeExtracted(unsigned long data);
main()
{
    unsigned long data;
    printf("please input data:");
    scanf("%d",&data);
    printf("\n%d=",data);
    CheckIfDataCanBeExtracted(data);
//    printf("%d",sizeof(short));
    getch();
}

void CheckIfDataCanBeExtracted(unsigned long data)
{
    unsigned long divider,temp,data1;
    data1=data;
    for(divider=2;divider<=data;divider++)
    {
         temp=data1%divider;
         if(temp) {continue;  }
         if(DividerIsPrime(divider)) {
        data1 = data1/divider;
        printf("%d",divider);
        if(data1==1) break;

        else {printf("*");  divider--;}


      }
    }
    return;

}

/* Check if this number is a prime number */
char DividerIsPrime(unsigned long data)
{
    unsigned long divider;
    char    status=True;
    for(divider=2;divider<data;divider++)
    {
        if(data%divider) status=True;
        else status=False;
    }
    return status;
}

Thanks for Paul's help, I know where is wrong. %d should be replaced by %ld.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尹雨沫 2024-09-13 19:52:39

当前编写的函数 DividerIsPrime 存在缺陷,从逻辑上讲它必须始终返回 True

原因是状态在每次迭代时都会改变。即使达到 status=False(该数字是合数,因为除法器的模数为零),迭代仍将继续,并且在每种情况下,status=True当divider == (data - 1)时,将在最终迭代中达到。

您可以按如下方式更改此设置:

/* Check if this number is a prime number */
char DividerIsPrime(unsigned long data)
{
    unsigned long divider;
    for(divider=2;divider<data;divider++)
    {
        if (0==(data % divider))
            return False;
    }

    return True;
}

您可能会通过一些“单元测试”发现这一点,例如:

assert(DividerIsPrime(5));
assert(!DividerIsPrime(6));  /* This test would fail without corrected code. */

显然,对于“素性测试”有更有效的算法。

Your function DividerIsPrime,as currently written, has the defect that logically it must always return True.

The reason for this is that status is changed at each iteration. Even if status=False is reached (the number is composite because the modulus came out zero for a divider), then the iterations will continue and in every case, status=True will be reached on the final iteration when divider == (data - 1).

You can change this as follows:

/* Check if this number is a prime number */
char DividerIsPrime(unsigned long data)
{
    unsigned long divider;
    for(divider=2;divider<data;divider++)
    {
        if (0==(data % divider))
            return False;
    }

    return True;
}

You would have found this with some "unit test" such as:

assert(DividerIsPrime(5));
assert(!DividerIsPrime(6));  /* This test would fail without corrected code. */

Obviously there are much more efficient algorithms for "primality testing".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文