打印第N个素数的代码 [英] Code to print Nth prime number

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

问题描述

我必须打印第N个素数。
例如:

第一个质数是2

第二个质数是3





第十个素数是29,依此类推....



我的算法是如下所示:

1)将2和3相加。

2)如果n< =堆栈大小,则将项放在位置n并输出答案

3)否则,从堆栈的最后一个元素开始,检查是否为prime

4)要检查素数,请除以堆栈中的每个元素。如果堆栈中任何元素的余数为0,则不是素数。中断循环

5)如果是素数,则加到堆栈中

6)仅搜索奇数



我的代码是:

  #include< iostream> 
使用命名空间std;

int main(){
int number,count = 0;
cin>>数; //素数
int a [number]的位置;
a [0] = 2;
a [1] = 3;
int top = 1;
if(number< = 2){
cout<< a [数字-1]<<恩德尔
} else {
for(int i = 5; i< = 10001; i + = 2){
for(int j = 0; j< = top; j ++){

if(i%a [j]!= 0){
count ++;

}
if(count ==(top + 1)){
a [++ top] = i;
if(((count + 1)== number){
cout<<在顶上];
休息时间;
}

}

}

}

}
返回0;
}

此代码突然停止工作,没有给出任何输出。

解决方案

这是您的循环逻辑问题。您需要尝试用 i 除以所有数字,从 0 top 在您的堆栈上,并且仅当 i 被它们中的 none 整除时,您才增加计数。因为它是增加的,如果它们中的任何一个都不可分割。



因此,更改逻辑以测试 i 可被 a [j] 整除。如果是,则跳出循环。如果您到达循环的末尾(j == top)并且没有成功除以其中的任何一个,那么您就知道它是素数,可以增加计数。另外,将计数与 top 进行比较的支票应位于 j 循环之外(即,在完成所有操作之后)



  for(int i = 5; i <= 10001; i + = 2){
for(int j = 0; j< = top; j ++){
if(i%a [j] == 0){
break;
}
if(j == top)
{
count ++;
a [++ top] = i;
休息时间;
}
}
if(count == number){
cout<<在顶上];
休息时间;
}
}

编辑:您还需要初始化计数为2,而不是0,以计算2和3。


I have to print the Nth prime number. For example:
1st prime number is 2
2nd prime number is 3
.
.
10th prime number is 29 and so on....

My algorithm is as follows:
1) Add 2 and 3 to stack.
2) If n <= Size of stack, get item at position n and output answer
3) Else, start from last element of stack, check if prime
4) To check for prime, divide from each element in stack. If remainder 0 for any element in stack, not a prime. Break loop
5) If Prime, add to stack
6) Search only odd numbers

My code is :

#include <iostream>
using namespace std;

int main() {
    int number, count = 0;
    cin >> number;          //position of prime number
    int a[number];
    a[0] = 2;
    a[1] = 3;
    int top = 1;
    if (number <= 2) {
        cout << a[number - 1] << endl;
    } else {
        for (int i = 5; i <= 10001; i += 2) {
            for (int j = 0; j <= top; j++) {

                if (i % a[j] != 0) {
                    count++;

                }
                if (count == (top + 1)) {
                    a[++top] = i;
                    if ((count + 1) == number) {
                        cout << a[top];
                        break;
                    }

                }

            }

        }

    }
    return 0;
}

This code abruptly stops working without giving any output.What is the flaw in my code?

解决方案

It's a problem with your looping logic. You need to trial divide i by all the numbers from 0 to top on your stack, and only if i is divisible by none of them do you increase count. As it is you are increasing it if it is indivisible by any of them.

So, change the logic to test ifi is divisible by a[j]. If it is, then break out of the loop. If you reach the end of the loop (j == top) and it hasn't successfully divided any of them then you know it's prime and you can increase count. Also, the check where you compare count to top should be outside of the j loop (i.e. after you have done all the trial divisions)

    for (int i = 5; i <= 10001; i += 2) {
        for (int j = 0; j <= top; j++) {
            if (i % a[j] == 0) {
                break;
            }
            if(j == top)
            {
                count++;
                a[++top] = i;
                break;
            }
        }
        if (count == number) {
            cout << a[top];
            break;
        }
    }

Edit: you also need to initialize count to 2, not 0, to account for 2 and 3.

这篇关于打印第N个素数的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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