当前位置:网站首页>Acwing 866. determining prime numbers by trial division
Acwing 866. determining prime numbers by trial division
2022-07-25 21:57:00 【_ Liu Xiaoyu】
Given n A positive integer ai, Determine whether each number is a prime number .
Input format
The first line contains integers n.
Next n That's ok , Each line contains a positive integer ai.
Output format
common n That's ok , Among them the first i Line output the i A positive integer ai Is it a prime number , Yes, output Yes, Otherwise output No.
Data range
1≤n≤100,
1≤ai≤231−1
sample input :
2
2
6
sample output :
Yes
No
Method 1: Direct violence enumeration O(n2)
But higher requirements will lead to overtime
bool is_prime(int n)
{
if(n < 2) return false;
for(int i=2; i <= n; i++)
if(n % i == 0)
return false;
return true;
}
Optimization idea : You only need to traverse half
because d|n when , n/d|n It's OK ; among d Representation factor ,n It means a number , | To divide or divide
Put it in order : d <= n/d that will do ; have to d * d <= n that will do ,
so , When looking for factors , Just traverse the root n Part can be
Method 2: Use it directly sqrt function
bool is_prime(int n){
if(n < 2) return false; //2 Is the smallest prime number , If n Less than 2, that n It must not be a prime number
for(int i = 2;i <= sqrt(n);i ++){
// Optimization part
if(n % i == 0){
// If it can be i to be divisible by , This number is not prime
return false; // Return no
}
}
return true; // Return is
}
however , This sqrt The bottom layer of the function runs very slowly , Every time this line of code is executed , Need to calculate once , Let's look at other methods
Method 3:
Optimize the traversal code to for(int i = 2;i * i <= n;i ++), Basically, it is similar to the above , But there may be a risk of overflow , Not recommended
Method 4:
Optimize the traversal code to for(int i=2; i <= n / i; i++), The principle is the same as above , But there is no risk of data overflow , This is recommended
Recommended method complete code
#include <iostream>
using namespace std;
// Trial division
// take O(n) Down to O(sqrt(n))
bool is_prime(int n)
{
if(n < 2) return false;
// for(int i=2; i * i <= n; i++) // There may be spillover risk
// for(int i=2; i <= sqrt(n); i++) // sqrt Every time the function calculates , Affect efficiency
for(int i=2; i <= n / i; i++) // Just judge the smaller divisor of the two
if(n % i == 0)
return false;
return true;
}
int main()
{
int m;
cin >> m;
while(m --)
{
int c;
cin >> c;
if(is_prime(c)) puts("Yes");
else puts("No");
}
return 0;
}
边栏推荐
- 2022 love analysis ― bank digitalization practice report
- 【面试:并发篇24:多线程:综合练习】顺序控制
- [Flink] flick rocksdbliststate reports an error you cannot add null to a liststate
- 2022 the latest software tests eight part essay. Whether you can offer depends on how you recite it
- [leetcode ladder] linked list · 876 find the middle node of the linked list
- 立创EDA——器件的创建01-电阻(二)
- Tesseract OCR初探
- 如何快速搭建图片服务器[通俗易懂]
- 【汇编语言01】基础知识
- [hand torn STL] unordered_ set、unordered_ Map (encapsulated with hash table)
猜你喜欢

信息安全建设原则指导
![[redis underlying parsing] linked list type](/img/e8/c192629dce1a958155a562d216d532.png)
[redis underlying parsing] linked list type
![[Flink] flick rocksdbliststate reports an error you cannot add null to a liststate](/img/c0/1923e17f166ab4bc7d20f48398f366.jpg)
[Flink] flick rocksdbliststate reports an error you cannot add null to a liststate
![[leetcode ladder] linked list · 876 find the middle node of the linked list](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[leetcode ladder] linked list · 876 find the middle node of the linked list

【饭谈】如何设计好一款测试平台?

Lichuang EDA -- creation of devices 01 resistance (II)

Basic knowledge in the project

Create EDA - why should I learn EDA

919. 完全二叉树插入器 : 简单 BFS 运用题

Share | intelligent fire emergency management platform solution (PDF attached)
随机推荐
再次来光顾
C common set
Idea resolves the prompt of profile properties disappear
YUV420 yuv420sp image format "recommended collection"
【饭谈】Web3.0到来后,测试人员该何去何从?(十条预言和建议)
自动化测试岗花20K招人,到最后居然没一个合适的,招两个应届生都比他们强吧
狗粮的成分
ORIGYN基金会正式启动$OGY Staking,引领新一轮生态利好
[test development methodology] experience of test development platform PK - choice
The file cannot be saved (what if the folder is damaged and cannot be read)
Six principles of C program design
[51nod1676 undirected graph isomorphism] undirected graph hash [easy to understand]
关于接口测试你想知道的都在这儿了
Web3 entrepreneurship has all the elements of explosive growth of innovation
Create files, file permissions, ownership, and sticky bits
c sqlite ... ...
redis主从架构锁失效问题(主从)
Redis 使用详解
dovecot 设置邮箱quota
Guys, how can Flink SQL submit tasks in per job mode?