3

I'm interested in tips for my algorithm that I use to find out the divisors of a very large number, more specifically "n over k" or C(n, k). The number itself can range very high, so it really needs to take time complexity into the 'equation' so to say.

The formula for n over k is n! / (k!(n-k)!) and I understand that I must try to exploit the fact that factorials are kind of 'recursive' somehow - but I havent yet read too much discrete mathematics so the problem is both of a mathematical and a programming nature.

I guess what I'm really looking for are just some tips heading me in the right direction - I'm really stuck.

zacdawg
  • 35
  • 5
  • You could factor the numerator and the denominator after canceling with the larger of `k!` and `(n-k)!`. This would mean factoring a total of `2 * min (k, n-k)` numbers. After that, cancel common factors. Factoring is time somewhat time consuming, but seeing how the recursions for binomial coefficients I can think of off the top of my head are additive, not multiplicative, you'd probably have a hard time deriving a closed simple expression/recursion for factors. – G. Bach May 13 '16 at 11:50

2 Answers2

2

First you could start with the fact that : C(n,k) = (n/k) C(n-1,k-1).
You can prouve that C(n,k) is divisible by n/gcd(n,k).
If n is prime then n divides C(n,k).
Check Kummer's theorem: if p is a prime number, n a positive number, and k a positive number with 0< k < n then the greatest exponent r for which p^r divides C(n,k) is the number of carries needed in the subtraction n-k in base p.

Let us suppose that n>4 :

  • if p>n then p cannot divide C(n,k) because in base p, n and k are only one digit wide → no carry in the subtraction

  • so we have to check for prime divisors in [2;n]. As C(n,k)=C(n,n-k) we can suppose k≤n/2 and n/2≤n-k≤n

  • for the prime divisors in the range [n/2;n] we have n/2 < p≤n, or equivalently p≤n<2p. We have p≥2 so p≤n < p² which implies that n has exactly 2 digits when written in base p and the first digit has to be 1. As k≤n/2 < p, k can only be one digit wide. Either the subtraction as one carry and one only when n-k< p ⇒ p divides C(n,k); either the subtraction has no carry and p does not divide C(n,k).
    The first result is :

    every prime number in [n-k;n] is a prime divisor of C(n,k) with exponent 1.
    no prime number in [n/2;n-k] is a prime divisor of C(n,k).

  • in [sqrt(n); n/2] we have 2p≤n< p², n is exactly 2 digits wide in base p, k< n implies k has at most 2 digits. Two cases: only one carry on no carry at all. A carry exists only if the last digit of n is greater than the last digit of p iif n modulo p < k modulo p The second result is :

    For every prime number p in [sqrt(n);n/2] p divides C(n;k) with exponent 1 iff n mod p < k mod p p does not divide C(n;k) iff n mod p ≥ k mod p

  • in the range [2; sqrt(n)] we have to check all the prime numbers. It's only in this range that a prime divisor will have an exponent greater than 1

Dhruv Joshi
  • 33
  • 1
  • 7
Picodev
  • 506
  • 4
  • 8
0

There will be an awful lot of divisors. If you only need prime divisors, then this is easy for each prime: the exponent of p in n! is [n/p] + [n/p^2] + [n/p^3] + ... where [x] denotes the integer part of x. There will be only a finite number of non-zero terms. You can reuse the result of [n/p^t] to calculate [n/p^(t+1)].

As an example, how many zeroes are at the end of 2022!? Let's find the number of times 5 divides 2022!

[2022/5] = 404
[404/5] = 80
[80/5] = 16
[16/5] = 3
[3/5] = 0

So 5 divides 2022! 404+80+16+3=503 times, and 2 obviously more times than that, so 10=5*2 does it 503 times. Check.

So in order to find how many times a prime p divides a binomial coefficient C(n,k), di the above calculation for n, k and n-k separately, and subtract:

the exponent of p in C(n,k) = (the exponent of p in n!) - 
                              (the exponent of p in k!) -
                              (the exponent of p in (n-k)!)

Now repeat this for all prime numbers less or equal than sqrt(n), and you are done.

This is essentially the same calculation as in the other answer but without computing numbers base p explicitly, so it could be a bit easier to perform in practice.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243