从1到100打印的质数质数

2023-09-10 22:36:17 作者:男孩被女老师轮流布置作业

本C ++ code打印出下列素数: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

但我不认为这是我的书希望它被编写方式。它提到一些有关的平方根。所以,我没有尝试改变我的第二个循环的(INT J = 2; J<开方(我); J ++),但它并没有给我我需要的结果

我怎么会需要更改此code到我的书,希望它成为的方式?

  INT主要()
{
    的for(int i = 2; I< 100;我++)
        对于(INT J = 2; J<我; J ++)
        {
            如果(我引用%j == 0)
                打破;
            否则,如果(ⅰ== J + 1)
                COUT<< I<< ;

        }
    返回0;
}
 

  编程 打印出100到200间的素数 质数

一个质整数是一个有   正是两种不同的除数,   即1和号码本身。写,   运行和测试一个C ++程序,   查找并打印的所有质数   小于100(提示:1是一个素数   数。为2至100的每个号码,   发现余数=%n,其中n   从2至范围的sqrt(号码)。 \如果n   大于的sqrt(编号),该   号码不是由n个相等整除。   为什么?如果任何剩余等于0,则   数不为素数。)

解决方案

三种方式:

1。

  INT主要()
{
    的for(int i = 2; I< 100;我++)
        对于(INT J = 2;Ĵ* J< = I,J ++)
        {
            如果(我引用%j == 0)
                打破;
            否则,如果第(j + 1>的sqrt(i))的{
                COUT<< I<< ;

            }

        }

    返回0;
}
 

2。

  INT主要()
{
    的for(int i = 2; I< 100;我++)
    {
        布尔素= TRUE;
        对于(INT J = 2;Ĵ* J< = I,J ++)
        {
            如果(我引用%j == 0)
            {
                素= FALSE;
                打破;
            }
        }
        如果(总理)COUT<< I<< ;
    }
    返回0;
}
 

3。

 的#include<载体>
诠释的main()
{
    的std ::矢量< INT>素数;
    primes.push_back(2);
    的for(int i = 3; I< 100;我++)
    {
        布尔素= TRUE;
        对于(INT J = 0; J< primes.size()及和放大器;素数研究[J] *素数研究[J] LT; = I; J ++)
        {
            如果(I%的素数研究[J] == 0)
            {
                素= FALSE;
                打破;
            }
        }
        如果(素)
        {
            primes.push_back(ⅰ);
            COUT<< I<< ;
        }
    }

    返回0;
}
 

编辑:在第三个例子,我们追踪所有的previously素数计算的。如果一个数整除的非质数,也有一些素&其中; =该除数它也是divisble通过。本所primes_in_range / total_range的因素减少了计算。

This c++ code prints out the following prime numbers: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97.

But I don't think that's the way my book wants it to be written. It mentions something about square root of a number. So I did try changing my 2nd loop to for (int j=2; j<sqrt(i); j++) but it did not give me the result I needed.

How would I need to change this code to the way my book wants it to be?

int main () 
{
    for (int i=2; i<100; i++) 
        for (int j=2; j<i; j++)
        {
            if (i % j == 0) 
                break;
            else if (i == j+1)
                cout << i << " ";

        }   
    return 0;
}

A prime integer number is one that has exactly two different divisors, namely 1 and the number itself. Write, run, and test a C++ program that finds and prints all the prime numbers less than 100. (Hint: 1 is a prime number. For each number from 2 to 100, find Remainder = Number % n, where n ranges from 2 to sqrt(number). \ If n is greater than sqrt(number), the number is not equally divisible by n. Why? If any Remainder equals 0, the number is no a prime number.)

解决方案

Three ways:

1.

int main () 
{
    for (int i=2; i<100; i++) 
        for (int j=2; j*j<=i; j++)
        {
            if (i % j == 0) 
                break;
            else if (j+1 > sqrt(i)) {
                cout << i << " ";

            }

        }   

    return 0;
}

2.

int main () 
{
    for (int i=2; i<100; i++) 
    {
        bool prime=true;
        for (int j=2; j*j<=i; j++)
        {
            if (i % j == 0) 
            {
                prime=false;
                break;    
            }
        }   
        if(prime) cout << i << " ";
    }
    return 0;
}

3.

#include <vector>
int main()
{
    std::vector<int> primes;
    primes.push_back(2);
    for(int i=3; i < 100; i++)
    {
        bool prime=true;
        for(int j=0;j<primes.size() && primes[j]*primes[j] <= i;j++)
        {
            if(i % primes[j] == 0)
            {
                prime=false;
                break;
            }
        }
        if(prime) 
        {
            primes.push_back(i);
            cout << i << " ";
        }
    }

    return 0;
}

Edit: In the third example, we keep track of all of our previously calculated primes. If a number is divisible by a non-prime number, there is also some prime <= that divisor which it is also divisble by. This reduces computation by a factor of primes_in_range/total_range.