Prime playing table entry

The idea is:
to create a deposit primes array A and determine the number is not a prime number array B,
first array B default setting for all prime, then the cycle starts at 2, to determine if the number is a prime number, then it will be his It becomes not a multiple of all primes (with factor does not prime number) this number is then stored in the array a, the cycle continues

This code is the output of all the primes less than 100

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
//素数打表
//先创建一个数组用来存素数,再一个数组用来判断这个值是不是素数
int sushu[100];
bool is_sushu[100];
//写一个方法
int num=0;
void judge_sushu(){
    int i,j;
    //先初始化全都是素数
    memset(is_sushu,true,sizeof(is_sushu));
    //然后从2开始
    for(i=2;i<100;i++){
        if(is_sushu[i]){
            //如果他是素数,那就把他存在素数数组中,然后把他的倍数全都变成不是素数
            sushu[num++]=i;
            for(j=2;j*i<100;j++)
                is_sushu[i*j]=false;
        }
    }
    //再把0和1设置为不是素数
    is_sushu[0]=false;
    is_sushu[1]=false;

}
int main(){
    //输出一定范围内的所有素数
    int i;
    judge_sushu();
   // len=sizeof(sushu)/sizeof(int);
    for(i=0;i<num;i++)
        printf("%d ",sushu[i]);
}

Published 72 original articles · won praise 5 · Views 2791

Guess you like

Origin blog.csdn.net/qq_41115379/article/details/105027012