método de tamizado java / c_ (método de tamiz Essier) para generar una secuencia de números primos

Directorio de artículos

Java

import java.util.ArrayList;
import java.util.Scanner;

/*
 * @Description: 
 * @Version: 2.0
 * @Author: xuchaoxin
 * @Date: 2021-03-14 21:44:28
 * @LastEditors: xuchaoxin
 * @LastEditTime: 2021-03-14 21:57:50
 */
public class FilterPrime {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner=new Scanner(System.in);
        /*输入素数的上界*/
        int n=scanner.nextInt();
        scanner.close();
        ArrayList<Integer> arrayList = new ArrayList<>();
        /* 初始化序列 */
        for (int i = 2; i < n; i++) {
    
    
            arrayList.add(i);
        }

        int tempPrime;//保存素数
        /*开始筛选,初步估计需要二重循环
        * 那么要扫描多少次?
        * 停止扫描的条件是什么?
        * 用while是否更方便?*/
        for (int i = 0; i < arrayList.size(); ) {
    
    
            tempPrime = arrayList.get(0);
            System.out.println(tempPrime);
            /*注意这里你使用了remove方法*/
            /*事实上,当你发现了打印的元素个数不够时,就应该看看打印语句位于哪里,知道去哪里排查问题
            * 而不是凭感觉找个地方分析,这往往是我不的不要的习惯*/
            for (int j = 0; j < arrayList.size(); j++) {
    
    

                if (arrayList.get(j) % tempPrime == 0) {
    
    
                    arrayList.remove(j);
                }
            }
        }
    }
}

C

#include<stdio.h>
#include<stdlib.h>
int main() {
    
    
	//另一种策略时数据集抽取,来带到类似产出被筛选的效果,比较消耗空间,但是节省时间
	//这里的策略是标记法,比较节省空间,但是耗时间
	int n;
	printf("input the integer you upper bound you want to print(calculate):\n");
	scanf_s("%d", &n);
	printf("invoke the malloc function to apply for memory to store integers:\n\
		\nin different system may have different sequnce: \n");
	int* a = (int*)malloc(n * sizeof(int));//remember to free the memory;
	/*if malloc lead to a err ,try use a big enough array to instead.*/
	int i1 = 2;//元素序列
	int i2 = 0;//索引序列
	/*初始换序列2,3,4,5...*/
	for (i2 = 0, i1 = 2; i1 < n; ) {
    
    
		a[i2++] = i1++;
	}
	/*start to calculate the result*/
	//int j = 2/*head element(prime)*/
	//	, k = 2;/*for traverse*/
	//int bi = 0;
	int tempPrime=0;
	int count = 0;//record how many words have been judged(include nonPrime and konwnPrime)
	int index1 = 0;
	int index2 = 0;
	int index3 = 0;
	int line_break = 1;//to help line break counter
	for (index1 = 0; count < n; index1++) {
    
    
		/*扫描第一个非零元素(也就是获取序列的第一个元素(素数))*/
		/*打印这个素数*/
		for (index2 = 0; index2 < n; index2++) {
    
    
			if (a[index2]) {
    
    
				tempPrime = a[index2];
				break;
			}
		}
		printf("%-8d\t", tempPrime);//default center right(use '-' to center left)
		if (line_break++ % 10 == 0) {
    
    
			printf("\n");
		}
		//tempPrime = a[j];

		for (index3 = 0; index3 < n; index3++) {
    
    
			if (a[index3] != 0) {
    
    
				if (a[index3] % tempPrime == 0) {
    
    
					a[index3] = 0;
					count++;
				}
			}
		}
	

	}
	free(a);

}

Supongo que te gusta

Origin blog.csdn.net/xuchaoxin1375/article/details/114819279
Recomendado
Clasificación