Factor and factorial, factor factorial

Source: Internet
Author: User

Factor and factorial, factor factorial

Enter a positive integer n (2 <=n <= 100) and multiply the factorial n! = 1*2*3 * · * n is decomposed into the form of prime factor multiplication, and each prime number is output from small to large (2, 3, 5 ,···) index. For example, 825 = 3*5 ^ 2*11 should be expressed as (, 1 ), 0, 1, 2, 0, 1, 2, 3, 5, 7, and 11 respectively. Your program should ignore prime numbers that are larger than the maximum prime factor (otherwise there will be an infinite number of zeros at the end ).

Sample input:

5

53

Sample output:

5! = 3 1 1

53! = 49 23 12 8 4 4 3 2 2 1 1 1 1 1 1

Program:

# Include <iostream>

# Include <cstring>

Using namespace std;

// Determine the prime number. Note: n cannot be too large.

Int is_prime (int n)

{

For (int I = 2; I * I <= n; I ++)

If (n % I = 0) return 0;

Return 1;

}

// Prime number table

Int prime [100], count = 0;

Int main ()

{

// The exponent of n and each Prime Number

Int n, p [100];

// Construct a prime number table

For (int I = 2; I <= 100; I ++)

If (is_prime (I) prime [count ++] = I;

While (cin> n)

{

Cout <n <"! "<" = ";

Memset (p, 0, sizeof (p ));

Int maxp = 0;

For (int I = 1; I <= n; I ++)

{

// I must be copied to m instead of being modified directly during division.

Int m = I;

For (int j = 0; j <count; j ++)

While (m % prime [j] = 0) // repeatedly divided by prime [j] and accumulated p [j]

{

M = m/prime [j];

P [j] ++;

If (j> maxp) maxp = j; // update the maximum prime factor subscript.

}

}

// Only loop to the maximum subscript

For (int I = 0; I <= maxp; I ++)

Cout <"<p [I];

Cout <endl;

}

Return 0;

}

 

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.