简单的质数程序...我的代码/脚本有什么问题?

发布于 2024-12-11 20:47:16 字数 903 浏览 0 评论 0原文

我想知道我可以在调试或发现程序中的错误时获得一些帮助。目标是获取用户输入,然后显示从输入到零的素数、最大素数到最小素数。

问题是输出包括用户输入,该输入本身可能是也可能不是素数,并且多次重复素数:( 另外,我想知道为什么不包括2?

我的代码:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int prime, division, input;
    cout << "Please enter a number you wish to count down prime numbers from." << endl;
    cin >> input;

    for (prime = input ; prime >=2 ; prime-- )
    {
        for (division = 2 ; division < prime ; division++ )
        {
            if (prime % division == 0)
            {
            break;
            }
            else
            {
            cout << prime << " ";
            }
        }
    }
    return 0;
}

我的输出:

请输入您想要从中倒数素数的数字。 15

15 13 13 13 13 13 13 13 13 13 13 13 11 11 11 11 11 11 11 11 11 9 7 7 7 7 7 5 5 5 3

感谢那些提供帮助的人!

I am wondering I can get some help at debugging or spotting the error in my program. The objective is to obtain user input and then display primes from input to zero, greatest prime to lowest.

Problem is the output includes the user input which may or may not be a prime number in itself, and repeats primes several times :(
Also, I am wondering why 2 isn't included?

My code:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int prime, division, input;
    cout << "Please enter a number you wish to count down prime numbers from." << endl;
    cin >> input;

    for (prime = input ; prime >=2 ; prime-- )
    {
        for (division = 2 ; division < prime ; division++ )
        {
            if (prime % division == 0)
            {
            break;
            }
            else
            {
            cout << prime << " ";
            }
        }
    }
    return 0;
}

My output:

Please enter a number you wish to count down prime numbers from.
15

15 13 13 13 13 13 13 13 13 13 13 13 11 11 11 11 11 11 11 11 11 9 7 7 7 7 7 5 5 5 3

Thanks for those who help!

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

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

发布评论

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

评论(5

眼泪都笑了 2024-12-18 20:47:16

这是一个判断输入数字是否为素数的程序。任何数字都可以被始终小于它的数字完全整除。
如果是素数,它们可以完全被 1 和它本身整除;
所以我使用了一个计数器变量,它计算一个数字完全被一个或小于它的数字整除的次数。
对于素数,计数始终为 2,对于其他数,计数将大于 2。
计数将是 1 为 1...
所以程序如下......

    #include<iostream.h>
    #include<conio.h>
    class prime
    {
       int a;
       public:
         void check();

    };
       void prime::check()
                         {
                          cout<<"Insert a number";
                          cin>>a;
                          int count=0;
                          for(int i=a;i>=1;i--)
                             {
                                if(a%i==0)
                                      {
                                        count++;
                                       }
                               }
                                 if(count==1)
                                           {
                                cout<<"\nOne is neither prime nor composite";
                                            }
                                  if(count>2)
                                           {
                                cout<<"\nthe number is not prime its composite" ;
                                           }
                                 if(count==2)
                                          {    
                                cout<<"\nthe numner is prime";
                                           }
             }

     void main()
     {
      clrscr();
     prime k;
     k.check();
     getch();
     }

打印所有小于输入给定数字的素数;代码如下:

      #include<iostream.h>
      #include<stdio.h>
      #include<conio.h>
        class prime
        {
               int a;
               int i;
        public:
          void display();
        };

          void prime::display()
                   {
                     cout<<"Enter any number to find primes less than it";
                     cin>>a;
                        int count=0;
                     for(int j=a;j>=1;j--)
                      {
                        for(int i=1;i<=j;i++)
                             {
                                if(j%i==0)
                                         {
                                           count++;
                                          }
                               }
                        if(count==2)
                                   {
                                     cout<<"\n"<<j;
                                    }
                         count=0;
                        }
                  }
     void main()
     {
        clrscr();
        prime k;
        k.display();
        getch();
      }

This is a program which tells whether the input number is prime or not.Any number is completely divisible by a number which is always less than it.
In case of prime numbers they are completely divisible by 1 and itself;
So i have used a counter variable which counts how many times a number is completely divisible by a number or numbers less than it.
The count will be always 2 for prime numbers and count will be more than two for others.
Count will be 1 for one...
So the program is as follows....

    #include<iostream.h>
    #include<conio.h>
    class prime
    {
       int a;
       public:
         void check();

    };
       void prime::check()
                         {
                          cout<<"Insert a number";
                          cin>>a;
                          int count=0;
                          for(int i=a;i>=1;i--)
                             {
                                if(a%i==0)
                                      {
                                        count++;
                                       }
                               }
                                 if(count==1)
                                           {
                                cout<<"\nOne is neither prime nor composite";
                                            }
                                  if(count>2)
                                           {
                                cout<<"\nthe number is not prime its composite" ;
                                           }
                                 if(count==2)
                                          {    
                                cout<<"\nthe numner is prime";
                                           }
             }

     void main()
     {
      clrscr();
     prime k;
     k.check();
     getch();
     }

To print all the prime numbers less than the number given as input; code is as follows:

      #include<iostream.h>
      #include<stdio.h>
      #include<conio.h>
        class prime
        {
               int a;
               int i;
        public:
          void display();
        };

          void prime::display()
                   {
                     cout<<"Enter any number to find primes less than it";
                     cin>>a;
                        int count=0;
                     for(int j=a;j>=1;j--)
                      {
                        for(int i=1;i<=j;i++)
                             {
                                if(j%i==0)
                                         {
                                           count++;
                                          }
                               }
                        if(count==2)
                                   {
                                     cout<<"\n"<<j;
                                    }
                         count=0;
                        }
                  }
     void main()
     {
        clrscr();
        prime k;
        k.display();
        getch();
      }
静待花开 2024-12-18 20:47:16

试试这个代码

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int prime, division, input;
    cout << "Please enter a number you wish to count down prime numbers from." << endl;
    cin >> input;




    for (prime = input ; prime >=2 ; prime--)
    {
        boolean isDivisible = false;
        for (division = 2 ; division < prime ; division ++)
        {
            if (prime % division == 0)
            {
                isDivisible = true;
            }
        }
        if (isDivisible == false)
        {
            cout << prime << " ";
        }
    }
    return 0;
}

