筛选质数
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,a[1500000]={0},j,b=0;
cin>>n;
for(i=2;i<=sqrt(n);i++)//2至根号n筛选
{
if(a[i]==0)
{
for(j=i*2;j<=n;j=j+i)
{
a[j]=1;
}
}
}
for(i=2;i<=n;i++)
{
if(a[i]==0)b++;
}
cout<<b;
return 0;
}
分解质因子
输入2个整数A和B,把每个数分解质因子并输出。
输入格式
第一行2个正整数:A和B,范围在[2,1000000]。 B-A < 20000.
输出格式
A到B之间每个数的分解质因子表达式。
输入/输出例子1
输入:
10 16
输出:
2 5
11
2 2 3
13
2 7
3 5
2 2 2 2
#include<bits/stdc++.h>
using namespace std;
int n,m;
int main(){
cin>>n>>m;
for(int i=n;i<=m;i++)
{
int x=i;
for(int j=2;x!=1;j++)
{
if(j>sqrt(x))
{
cout<<x;
break;
}
int y=0;
for(int d=2;d<j;d++)
{
if(j%d==0)
{
y++;
break;
}
}
if(y!=0) continue;
if(x%j==0)
{
cout<<j<<' ';
x/=j;
j--;
}
}
cout<<endl;
}
return 0;
}