C-质数因子

题目描述

A prime number[质数] is a positive integer that has exactly two different positive divisors,1 and itself. For example, 2,3,5,7,11, and 13 are prime numbers. Write a C program to calculate the prime divisors[质因子] of the number n.

输入

The input will consist of several numbers, separated by a space. The first number represents the total number m(1<m<100) of the non-negative integers to process. The following numbers represents even integers ni(2<= ni <=10000000).

输出

Output the prime divisors.There are m results. Display one result per line.

样例输入

2 2 100

样例输出

2=2
100=2*2*5*5

在找一个数的质数因子过程中,其实就是在寻找他的因子过程。我们可以对它先找第一个整除数,对它相除然后赋值,比如15/3=5,25/5=5,100/2=50,然后继续寻找下一个整除数。有人可能会疑问不用考虑非质数的问题吗,其实是不用考虑的。因为每个非质数都可以被质数相除。直到寻找到最后一个质因子时,也就是本身,相除为1结束循环

#include<stdio.h>
int prime_divisors(int n)
{
    for(int i=2;i<100000;i++)//找质数因子
    {
        if(n%i==0)
        {
            n=n/i;
            printf("%d",i);
            if(n!=1)
            {
                printf("*");
            }
            else
            {
                return 0;
            }
            return prime_divisors(n);
        }
    }
}

int main()
{
    int n,num;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d",&num);
        printf("%d=",num);
        prime_divisors(num);
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值