分解质因数

Description
求出区间[a,b]中所有整数的质因数分解。
Input
输入两个整数a,b。
Output
每行输出一个数的分解,形如k=a1a2a3…(a1<=a2<=a3…,k也是从小到大的)(具体可看样例)
Sample Input
3 10
Sample Output
3=3
4=22
5=5
6=2
3
7=7
8=222
9=33
10=2
5
Hint
提示
先筛出所有素数,然后再分解。
数据规模和约定
2<=a<=b<=10000

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
const int N=1e6+10;
const double pai=4*atan(1);
int a[N];

int f(int x)
{
    int i,j=sqrt(x);
    if(x%2==0)
        return 2;
    for(int i=3;i<=j;i++)
        if(x%i==0)
        return i;
    return x;
}

int main()
{
    ios::sync_with_stdio(false);
    int a,b;
    while(cin>>a>>b)
    {
        for(int i=a;i<=b;i++)
            {
                int m=f(i);
                int k,t;
                k=i/m;
                cout<<i<<"="<<m;
                while(k>1)
                {
                    t=f(k);
                    k/=t;
                    cout<<"*"<<t;
                }
                cout<<endl;

            }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值