执行程序找到第n个素数时出现问题。 [英] Problem while executing program to find nth prime no.

查看:116
本文介绍了执行程序找到第n个素数时出现问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我通过将n作为函数中的参数(void prime)来找到第n个素数的代码。但是我没有得到预期的答案。



我尝试过:



Below is the code I have made to find the nth prime no by taking "n" as the argument in the function (" void prime"). However I am not getting the answer as expected.

What I have tried:

#include<iostream>
#include<cmath>

using namespace std;

void prime(int n)
{
    int ans=0;
    //int c=0;
    int MAX=1000000;
    int temp=0;
    if(n==1)
    {

        cout<<2;
    }
    else if(n==2)
    {

        cout<<2;

    }
    else
    {

    for (int i=5;i<MAX;i++)
    {
        for (int j=2;j<sqrt;j++)
        {

            if(i%j==0)
                temp=1;
            break;

        }
        if(temp==1)
        ans++;
            if(ans==n)
            {
                cout<<"\n "<<n<<"th prime no. is "<<i;
                    break;
            }
    }
    }

}
int main()
{
    cout<<"\n Enter n";
    int n;
    cin>>n;
    prime(n);
    return 0;
}

推荐答案

因此请使用调试器并按照您的代码进行操作。

这是你的功课的一部分,让代码正常工作是任务的一部分:实际上,它通常是最有趣和最耗时的一点!



因此,在函数的第一行放置一个断点,并逐步完成代码,预先解决应该发生的事情。将其与发生的事情相比较,当它们不匹配时,您将开始了解原因。您可以使用调试器查看变量内容,逐行逐步执行代码,对于某些系统,您甚至可以在代码运行时编辑代码!



但我们不知道您使用的系统和IDE,因此我们无法给出明确的指示。谷歌的调试器和你的编译系统的名称,你应该至少找到关于如何使用它的基本说明。
So use the debugger and follow through what your code is doing.
This is part of your homework, and getting the code to work correctly is part of the task: indeed, it's often the bit that is most interesting and time consuming!

So put a breakpoint on the first line of the function, and step through the code working out in advance what should be happening. Compare that with what did happen and when they don't match you will start to get an idea as to why. You can use the debugger to look at variable contents, as well as stepping through your code line-by-line, and for some systems you can even edit the code while it is running!

But we don't know what system and IDE you are using, so we can't give explicit instructions. Google for "debugger" and the name of your compilation system and you should find at least basic instructions on how to use it.


首先,请遵循OriginalGriff的好建议:熟悉使用调试器。到目前为止,这可能是你在编程技巧上迈出的最大一步!



现在问题:你的代码中没有显示变量<正在计算code> sqrt 。现在的代码甚至都不会编译。



第二种:你已经使用变量 temp 作为指标那个不是素数。你首先假设它是素数,然后测试所有整数 j 它们是否是 i 的分隔符。一旦你找到一个 j 这是一个分隔符,那么 i 就不能是素数而你设置 temp 到1.到目前为止很好 - 但是你得出了错误的结论:

First of, please follow the good advice of OriginalGriff: Get familiar with using a debugger. That's probably going to be the biggest step you will make in your programming skills so far!

Now to your question: You did not show in your code how the variable sqrt is being computed. The code as it stands would not even compile.

And second: You have used the variable temp as indicator that i is not prime. You start off with the assumption that it is prime and then test all integers j whether they are a divider of i. As soon as you have found a j that is a divider, then i cannot be prime and you set temp to 1. So far so good -- but then you are drawing the wrong conclusion:
if (temp==1)
    ans++;
if (ans==n)
{
    cout<<"\n "<<n<<"th prime no. is "<<i;
    break;
}



在变量 ans 中,您可以计算已找到的素数远。所以你应该增加它,如果 temp 为0,即没有找到我的分隔符!



And还有一个潜伏的bug:你在函数的最开始初始化 temp 。现在考虑第一次将 temp 设置为1时会发生什么。您正在测试的下一个 i 会发生什么?看到问题?它很容易修复,但这是留给你的,因为这是你的作业。


In variable ans you count how many prime numbers you have found so far. So you should increase it, if temp is 0, i.e. no divider of i had been found!

And there is still one more bug lurking: You initialize temp at the very beginning of your function. Now consider what happens when temp has been set to 1 for the first time. What will happen to the next i you are testing? See the problem? It can easily be fixed, but that is left for you as it is your homework.


if(n==1)
{

    cout<<2;
}
else if(n==2)
{

    cout<<2;

}



高度怀疑拳头和第二个素数是2.

它还看起来代码不能编译。



使用调试器查看代码正在做什么。它允许你逐行执行第1行并在执行时检查变量。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码正在执行并确保它能达到预期的效果。



Debugger - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [< a href =http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-开始target =_ blanktitle =新

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有做到预期的时候,你就接近了一个错误。


It is highly suspect that fist and second primes are 2.
It also look like the code do not compile.

Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[<a href="http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn" target="_blank" title="New
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.


这篇关于执行程序找到第n个素数时出现问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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