当前位置:网站首页>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;
}
边栏推荐
- Pit encountered in win11 pytorch GPU installation
- Binary tree problem solving (2)
- Leetcode- insert and sort the linked list
- Embedded-c language-8-character pointer array / large program implementation
- List of common bugs in software testing
- How to configure PostgreSQL 12.9 to allow remote connections
- How to modify data file path in DM database
- Mysql表insert中文变?号的问题解决办法
- How to write a client-side technical solution
- Markdown edit syntax
猜你喜欢

MySQL table insert Chinese change? Solution to the problem of No

Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy

Ten thousand volumes are known to all, and one page of a book is always relevant. TVP reading club will take you through the reading puzzle!

2022-003arts: recursive routine of binary tree

Landing guide for "prohibit using select * as query field list"

Deep understanding of lambda expressions

Record the bug of unity 2020.3.31f1 once

关于Steam 教育的知识整理

cs架构下抓包的几种方法

Lay the foundation for children's programming to become a basic discipline
随机推荐
Analyzing the hands-on building tutorial in children's programming
Change deepin to Alibaba image source
Ognl和EL表达式以及内存马的安全研究
6.30 year end summary, end of student age
Save the CDA from the disc to the computer
6.30年终小结,学生时代结束
Pytest learning ----- pytest Interface Association framework encapsulation of interface automation testing
win10 磁盘管理 压缩卷 无法启动问题
Thinkphp Kernel wo system source Commercial Open source multi - user + multi - Customer Service + SMS + email notification
My first experience of shadowless cloud computer
6月书讯 | 9本新书上市,阵容强大,闭眼入!
Pit encountered in win11 pytorch GPU installation
Go GC garbage collection notes (three color mark)
Cannot activate CONDA virtual environment in vscode
10 minute quick start UI automation ----- puppeter
Mysql database learning
The core idea of performance optimization, dry goods sharing
关于Steam 教育的知识整理
DC-1靶场搭建及渗透实战详细过程(DC靶场系列)
培养中小学生对教育机器人的热爱之心