使用python和C实现判断一个数是否为素数的方法比较

19 篇文章 0 订阅

先看C程序:

// divisors.c -- nested ifs display divisors of a number
#include <stdio.h>
#include <stdbool.h>  // 程序中包含了 stdbool.h 头文件,便可用bool代替_Bool类型,用true和false分别代替 1 和 0
int main(void)
{
    unsigned long num;          // number to be checked   待测试的数
    unsigned long div;          // potential divisors     可能的约数
    // 如果系统不支持_Bool 类型,可以把isPrime 的类型改为 int,并用1和0分别替换程序中的 true 和 false
    bool isPrime;               // prime flag             素数标记  不初始化,默认是false,即 int 的 0,可以通过 debug 查看

    printf("Please enter an integer for analysis; ");
    printf("Enter q to quit.\n");
    while (scanf("%lu", &num) == 1)
    {
        for (div = 2, isPrime = true; (div * div) <= num; div++)
        {
            if (num % div == 0)
            {   // 测试的数若是一个完全平方数,比如 144,显示144可以被 12 和 12 整数,显得有点傻!!!
                // 可以使用嵌套 if 语句测试 div是否等于num/div;如果是,程序只打印一个约数即可
                if ((div * div) != num)
                    printf("%lu is divisible by %lu and %lu.\n",
                           num, div, num / div);
                else
                    printf("%lu is divisible by %lu.\n",
                           num, div);     // 完全平方数的显示方式
                isPrime= false; // number is not prime    该数不是素数
            }
        }
        /*
           如何知道一个数是素数?如果 num是个素数,程序流不会进入for模块中的 if 语句,要解决这个问题,可以在外层循环把一个变量设置为某个值(如: 1)
           然后在for模块的if语句中把该变量重新设置为 0,循环完成后,检查该变量是否是 1,如果是,说明没有进入for模块的if语句,那么该数就是素数,
           这样的变量通常称为标记( flag )
         */
        if (isPrime)
            printf("%lu is prime.\n", num);
        printf("Please enter another integer for analysis; ");
        printf("Enter q to quit.\n");
    }
    printf("Bye.\n");

    return 0;
}

输出结果:

Please enter an integer for analysis; Enter q to quit.
5
5 is prime.
Please enter another integer for analysis; Enter q to quit.
144
144 is divisible by 2 and 72.
144 is divisible by 3 and 48.
144 is divisible by 4 and 36.
144 is divisible by 6 and 24.
144 is divisible by 8 and 18.
144 is divisible by 9 and 16.
144 is divisible by 12.
Please enter another integer for analysis; Enter q to quit.
2019
2019 is divisible by 3 and 673.
Please enter another integer for analysis; Enter q to quit.
2017
2017 is prime.
Please enter another integer for analysis; Enter q to quit.
q
Bye.

Process finished with exit code 0

再看python实现:

num = int(input('Please enter an integer for analysis:\n'))
# print('Enter q to quit.')
# print(type(num))    # 查阅文档发现 input() 函数返回值确实是str型
div = 2
isPrime = True
while(1):
    while(div*div <= num):
        if num % div ==0:
            if div*div != num:
                div2 = num/div
                #print('%d is divisible by %d and %d.'%(num,div,div2))
                print('{} is divisible by {} and {}.'.format(num,div,div2))
            else:
                #print('%d is divisible by %d.'%(num,div))
                print('{} is divisible by {}.'.format(num,div))
            isPrime = False
        div = div+1
    if isPrime:
        #print('%d is prime.'%num)
        print('{} is prime.'.format(num))
    break                               
    

输出结果:

Please enter an integer for analysis:
144
144 is divisible by 2 and 72.0.
144 is divisible by 3 and 48.0.
144 is divisible by 4 and 36.0.
144 is divisible by 6 and 24.0.
144 is divisible by 8 and 18.0.
144 is divisible by 9 and 16.0.
144 is divisible by 12.

# 再次运行 输入 17
Please enter an integer for analysis:
17
17 is prime.

 

 

 

 

 

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值