4 kyu

One Line Task: Mathematical Combination

Description:

Big thanks to @dfhwze for the approval.

This kata is inspired by @Voile's One Line Task: Hard Fibonacci Numbers.

Task

Write a function that takes two non-negative integers n and k, and computes the mathematical combination (nk)\binom{n}{k}.


Examples

  • (10)=1, (11)=1, (12)=0\binom{1}{0} = 1,\ \binom{1}{1} = 1,\ \binom{1}{2} = 0

  • (63)=20, (64)=15, (69)=0\binom{6}{3} = 20,\ \binom{6}{4} = 15,\ \binom{6}{9} = 0


Note

  •  nN{0}    (n0)=(nn)=1\forall\ n \in \mathbb{N} \cup \{ 0 \} \implies \binom{n}{0} = \binom{n}{n} = 1.

  •  n,kN{0}k>n    (nk)=0\forall\ n,k \in \mathbb{N} \cup \{ 0 \} \land k \gt n \implies \binom{n}{k} = 0.

  • Lines of your code must not exceed 1.

  • Length of your code must not exceed 44. (Record: @monadius's 41 characters)

  • Disabled modules are numpy, scipy, sklearn, gmpy2, math and sys.

  • Disabled built-in functions are open, exec, eval, globals, locals and exit.

  • Credits to @Blind4Basics's module forbidder.


Tests

  • For all tests, n,kN{0}k<max(n+2,2n)n,k \in \mathbb{N} \cup \{ 0 \} \land k \lt \max (n+2, 2n).
  • 108 fixed tests, including 6 example tests, 93 mini tests and 9 big tests.

  • 300 small random tests, n[100,200)n \in [100, 200).

  • 20 big random tests, n[1100,1200)n \in [1100, 1200).

  • One Last Test, n[4990,5000)k[49n100,51n100)n \in [4990, 5000) \land k \in \left[\lfloor \frac{49n}{100} \rfloor, \lfloor \frac{51n}{100} \rfloor \right).


I hope you enjoy the tests. I had fun writing them. ;)

Restricted
Performance
Mathematics

More By Author:

Check out these other kata created by lachesism

Stats:

CreatedFeb 17, 2024
PublishedFeb 17, 2024
Warriors Trained427
Total Skips15
Total Code Submissions869
Total Times Completed31
Python Completions31
Total Stars21
% of votes with a positive feedback rating100% of 9
Total "Very Satisfied" Votes9
Total "Somewhat Satisfied" Votes0
Total "Not Satisfied" Votes0
Total Rank Assessments4
Average Assessed Rank
4 kyu
Highest Assessed Rank
3 kyu
Lowest Assessed Rank
4 kyu
Ad
Contributors
  • lachesism Avatar
  • dfhwze Avatar
Ad