利用数组下标对应指定范围内的质数,利用数组布尔类型值标志是否为质数

 1 package com.jdk7.chapter4;
 2
 3 public class PrimeNumber {
 4     public void getPrime(int range){
 5         boolean[] sourceData = this.isPrime(range);
 6
 7         if(!(sourceData==null)){
 8             int size = sourceData.length;
 9             Integer[] resultData = new Integer[size];
10             //number定义要放在自增运算循环外,否则可能达不到预期效果
11             int number = 0;
12             for(int i=1;i<size;i++){
13                 if(sourceData[i]){
14                     resultData[i] = i;
15                     System.out.print("素数["+i+"] :"+resultData[i].toString()+" ");
16                     if(++number % 10 == 0){
17                         System.out.println();
18                     }
19                 }
20             }
21             System.out.println();
22             System.out.println("质数总个数为: "+number);
23         }
24     }
25
26     private boolean[] isPrime(int range){
27         boolean[] tag = new boolean[range+1];
28         if(range<=0){return null;}
29         for(int i=1;i<range+1;i++){
30             if(i==1){
31                 tag[i] = false;
32             }else{
33                 tag[i] = true;
34                 }
35         }
36         //假设tag[i]为质数,则质数的倍数一定为非质数
37         int sqrt = (int)Math.sqrt(range);
38         for(int i=1;i<=sqrt;i++){
39             if(tag[i]){
40                 for(int j=(2 * i);j<=range;j+=i){
41                     tag[j] = false;
42                 }
43             }
44         }
45         return tag;
46     }
47 }
 1 package com.jdk7.chapter4;
 2
 3 public class PrimeNumberTest {
 4     public static void main(String[] args) {
 5         PrimeNumber pn = new PrimeNumber();
 6         int range = 100;
 7         pn.getPrime(range);
 8     }
 9 }
10
11 执行结果:
12 素数[2] :2 素数[3] :3 素数[5] :5 素数[7] :7 素数[11] :11 素数[13] :13 素数[17] :17 素数[19] :19 素数[23] :23 素数[29] :29
13 素数[31] :31 素数[37] :37 素数[41] :41 素数[43] :43 素数[47] :47 素数[53] :53 素数[59] :59 素数[61] :61 素数[67] :67 素数[71] :71
14 素数[73] :73 素数[79] :79 素数[83] :83 素数[89] :89 素数[97] :97
15 质数总个数为: 25

原文地址:https://www.cnblogs.com/celine/p/8428747.html

时间: 2024-07-31 22:45:30

利用数组下标对应指定范围内的质数,利用数组布尔类型值标志是否为质数的相关文章

程序员面试题目总结--数组(三)【旋转数组的最小数字、旋转数组中查找指定数、两个排序数组所有元素中间值、数组中重复次数最多的数、数组中出现次数超过一半的数】

转!http://blog.csdn.net/dabusideqiang/article/details/38271661 11.求旋转数组的最小数字 题目:输入一个排好序的数组的一个旋转,输出旋转数组的最小元素. 分析:数组的旋转:把一个数组最开始的若干个元素搬到数组的末尾.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1.这道题最直观的解法并不难.从头到尾遍历数组一次,就能找出最小的元素,时间复杂度显然是O(N).但这个思路没有利用输入数组

从一个数组中随机取出一定数量元素组成新数组

/** * 从一个数组中随机取出一定数量元素组成新数组 * @param array 一个String类型的数组 * @param number需要取出元素的数量 * @return 一个随机的数组 * @throws NullPointerException原数组不能为空 *@throws ArrayIndexOutOfBoundsException新数组长度应不大于原数组的长度 */ public static String[]  getRandomArray(String[] array,

LeetCode:Range Sum Query - Immutable - 数组指定区间内的元素和

1.题目名称 Range Sum Query(数组指定区间内的元素和) 2.题目地址 https://leetcode.com/problems/range-sum-query-immutable/ 3.题目内容 英文:Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 中文:给定一个数组nums,求出索引i和j之间元素的和,i一定是小于或等于j

利用快慢下标操作字符串数组

1.去除掉多余的空格: e.g. Before:   Life___is__short___I___use___python_'\0'(下划线为空格) After:  Life_is_short_I_use_python'\0' (去除掉多余的空格) 去掉空格比较简单,我们可以通过逐个判断字符,如果有连续的空格就使数组左移直到只剩一个空格,可是这么做效率十分低下; 前面学习过利用快慢下标的方法实现快排思想,这里也可以采用类似的方法 : 对于删除后的数组,长度将减少或者不变,则指向删除后数组的下标

js数组移除指定对象或下标i

非常常用的一段代码 1 //数组移除指定对象或下标i 2 Array.prototype.remove = function (obj) { 3 for (var i = 0; i < this.length; i++) { 4 var temp = this[i]; 5 if (!isNaN(obj)) { 6 temp = i; 7 } 8 if (temp == obj) { 9 for (var j = i; j < this.length; j++) { 10 this[j] = t

学underscore在数组中查找指定元素

前言 在开发中,我们经常会遇到在数组中查找指定元素的需求,可能大家觉得这个需求过于简单,然而如何优雅的去实现一个 findIndex 和 findLastIndex.indexOf 和 lastIndexOf 方法却是很少人去思考的.本文就带着大家一起参考着 underscore 去实现这些方法. 在实现前,先看看 ES6 的 findIndex 方法,让大家了解 findIndex 的使用方法. findIndex ES6 对数组新增了 findIndex 方法,它会返回数组中满足提供的函数的

ES6数组中删除指定元素

知识点: ES6从数组中删除指定元素 findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引.否则返回-1. arr.splice(arr.findIndex(item => item.id === data.id), 1) http://louiszhai.github.io/2017/04/28/array/ 1:js中的splice方法 splice(index,len,[item]) 注释:该方法会改变原始数组. splice有3个参数,它也可以用来替换/删除/添加数组

JavaScript 内置对象 Array 数组

Array 数组 1.创建数组 构造函数 new Array() 小括号内可以是数组元素个数,也可以是数组项 数组字面量表示法 var arr = [1,2,3,4,5]; 2.数组长度 arr.length #####3.数组方法 arr.push(n1,n2,...,nn); 顺序添加元素到数组尾部,返回新添加数组后的长度 arr.unshift(n1,n2,...,nn); 顺序添加元素到数组开头,返回新添加数组后的长度 arr.pop(); 删除数组最后一个元素,返回被删的元素 arr.

重载二维数组下标&quot;[][]&quot;

问题来源:(待续) 解决办法: (1) 可变长数组 对于可变的二维数组下标重载,在数组初始化时指定维数,之后可以像一般的二维数组进行赋值和取值操作. 当然,使用模板template来实现更好. 1 class Array2d{ 2 private: 3 int* a; 4 int nrow; 5 int ncol; 6 public: 7 Array2d(const int nrow_, const int ncol_) 8 :nrow(nrow_), ncol(ncol_) 9 { 10 a