当前位置:网站首页>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;
}
边栏推荐
- Cultivate primary and secondary school students' love for educational robots
- Idea autoguide package and autodelete package Settings
- Solution of DM database unable to open graphical interface
- Learn AI safety monitoring project from zero [attach detailed code]
- Thinkphp內核工單系統源碼商業開源版 多用戶+多客服+短信+郵件通知
- C# 图片显示占用问题
- Tawang food industry insight | current situation, consumption data and trend analysis of domestic infant complementary food market
- Analyzing the hands-on building tutorial in children's programming
- How do I interview for a successful software testing position? If you want to get a high salary, you must see the offer
- oracle 存储过程与job任务设置
猜你喜欢
Idea automatic package import and automatic package deletion settings
Unit testing classic three questions: what, why, and how?
MySQL table insert Chinese change? Solution to the problem of No
[understand one article] FD_ Use of set
idea自動導包和自動删包設置
Learn what definitelytyped is through the typescript development environment of SAP ui5
Acelems Expressway microgrid energy efficiency management platform and intelligent lighting solution intelligent lighting tunnel
C# 基于MQTTNet的服务端与客户端通信案例
奠定少儿编程成为基础学科的原理
Application of intelligent robot in agricultural ecology
随机推荐
C language practice - number guessing game
Precipitate yourself and stay up late to sort out 100 knowledge points of interface testing professional literacy
oracle 存储过程与job任务设置
Steam教育的实际问题解决能力
Solution: the agent throws an exception error
Use of Baidu map
Design and implementation of general interface open platform - (44) log processing of API services
DJB Hash
Starting from the classification of database, I understand the map database
汇编语言中的标志位:CF、PF、AF、ZF、SF、TF、IF、DF、OF
idea自動導包和自動删包設置
VMware installation win10 reports an error: operating system not found
TypeScript函数详解
Learn BeanShell before you dare to say you know JMeter
Ruby replaces gem Alibaba image
Keil compilation code of CY7C68013A
数学知识(欧拉函数)
June book news | 9 new books are listed, with a strong lineup and eyes closed!
win10 磁盘管理 压缩卷 无法启动问题
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification