质数范围 [英] Range of prime numbers

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

问题描述

这是一个非常简单的问题,但是在提交时出现错误
问题:
编写一个函数isPrime(n),如果n为素数则分别输出1或0.在将开始整数(s)和结束整数(e)作为输入并在每次检测到质数时输出1以及在其他情况下都包含s和e的程序中使用此函数.下面给出几个例子.

it''s a pretty easy question, but im getting errors while submitting
Question:
Write a function isPrime(n) which outputs 1 or 0 if n is prime or not, respectively. Use this function in a program that takes a starting integer (s) and ending integer (e) as input and outputs 1 every time a prime number is detected and 0 otherwise, s and e are included. Few examples are given below.

./a.out "34" "40"
0 0 0 1 0 0 0				# only 37 as prime number

./a.out "4" "9"
0 1 0 1 0 0				# only 5 and 7 as prime numbers



我尝试过的事情:



What I have tried:

#include<stdio.h>
#include<stdlib.h>

void isPrime(int n);
int main(int argc,char*argv[])
{
    int s,e;
    if(argc!=3) exit(1);
    s=atoi(argv[1]);
    e=atoi(argv[2]);
    int i;
    for(i=s;i<=e;i++)
{
    isPrime(i);
}
return 0;
}
void isPrime(int n)
{
    int i, flag = 0;

    for(i=2; i<=n; ++i)
    {
        // condition for nonprime number
        if(n%i==0)
        {
            flag=1;
            break;
        }
    }

    if (flag==0)
        printf("1");
    else
        printf("0");
}

推荐答案

Member 13095321

这行中有一个错误:
Hi Member 13095321,

There is a mistake in this line:
for(i=2; i<=n; ++i)

应该是,

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

也不需要运行n来检查可除性.这个数字的一​​半是可以的.假设n = 37,则无需循环到36以检查可除性;循环到n/2 = 18会很好,因为后面的数字(19,20,....)并不是36的因数.

还有其他查找素数的有效方法.您也可以尝试使用它们.

Also there is no need to run up to n for checking divisibility. Half of that number is fine. Say n=37, then there is no need to loop up to 36 to check the divisibility; looping up to n/2 = 18 would be fine as later numbers (19, 20,....) are not factors of 36.

There are other efficient approaches for finding primes. You can try them as well.


报价:

但是在提交时出现错误


哪些错误消息?
假设您有C编译器,则应该学习如何使用调试器,它是一个了不起的工具.

当您不了解代码在做什么或为什么要做什么时,答案是调试器.
使用调试器查看您的代码在做什么.只需设置一个断点并查看您的代码执行情况,调试器就可以让您逐行执行第1行并在执行时检查变量,这是一个了不起的学习工具.

调试器-维基百科,免费百科全书 [ Visual Studio 2010中的精通调试-入门指南 [ ^ ]
使用Visual Studio 2010进行基本调试-YouTube [


Which error messages ?
Assuming you have a C compiler, you should learn how to use a debugger, it is an incredible tool.

When you don''t understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner''s Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don''t find bugs, it just help you to. When the code don''t do what is expected, you are close to a bug.


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

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