Algorithm--factorial and factor

Source: Internet
Author: User

Factorial and factor

Requirements: Enter a positive integer n (1<n <101), and the factorial n! =1X2X3X...XN decomposition into a factor multiplied by the form of small to large output of each prime number (2,3,5, ... ) of the index.

For example, 825=3x52x11 should be represented as (0,1,2,0,1), indicating 0,1,2,0,1 2,3,5,7,11 respectively. The program should ignore primes that are larger than the maximum element factor (otherwise there will be an infinite number of 0 at the end).

  5  53   5 ! =3  1  53 ! =49    8  4  4  2  2  1  1  1  1  1  1  

Algorithm code:

#include <iostream>#include<cstring>using namespacestd;intprime[ -], CNT;BOOLIsPrime (intN//Judging primes, n cannot be too large{    if(N <2)return 0;  for(inti =2; I * I <= N; i++)        if(n% i = =0)return 0; return 1;}intMain () { for(inti =2; I <= -; i++)            if(IsPrime (i)) prime[cnt++] = i;//List of construction primes            intN, p[ -];  while(Cin >>N) {if(N <2|| N > -) Break; Memset (P,0,sizeof(p)); intMax =0;  for(inti =1; I <= N; i++)        {            intm = i;//I need to copy the I into m instead of modifying it directly while doing the division.             for(intj =0; J < CNT; J + +)            {                 while(m% prime[j] = =0)//divide repeatedly by prime[j], and accumulate p[j]{m/=Prime[j]; P[J]++; if(J > Max) max = j;//Update Max Factor subscript                }            }        }         for(inti =0; I <= Max; i++)//loop only to the maximum subscript{cout<< P[i] <<" "; } cout<<Endl; }}

Algorithm--factorial and factor

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.