Factorial prime numbers factorization


/ Published in: C#
Save to your folder(s)

Find how many factors there is in N! for a prime P
Ex: 5! = (2^3)*(3^1)*(5^1)
mult(5,2) = 3
mult(5,3) = 1
mult(5,5) = 1
all else = 0


Copy this code and paste it in your HTML
  1. /// <summary>
  2. /// Find how many factors there is in N for a prime P
  3. /// </summary>
  4. static int multiplicity(int n, int p)
  5. {
  6. int q = n, m = 0;
  7. if (p > n) return 0;
  8. if (p > n / 2) return 1;
  9. while (q >= p)
  10. {
  11. q /= p;
  12. m += q;
  13. }
  14. return m;
  15. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.