Try this code

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int prime, division, input;
    cout << "Please enter a number you wish to count down prime numbers from." << endl;
    cin >> input;




    for (prime = input ; prime >=2 ; prime--)
    {
        boolean isDivisible = false;
        for (division = 2 ; division < prime ; division ++)
        {
            if (prime % division == 0)
            {
                isDivisible = true;
            }
        }
        if (isDivisible == false)
        {
            cout << prime << " ";
        }
    }
    return 0;
}
伴我老 2024-12-18 20:47:16

对于第一个问题:

对于9,当division == 2时,9 % 2 ==1,所以打印9。而不是你应该有一个标志值来表示判断一个数是否是质数,确定它是质数后打印该数。

对于 2,编辑:当 prime == 2 时,并且因为 division prime 是内循环执行的条件,内循环退出而不运行。所有功劳归于马龙!

For the first problem:

for 9, when division == 2, 9 % 2 ==1, so 9 is printed.Rather than You should have a flag value to denote if a number is prime or not, and print the number after you are sure it is prime.

For 2, EDIT: when prime == 2, and because division < prime is the condition that the inner loop get executed, the inner loop exits without running. All credits go to Marlon!

谜兔 2024-12-18 20:47:16

检查你的内部 for 循环。每次运行循环时,初始化器都会重置为 2。然后对可疑素数执行 Mod 2。如果你得到 0,你真正知道的是这个数字是偶数。这可能会让你很快说这个数字不是素数,但 2 mod 2 是 0,并且 2 是素数。循环结束后,“除法”变为 3。15 mod 3 为 0。此时,您将跳出循环,但 15 不是质数。检查您的确定素数的算法。您可以让它检查已知的素数,但这不够动态。还有其他方法,其中之一围绕确定可疑素数的平方根。最后,您可以进行一些良好的旧(长)纸笔调试。

Check your inner for loop. Each run through the loop, your initializer gets reset to 2. Then you do Mod 2 on the suspected prime. If you get 0, all you really know is that the number is even. That might lead you to quickly say that the number isn't prime, but 2 mod 2 is 0, and 2 is prime. After the loop, "division" becomes 3. 15 mod 3 is 0. At that point, you break out of the loop, but 15 is NOT prime. Check your algorithm for determining primes. You could have it check against known primes, but that's not dynamic enough. There are other methods, one of which revolves around determining the suspected prime number's square root. Finally, you could do some good old (long) paper-pencil debugging.

一曲爱恨情仇 2024-12-18 20:47:16

你的内循环不正确。它不打印素数,只打印奇数。它在第一次迭代期间检查一个数字是否可以被 2 整除,并且偶数总是可以被整除。因此,如果数字是偶数,内部循环总是会中断,但如果是奇数,它会打印数字,并继续这样做,直到循环中断或终止。

让我们尝试用一个例子来说明这一点,外循环在 9 处。内循环将检查它是否能被 2 整除,因为它不能被 2 整除,所以它会打印出数字并再次继续。下一次迭代将检查它是否能被 3 整除,因为它会被打破。

并尝试使用函数来检查数字是否为素数,这使其更加模块化。这是一个稍微优化的版本......

bool prime(int num)
{
    int root = sqrt(num);
    if(num == 2)
        return true;
    if(num%2 == 0)
        return false;
    for(int i=3;i<=root+1;i=i+2)
        if(num % i == 0)
           return false;
    return true;
}

Your inner loop is not correct. Its not printing primes, just odd numbers. It checks whether a number is divisible by 2 during first iteration and an even number will always be divisble. So the inner loop always breaks out if the number is even but it prints the number if odd and continues doing so till the loop breaks or terminates.

Lets try to get this with an example, the outer loop is at 9. The inner loop will check if its divisble by 2, since its not it'll print out the number and continue again. Next iteration will check whether its divisible by 3, since it is it'll break out.

And try using a function to check whether a number is prime that makes it more modular. Here's a little optimized version...

bool prime(int num)
{
    int root = sqrt(num);
    if(num == 2)
        return true;
    if(num%2 == 0)
        return false;
    for(int i=3;i<=root+1;i=i+2)
        if(num % i == 0)
           return false;
    return true;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文