素数显示并计算c ++

时间:2022-08-16 11:23:43

The code is working fine for displaying prime numbers, but I need them to be displayed and to also display the number of prime numbers.

代码在显示素数时工作正常,但我需要显示它们并显示素数的数量。

The code is for prime numbers in a selected interval.

该代码用于选定间隔中的素数。

Here's my code:

这是我的代码:

bool prastevila_interval(int z, int k) //primes in interval
{
    for (int i = z; i < k; ++i){
        for (int j = 2; j <= i; j++){
            if (!(i % j) && (i != j))
                break;
            else if (i == j){
                cout<< i <<endl;
            }

        }
    }
return true;
}

//--------------------------------------------------------------------
int main() { 

                //Primes in interval
    cout<<"__________PRASTEVILA V INTERVALU__________"<<endl;

    cout << "Zacetek intervala : ";
    cin >> z;                          //starting point of interval

    cout << "Konec intervala : ";
    cin >> k;                          //ending point of interval

    cout<<"Prastevila v intervalu so: "<<endl;

    prastevila_interval(z, k) == true;

    cout <<"Stevilo prastevil na interval od "<< z 
         <<" do "<< k <<" je : "<< counter << endl;   
    // this counter is not working ---^
}

How to count the number of primes?

如何计算素数?

P.S. : I tried everything already and I just can't figure it out.

附: :我已经尝试了一切,但我无法理解。

5 个解决方案

#1


2  

You just need to update the counter on each prime number found. Since your method return always true, I suggest to change it like so:

您只需更新找到的每个素数的计数器。由于你的方法总是返回true,我建议改变它:

int prastevila_interval(int z, int k) //primes in interval
{
    int counter = 0;
    for (int i = z; i < k; ++i){
        for (int j = 2; j <= i; j++)
        {
            if (!(i % j) && (i != j))
                    break;
            else if (i == j)
            {
                    cout<< i <<endl;
                    counter++;
            }
         }      
     }
return counter;
}

and so:

cout <<"Stevilo prastevil na interval od "<< z 
     <<" do "<< k <<" je : "<< prastevila_interval(z,k) << endl; 

Note also, that this code:

另请注意,此代码:

prastevila_interval(z,k) == true;

doesn't do anything and generally meaningless. Your just check here whether the return value is true and don't do anything with it.

什么都不做,一般都没有意义。您只需在此处检查返回值是否为真且不对其执行任何操作。

#2


1  

Pass a pointer or a reference to the counter to your function, and have it update it.

将指针或对计数器的引用传递给您的函数,并让它更新它。

bool prastevila_interval(int z, int k, int& counter) //primes in interval
{
    counter = 0;
    for (int i=z; i<k; ++i){
        for (int j=2; j<=i; j++)
        {
            if (!(i%j)&&(i!=j))
                break;
            else if (i == j){
                cout<< i <<endl;
                ++counter;
            }

        }
    }
    return true;
}

Calling this will be like:

打电话给这将是:

int counter = 0;
prastevila_interval(lo, hi, counter);

Another way you can do this is by having prastevila_interval return an int instead of a bool, which is the number of primes printed.

另一种方法是让prastevila_interval返回一个int而不是bool,这是打印的素数。

#3


1  

You could modify your function to:

您可以将您的功能修改为:

