当前位置:网站首页>Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
Mathematical problems (number theory) trial division to judge prime numbers, decompose prime factors, and screen prime numbers
2022-07-02 04:52:00 【Wu Yu 4】
Prime number
In more than 1 In the integer of , Only 1 And itself , It's called prime number ( Prime number ).
1. The judgment of prime number ( Trial division )
int prime(long long n)
{
if(n<2) return 0;// Greater than required 1 The positive integer
else
{
for(int i=2;i<n;i++)// Go through it , The time complexity is appropriate O(n) Overtime
{
if(n%i==0) return 0;
}
}
return 1;
}Optimize it :
because n/d=x, meanwhile n/x=d It was also established , So we can conclude that divisors exist in pairs .
So we just need to enumerate the smaller one , namely d<=n/d, Rewrite the cycle condition as i<=n/i, In this way, the time complexity is O(sqrt(n)) 了 .
Topic link :866. Try division to determine the prime number - AcWing Question bank
Topic :

2. Decomposing prime factor ( Trial division )
Prime factor refers to the prime factor that can divide a given division .
【 If there are only two numbers 1 A common qualitative factor , Called coprime 】
【1 And any positive integer are mutually prime numbers 】
【 Any number can be regarded as the multiplication of multiple prime factors ,n=p1^k1*p2^k2*…*pn^kn】
link :AcWing 867. Decomposing prime factor - AcWing
Topic :

Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
for(int i=2;i<=n;i++)// The time complexity is O(n), Timeout
{
if(n%i==0)// What seems to be found is n The factor of , Not prime factor .
/* But there's no problem here , Because once the following is found, it can be divisible i,n Will enter the cycle until i Until you know you can't get rid of it . take 4 An example to , stay i=2 When n/=2 ——>n=2, also n/=2——>n=0. No, i=4 What's the matter , So divide all the cases where the factors are composite numbers , Guarantee that they are prime factors .*/
{
int s=0;
while(n%i==0)// Find out the index
{
n/=i;
s++;
}
cout<<i<<" "<<s<<endl;
}
}
cout<<endl;// The output has the requirement that each data should be wrapped
}
return 0;
}
Optimize it :
because n At most one of them is greater than sqrt(n) The quality factor of
So the cycle condition is changed to i<=n/i, At the same time, add a special judgment if(n>1) cout<<n<<" "<<1<<endl;
Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
cin>>t;
while(t--)
{
ll n;
cin>>n;
for(int i=2;i<=n/i;i++)// The time complexity is O(sqrt(n)), At most sqrt(n)
{
if(n%i==0)
{
int s=0;
while(n%i==0)
{
n/=i;
s++;
}
cout<<i<<" "<<s<<endl;
}
}
if(n>1) cout<<n<<" "<<1<<endl;
cout<<endl;
}
return 0;
}
3. Sieve prime number

Topic link :868. Sieve prime number - AcWing Question bank
Topic :

Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
int primes[N],cnt,n;
bool st[N];
int main()
{
cin>>n;
for(int i=2;i<=n;i++)
{
if(!st[i])// Traverse prime numbers from small to large , The smallest is 2
primes[cnt++]=i;
for(int j=i+i;j<=n;j+=i)// hold i All multiples of are deleted
st[j]=true;
}
cout<<cnt<<endl;
return 0;
}Time complexity calculation :
n/2+n/3+n/4+…+n/n
=n(1/2+1/3+1/4+…+1/n)
When n Towards positive infinity , The following is harmonic series , The value is ln n+c (c by 0.57)
Again because ln n<log2 n
So the time complexity is :O(nlog2 n)
Optimize it : Put the second cycle into if Go inside , Namely Ethmoidal method
Specific Ehrlich screen conveyor gate :
Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
int primes[N],cnt,n;
bool st[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=2;i<=n;i++)
{
if(!st[i])// Traverse prime numbers from small to large , The smallest is 2
{
primes[cnt++]=i;
for(int j=i+i;j<=n;j+=i)// hold i All multiples of are deleted
st[j]=true;
}
}
cout<<cnt<<endl;
return 0;
}

Both times AC, But the second sieve is five times faster than the first .
There is another kind. Linear sieve method :( The data range is 1e7 when It is twice as fast as Ehrlich sieve )
Code :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+10;
int primes[N],cnt,n;
bool st[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=2;i<=n;i++)
{
if(!st[i])// Traverse prime numbers from small to large , The smallest is 2
primes[cnt++]=i;
for(int j=0;primes[j]<=n/i;j++)
{
st[primes[j]*i]=true;
if(i%primes[j]==0) break;
}
}
cout<<cnt<<endl;
return 0;
}
边栏推荐
- Mouse events in JS
- Save the CDA from the disc to the computer
- AcrelEMS高速公路微电网能效管理平台与智能照明解决方案智慧点亮隧道
- UNET deployment based on deepstream
- Vmware安装win10报错:operating system not found
- Rhcsa --- work on the fourth day
- Ruby replaces gem Alibaba image
- [high speed bus] Introduction to jesd204b
- 面试会问的 Promise.all()
- Analyze the space occupied by the table according to segments, clusters and pages
猜你喜欢

Free drawing software recommended - draw io

Its appearance makes competitors tremble. Interpretation of Sony vision-s 02 products

How to modify data file path in DM database

Steam教育的实际问题解决能力

Realize the function of data uploading

Practical problem solving ability of steam Education

Knowledge arrangement about steam Education

Unit testing classic three questions: what, why, and how?

Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel

Let genuine SMS pressure measurement open source code
随机推荐
Lm09 Fisher inverse transform inversion mesh strategy
Cultivate primary and secondary school students' love for educational robots
Leetcode- insert and sort the linked list
June book news | 9 new books are listed, with a strong lineup and eyes closed!
A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
[Yu Yue education] autumn 2021 reference materials of Tongji University
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
解决:代理抛出异常错误
Win10 disk management compressed volume cannot be started
Promise all()
Mapping location after kotlin confusion
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
win10 磁盘管理 压缩卷 无法启动问题
数学问题(数论)试除法做质数的判断、分解质因数,筛质数
TypeScript函数详解
The solution to the complexity brought by lambda expression
2022-003arts: recursive routine of binary tree
idea自动导包和自动删包设置
Realize the function of data uploading
Embedded-c language-9-makefile/ structure / Consortium