Algorithm chapter-Factor and factorial

Source: Internet
Author: User

  source : "Algorithmic Competition Primer Classic" Example 5.4.2

  title : input positive integer n (2≤n≤100), the factorial n!=1*2*3*...*n decomposition into the form of prime factor multiplication, from small to large output each prime number (2, 3, 5 ...) of the exponent. For example, 5! expressed as 3 1 1 (5!=23*31*51=120), the program ignores primes that are larger than the maximum element factor (otherwise there will be an infinite number of 0 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

  Analysis:
Note that the n<=100, so the vegetarian factor also must not exceed 100 (can be used contradiction to prove that if there is a factor greater than 100, the element is not eliminated in the equation, the equation is not true). We first construct a prime table, and then use the factorial of each factor (small to large) and the prime number of the prime number of the table to take the model, the whole can be proved that the prime number is a prime factor, and record the exponent of each prime. Because am * an = am+n, we simply add up the exponent corresponding to all the element factors. An array p is used to hold the exponent of the corresponding prime number, and the index of the largest factor is labeled MAXP, and the exponent of the maximum factor is finally looped.

  Source :

#include <stdio.h>#include<string.h>intIs_prime (intN) {    inti;  for(i=2; i*i<=n;i++)        if(n%i==0)return 0; return 1;}intMain () {intn,m,i,j,prime[ -],p[ -],maxp,count=0;//Primer is the Prime table and P is the exponent corresponding to each prime number in the Prime number table .//count is the number of primes in the prime list, MAXP is the maximum element factor that can be output at the end of the index corresponding to the subscript of P     for(i=2; i<= -; i++)//a list of prime numbers, the prime factor must not exceed        if(Is_prime (i)) prime[count++]=i;  while(SCANF ("%d", &n) = =1) {printf ("%d! =", N); Memset (P,0,sizeof(p));//exponential 0 For each prime numbermaxp=0;//Max Factor index 0         for(i=1; i<=n;i++) {m=i;//each factor of factorial is cyclically divided by the number of primes in the (modulo) prime number table.             for(j=0; j<count;j++)                 while(m%prime[j]==0)//The ability to complete the proof is a factor .{m/=Prime[j]; P[J]++;//factor index plus 1                    if(J&GT;MAXP) Maxp=j;//Update Max Factor subscript                }        }         for(i=0; i<=maxp;i++)//omit primes larger than the maximum element factorprintf"%d", P[i]); printf ("\ n"); }    return 0;}

Algorithm chapter-Factor and factorial

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.