int prastevila_interval(int z, int k) //primes in interval   
{
    int counter = 0;      

    for (int i = z; i < k; ++i){
        for (int j = 2; j <= i; j++){
            if (!(i % j) && (i != j))
                break;
            else if (i == j){
                counter++;
                cout<< i <<endl;
         }
    }
return counter;
}

and then call it in your main as:

然后在您的主电话中将其称为:

int prime_number = prastevila_interval(lower_bound, upper_bound);

cout << "Number of primes:" << prime_numbers << endl;

#4


1  

Just add a counter to your function and increment the counter with one each time you find a prime.

只需在您的函数中添加一个计数器,并在每次找到素数时将计数器增加一个。

bool prastevila_interval(int z, int k, int &counter) //primes in interval
{
    for (int i=z; i<k; ++i){
            for (int j=2; j<=i; j++)
        {
            if (!(i%j)&&(i!=j))
                break;
            else if (i == j)
            {
                counter++;
                cout<< i <<endl;
            }   
    }
    return true;
}

Initialize a counter in your main function and pass it to the function as usual.

在主函数中初始化一个计数器并像往常一样将其传递给函数。

#5


1  

You can try it like:

你可以这样尝试:

bool prime(int num)
{
    if (num == 2) return true;
    else
        for (int j=2; j<=sqrt(num); j++)
        {
            if ( num%j == 0)
                return false;
        }
    return true;
}

int prastevila_interval(int z, int k) //primes in interval
{
    int counter =0;
    for (int i=z; i<k; ++i)
    {
        if (prime(i))
        {
            cout<< i <<" ";
            counter++;
        }

    }
    cout<<endl;
    return counter;
}

int main()
{

    //Primes in interval
    cout<<"__________PRASTEVILA V INTERVALU__________"<<endl;
    cout<<"Zacetek intervala : ";
    cin>>z;                          //starting point of interval
    cout<<"Konec intervala : ";
    cin>>k;                          //ending point of interval
    cout<<"Prastevila v intervalu so: "<<endl;
    int counter = prastevila_interval(z,k);
    cout<<"Stevilo prastevil na interval od "<<z<<" do "<<k<<" je : "<<counter<<endl;

#1


2  

You just need to update the counter on each prime number found. Since your method return always true, I suggest to change it like so:

您只需更新找到的每个素数的计数器。由于你的方法总是返回true,我建议改变它:

int prastevila_interval(int z, int k) //primes in interval
{
    int counter = 0;
    for (int i = z; i < k; ++i){
        for (int j = 2; j <= i; j++)
        {
            if (!(i % j) && (i != j))
                    break;
            else if (i == j)
            {
                    cout<< i <<endl;
                    counter++;
            }
         }      
     }
return counter;
}

and so:

cout <<"Stevilo prastevil na interval od "<< z 
     <<" do "<< k <<" je : "<< prastevila_interval(z,k) << endl; 

Note also, that this code:

另请注意,此代码:

prastevila_interval(z,k) == true;

doesn't do anything and generally meaningless. Your just check here whether the return value is true and don't do anything with it.

什么都不做,一般都没有意义。您只需在此处检查返回值是否为真且不对其执行任何操作。

#2


1  

Pass a pointer or a reference to the counter to your function, and have it update it.

将指针或对计数器的引用传递给您的函数,并让它更新它。

bool prastevila_interval(int z, int k, int& counter) //primes in interval
{
    counter = 0;
    for (int i=z; i<k; ++i){
        for (int j=2; j<=i; j++)
        {
            if (!(i%j)&&(i!=j))
                break;
            else if (i == j){
                cout<< i <<endl;
                ++counter;
            }

        }
    }
    return true;
}

Calling this will be like:

打电话给这将是:

int counter = 0;
prastevila_interval(lo, hi, counter);

Another way you can do this is by having prastevila_interval return an int instead of a bool, which is the number of primes printed.

另一种方法是让prastevila_interval返回一个int而不是bool,这是打印的素数。

#3


1  

You could modify your function to:

您可以将您的功能修改为:

int prastevila_interval(int z, int k) //primes in interval   
{
    int counter = 0;      

    for (int i = z; i < k; ++i){
        for (int j = 2; j <= i; j++){
            if (!(i % j) && (i != j))
                break;
            else if (i == j){
                counter++;
                cout<< i <<endl;
         }
    }
return counter;
}

and then call it in your main as:

然后在您的主电话中将其称为:

int prime_number = prastevila_interval(lower_bound, upper_bound);

cout << "Number of primes:" << prime_numbers << endl;

#4


1  

Just add a counter to your function and increment the counter with one each time you find a prime.

只需在您的函数中添加一个计数器,并在每次找到素数时将计数器增加一个。

bool prastevila_interval(int z, int k, int &counter) //primes in interval
{
    for (int i=z; i<k; ++i){
            for (int j=2; j<=i; j++)
        {
            if (!(i%j)&&(i!=j))
                break;
            else if (i == j)
            {
                counter++;
                cout<< i <<endl;
            }   
    }
    return true;
}

Initialize a counter in your main function and pass it to the function as usual.

在主函数中初始化一个计数器并像往常一样将其传递给函数。

#5


1  

You can try it like:

你可以这样尝试:

bool prime(int num)
{
    if (num == 2) return true;
    else
        for (int j=2; j<=sqrt(num); j++)
        {
            if ( num%j == 0)
                return false;
        }
    return true;
}

int prastevila_interval(int z, int k) //primes in interval
{
    int counter =0;
    for (int i=z; i<k; ++i)
    {
        if (prime(i))
        {
            cout<< i <<" ";
            counter++;
        }

    }
    cout<<endl;
    return counter;
}

int main()
{

    //Primes in interval
    cout<<"__________PRASTEVILA V INTERVALU__________"<<endl;
    cout<<"Zacetek intervala : ";
    cin>>z;                          //starting point of interval
    cout<<"Konec intervala : ";
    cin>>k;                          //ending point of interval
    cout<<"Prastevila v intervalu so: "<<endl;
    int counter = prastevila_interval(z,k);
    cout<<"Stevilo prastevil na interval od "<<z<<" do "<<k<<" je : "<<counter<<endl;