PAT 2-07 Factor decomposition (C language Implementation)

Source: Internet
Author: User

PAT 2-07 Factor decomposition (C language Implementation) 

PAT 2-07 Factor decomposition (C language implementation), the need for friends can refer to the next.


Title Description:

Given a positive integer n, the decomposition result of the element factor is given, that is, its factorization expression n = p1^k1* p2^k2*...*pm^km.

Input Format Description:

Enter a positive integer n in the range of long Int.

Output Format Description:

The element factorization expression of n is output in the given format, i.e. n = p1^k1* p2^k2*...*pm^km, where Pi is a factor and requires a small to large output, the exponent Ki is the number of pi, and when the ki==1 is only one of the factor pi, it does not output ki.

Sample input and output:

Serial number Input Output
1
1024
1024=2^10
2
1323
1323=3^3*7^2
3
97532468
97532468=2^2*11*17*101*1291
4
1
1=1
5
3
3=3
Solution Description:

From small to large to look for the element factor, and the number of each element of the factor to do statistics.

Source:

#include <stdio.h> #include <math.h>int main (void) {int i,j;   Long int m, N;int count;int isprime,flag;isprime = 1;flag = 0;scanf ("%ld", &n);p rintf ("%ld=", n); m = sqrt (n); for (i = 2; I <= m;i++) {if (n%i = = 0) {//printf ("%d", i); isprime = 0;count = 1;n = N/i;while (n%i = = 0) {n = n/i;count++;} if (flag) printf ("*"); Elseflag = 1;if (count = = 1) printf ("%d", I), elseprintf ("%d^%d", I,count);}} if (isprime) printf ("%d", n); return 0;} 

PAT 2-07 Factor decomposition (C language Implementation)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.