Getting started with algorithm competitions 5.42 basics of mathematics-factors and factorial, getting started with algorithm Competitions

Source: Internet
Author: User

Getting started with algorithm competitions 5.42 basics of mathematics-factors and factorial, getting started with algorithm Competitions

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 output from small to large is (2, 3, 4, 5 ...) Index. For example, 825 = 3*5*5*11 should be expressed as (, 1 ), 0, 1, 2, 0, 1, 2, 3, 5, 7, and 11 respectively. The program should ignore prime numbers greater than the maximum 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

1 # include <stdio. h> 2 # include <string. h> 3 // determine the prime number 4/* do NOT use this if x is very large or small 5 * n too hour n = 1 will be incorrectly judged as a prime number, I * I may overflow 6 */7 int is_prime (int n) 8 {9 int I; 10 for (I = 2; I * I <= n; I ++) // judge the integer i11 if (n % I = 0) return 0 that cannot exceed sqrt (x); // once a factor greater than 1 is found, returns 0 (false) 12 return 1 immediately; // returns 1 (true) 13} 14 15 // prime number table 16 int prime [100], count = 0; 17 int main () 18 {19 // variable I, n and the exponent of each Prime Number 20 int I, n, p [100]; 21 // construct a prime number Table 22 for (int I = 2; I <= 100; I ++) 23 if (is_prime (I) prime [count ++] = I; 24 while (scanf ("% d", & n) = 1) 25 {26 printf ("% d! = ", N); 27 memset (p, 0, sizeof (p); // string. h28 int maxp = 0; // defines the maximum prime factor subscript and assigns the initial value 29 for (I = 1; I <= n; I ++) 30 {31 // I must be copied to the variable m, instead of directly modifying it 32 int m = I; 33 for (int j = 0; j <count; j ++) 34 while (m % prime [j] = 0) // repeatedly divided by prime [j], and accumulate p [I] 35 {36 m/= prime [j]; 37 p [j] ++; 38 if (j> maxp) maxp = j; // update the maximum prime factor subscript 39} 40} 41 // only loop to the maximum subscript 42 for (I = 0; I <= maxp; I ++) 43 printf ("% d", p [I]); 44 printf ("\ n"); 45} 46 return 0; 47}View Code

Analysis:
Because am × an = a (m + n), you only need to accumulate the exponent corresponding to all prime factors. Note that n <= 100, these prime factors will not exceed 100, ignore the last 0 when outputting the data.

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.