素数 - 100以内的素数

需求

        求100以内的素数

定义

        素数或质数(prime number)是指只能被1和其自身整除,且大于1的自然数。

工具

        language : C++

        IDE : Microsoft Visual Studio Community 2019

实现

方法一:

// 100以内的素数,数量:25个
#include <iostream>
using namespace std;

int main() {
    int i, j;
    int count = 0;
    cout << "100以内的素数:" << endl;
    for (i = 2; i < 101; i++) {
        for (j = 2; j < i; j++) {
            if (i % j == 0) {
                break;
            }
        }
        if (j >= i) {
            cout << " " << i;
            count++;
        }
    }
    cout << "count: " << count << endl;
    return 0;
}

运行结果: 

100以内的素数:
    2    3    5    7    11    13    17    19    23    29    31    37    41    43    47    53    59    61    67    71    73    79    83    89    97
count: 25
D : \visual studio 2019\test\debug\test.exe(进程 6716)已退出,代码为 0。
按任意键关闭此窗口. . . 

方法二:(减少方法一内层循环次数)

// 100以内的素数,数量:25个
#include <iostream>
#include <cmath> // sqrt
using namespace std;

int main() {
    int i, j;
    int root;
    int count = 0;
    cout << "100以内的素数:" << endl;
    for (i = 2; i < 101; i++) {
        root = sqrt(i);
        for (j = 2; j < root + 1; j++) {
            if (i % j == 0) {
                break;
            }
        }
        if (j > root) {
            cout << " " << i;
            count++;
        }
    }
    cout << "\ncount: " << count;
    return 0;
}

运行结果: 

100以内的素数:
    2    3    5    7    11    13    17    19    23    29    31    37    41    43    47    53    59    61    67    71    73    79    83    89    97
count: 25
D : \visual studio 2019\test\debug\test.exe(进程 4260)已退出,代码为 0。
按任意键关闭此窗口. . . 

 方法三:(与方法二相同)

// 100以内的素数,数量:25个
#include <iostream>
#include <cmath> // sqrt
using namespace std;

int main() {
    int i, j;
    int count = 0;
    cout << "100以内的素数:" << endl;
    for (i = 2; i < 101; i++) {
        for (j = 2; j < (int)sqrt(i) + 1; j++) {
            if (i % j == 0) {
                break;
            }
        }
        if (j > (int)sqrt(i)) {
            cout << " " << i;
            count++;
        }
    }
    cout << "\ncount: " << count;
    return 0;
}

运行结果:

100以内的素数:
    2    3    5    7    11    13    17    19    23    29    31    37    41    43    47    53    59    61    67    71    73    79    83    89    97
count: 25
D : \visual studio 2019\test\debug\test.exe(进程 9320)已退出,代码为 0。
按任意键关闭此窗口. . . 

方法四:(与方法二、方法三基本相同,只是改变了素数判断条件)

// 100以内的素数,数量:25个
#include <iostream>
#include <cmath> // sqrt
using namespace std;

int main() {
    bool isPrimeNumber;
    int count = 0;
    cout << "100以内的素数:" << endl;
    for (int i = 2; i < 101; i++) {
        isPrimeNumber = true;
        for (int j = 2; j < (int)sqrt(i) + 1; j++) {
            if (i % j == 0) {
                isPrimeNumber = false;
                break;
            }
        }
        if (isPrimeNumber) {
            cout << " " << i;
            count++;
        }
    }
    cout << "\ncount: " << count;
    return 0;
}

运行结果:

100以内的素数:
    2    3    5    7    11    13    17    19    23    29    31    37    41    43    47    53    59    61    67    71    73    79    83    89    97
count: 25
D : \visual studio 2019\test\debug\test.exe(进程 8388)已退出,代码为 0。
按任意键关闭此窗口. . . 

方法五:(在优快云上看别人用Java写的,我改了一下,不过找了半天没找到原文,好像叫“笑而不语XX法”)

// 100以内的素数,数量:25个
#include <iostream>
using namespace std;

int main() {
    int count = 0;
    cout << "100以内的素数:" << endl;
    for (int i = 2; i < 101; i++) {
        if (i == 2 || i == 3 || i == 5 || i==7) {
            cout << " " << i;
            count++;
        }
        else if (i % 2 != 0 && i % 3 != 0 && i % 5 != 0 && i % 7 != 0) {
            cout << " " << i;
            count++;
        }
    }
    cout << "\ncount: " << count;
    return 0;
}

运行结果:

100以内的素数:
    2    3    5    7    11    13    17    19    23    29    31    37    41    43    47    53    59    61    67    71    73    79    83    89    97
count: 25
D : \visual studio 2019\test\debug\test.exe(进程 8848)已退出,代码为 0。
按任意键关闭此窗口. . . 

方法六:(出处:C++从入门到项目实践(超值版))

// 100以内的素数,数量:25个
#include<iostream>
#include<iomanip> // setw
using namespace std;
const int n = 100;
int main() {
    int a[n];
    int i, j;
    for (i = 0; i < n; i++) a[i] = 1 + i; // 用数组保存整数1-100
    a[0] = 0; // 1不是素数,置0
    for (i = 1; i < n; i++) {
        if (a[i] == 0)  continue; // 该数已经置0,判断下一个数
        for (j = i + 1; j < n; j++) if (a[j] % a[i] == 0)  a[j] = 0; // 是a[i]倍数的元素置0;
    }
    int count = 0;
    cout << "1-" << n << "之间的素数:" << endl;
    for (i = 0; i < n; i++) // 输出所有素数
        if (a[i] != 0) { // 非0元素即为素数
            cout << setw(6) << a[i];
            count++;
            if (count % 5 == 0)  cout << endl; // 每行5个数据
        }
    return 0;
}

运行结果:

1 - 100之间的素数:
          2          3          5          7        11
        13        17        19        23        29
        31        37        41        43        47
        53        59        61        67        71
        73        79        83        89        97

D:\visual studio 2019\test\Debug\test.exe(进程 9072)已退出,代码为 0。
按任意键关闭此窗口. . .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值