当前位置:网站首页>小蓝做实验(统计质数个数)
小蓝做实验(统计质数个数)
2022-06-21 15:32:00 【iFulling】
【问题描述】
小蓝很喜欢科研,他最近做了一个实验得到了一批实验数据,一共是两百万个正整数。如果按照预期,所有的实验数据 x 都应该满足 1 0 7 ≤ x ≤ 1 0 8 10^7 ≤ x ≤ 10^8 107≤x≤108。但是做实验都会有一些误差,会导致出现一些预期外的数据,这种误差数据 y 的范围是 1 0 3 ≤ y ≤ 1 0 12 10^3 ≤ y ≤ 10^{12} 103≤y≤1012 。由于小蓝做实验很可靠,所以他所有的实验数据中99.99% 以上都是符合预期的。小蓝的所有实验数据都在 primes.txt 中,现在他想统计这两百万个正整数中有多少个是质数,你能告诉他吗?
【素材】
百度网盘链接: primes.txt
提取码: imfa
【解题】
这里用到了定理:如果一个数是合数,那么它的最小质因数肯定小于等于它的平方根。
【代码】
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class B {
public static void main(String[] args) throws IOException {
int count = 0;
BufferedReader reader = new BufferedReader(new FileReader("src/primes.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
long num = Long.parseLong(line);
if (check(num)) {
System.out.println(num);
count++;
}
}
reader.close();
System.out.println(count);
}
public static boolean check(double nums) {
/* * 判断是否是质数 * */
boolean ret = true;
for (int i = 2; i <= (int) (Math.pow(nums, 0.5) + 1); i++) {
if (nums % i == 0) {
ret = false;
break;
}
}
return ret;
}
}
边栏推荐
- Alibaba cloud energy consumption treasure will be released soon to help SMEs' green upgrading and participate in the carbon neutral trillion market
- Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures!
- Analysis on the scale and market structure of China's bill financing industry in 2020 [figure]
- QT - basic knowledge
- What is PDT
- Implementation of asynchronous request pool
- Indexes, constraints and views in Oracle Database
- Promotion guide for large enterprises: material preparation, PPT writing and on-site defense
- Telnet batch test (II): key codes for the implementation of Telnet batch test script
- 阶乘求和
猜你喜欢

Phantom star VR product details 31: escape from the secret room

2022 Hunan latest fire facility operator simulation test question bank and answers

Learn upward management and four questioning skills to get twice the result with half the effort

Gee Registration Guide

Apple was fined by Dutch regulators, totaling about RMB 180million

The application of RPC technology and its framework sekiro in crawler reverse, encrypting data is a shuttle!

Browser evaluation: a free, simple and magical super browser - xiangtian browser

GO语言-type关键字

New project template of punctual atom F103 based on firmware library

Metric win computer application
随机推荐
First Canary deployment with rancher
Finding minimum spanning tree by using union search set
Complete and detailed installation steps for kubeneter version 1.7
5700s layer 2 interface and layer 3 interface switching
100% troubleshooting and analysis of Alibaba cloud hard disk
Mysql5.7 add SSL authentication
Niuke - real exercise-01
Several common implementation methods of mock interface test
Small case of throttling function
GO语言-结构体
Alibaba cloud energy consumption treasure will be released soon to help SMEs' green upgrading and participate in the carbon neutral trillion market
2022 Hunan latest fire facility operator simulation test question bank and answers
[Yugong series] February 2022 wechat applet -app Subpackages and preloadrule of JSON configuration attribute
After the uproar, is the yuan universe "cool"?
Write static multi data source code and do scheduled tasks to realize database data synchronization
In 2021, China's deposit balance continued to grow, and the balance of RMB and foreign currency deposits reached a record high [figure]
What is PDT
Analysis of China's social financing scale and financing structure in 2021: RMB loans to the real economy account for more than 60%[figure]
通过编译内核的方式增加系统调用
What is SQL injection