Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7968011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:00:37+00:00 2026-06-04T07:00:37+00:00

What could be the simplest and time efficient logic to express n! as product

  • 0

What could be the simplest and time efficient logic to express n! as product of powers of prime?

I am more interested to find the powers of prime so that I can know the numbers of factors.
As n! can be expresses as p1^e1 * p2^e2 * … * pk^ek, where each p is a prime number, then the number of factors of n is (e1 + 1)(e2 + 1) … *(ek + 1)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:00:38+00:00Added an answer on June 4, 2026 at 7:00 am

    The most efficient way to find the prime factorisation of n! that I know of is counting how often each prime appears as a factor in n!. Obviously, no prime larger than n appears, so let p <= n be a prime.

    Among the numbers 1, ..., n, there are

    q1 = floor(n/p)
    

    multiples of p. Among these, there are

    q2 = floor(q1/p) = floor(n/p²)
    

    multiples of p². Among these, there are

    q3 = floor(q2/p) = floor(n/p³)
    

    multiples of p³. Etc. So the exponent of p in the prime factorisation of n! is

    q1 + q2 + q3 + ...
    

    (An a = p^k*b, with b not divisible by p contributes k to the exponent, and appears in the k lists corresponding to the counts q1, ..., qk.)
    We can succinctly write a function for that:

    unsigned long long factorial_exponent(unsigned long long n, unsigned long long p) {
        unsigned long long exponent = 0;
        do {
            n /= p;
            exponent += n;
        }while(n);
        return exponent;
    }
    

    That uses floor(log n/log p) + 1 divisions, so, if the primes not exceeding n are known, that contributes approximately

    log n * ∑ (1/log p + 1) ≈ 2n/log n
           p≤n
    

    divisions and additions to the total work. (Note: since most of the primes ≤ n are > √n, and for primes p > √n obviously q2 = 0, it is faster to calculate their exponent directly: n/p, that reduces the number of divisions needed by about half.)

    Finding the primes not exceeding n is best done with a sieve, if you already have a good implementation, the Sieve of Atkin does it in O(n) or O(n/log log n) operations (depends on the implementation, but for feasible ranges, log log n can be considered a constant), otherwise use the Sieve of Eratosthenes, it’s simple to implement and finds the primes not exceeding n in – again depending on the implementation – O(n*log log n) or O(n) operations.

    The total work for that algorithm is dominated by finding the primes (but for feasible n, the contribution of the determination of the exponents is still not negligible).

    On the other hand, the work needed for finding the prime factorisation of each k ≤ n of course depends on the algorithm used for that. Using trial division for that would result in a total work of about c*n^1.5/log n – I haven’t done anything to determine the constant c, and depending on details, you may have a factor of log n in the numerator or denominator, but it’s basically n^1.5. A better method of finding the factorisations would be first finding the smallest prime factor [or any prime factor] with a modification of the Sieve of Eratosthenes, again in O(n*log log n) operations, and then use that to find the factorisation. You can store the factorisations and then, when processing k with the known prime divisor p, look up the factorisation of k/p, or generate the factorisation on the fly by recursively looking up the known prime factor q of k/p, then of k/(p*q) etc. until the factorisation is complete – that is much simpler to handle if the known prime factor is always the smallest.

    On average, the prime factorisation of k contains ≈ log log k terms, so that method would lead to O(n*log log n) overall complexity. But the constant factors in this method are considerably larger than in the first, so even if the prime-finding gives the same complexity, the first is faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What could be the simplest and time efficient logic to find out the factors
Could you let me know how can I change font size of button caption
What would be a simplest algorithm one or more people could use to decide
This is the simplest example I could come up with that reproduces the problem.
I am using Visual C# 2008 Express edition. If at design time I have
I thought that I could use SimpleDB to take care of the most challenging
I was looking for the simplest way to convert a date an time from
I may have bitten off more than i can chew here. I've moved outside
I'm getting this error... ImportError: Could not find 'input_readers' on path 'map reduce' when
As a test I am trying to create the simplest webpage where I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.