求可计算正/负/零值N的Excel二项式系数(N,K)单公式
Hey there, I’ve got a single Excel formula that’ll handle binomial coefficients for positive, negative, or zero values of N (between -10 and 10) and K (0 to 10, per your range constraints) perfectly. It’ll even validate inputs and work for both single calculations and batch table use.
Excel Formula for Generalized Binomial Coefficients
=IF(OR(K<0,K>10,N<-10,N>10),"Invalid Input",IF(K=0,1,IF(N>=0,FACT(N)/(FACT(N-K)*FACT(K)),PRODUCT(ROW(INDIRECT("1:"&K))+N-1)/FACT(K))))
How This Formula Works
Let’s break down each part to make sense of it:
- Input Validation: The opening
IF(OR(K<0,K>10,N<-10,N>10),"Invalid Input",...)checks if your N/K values fall outside the allowed range. If they do, it returns a clear error message instead of a wrong calculation. - K=0 Edge Case: For any valid N, the binomial coefficient (\binom{N}{0}) is always 1. We handle this upfront to skip unnecessary math.
- Non-Negative N: When N is positive or zero, we use the standard factorial-based formula you mentioned:
FACT(N)/(FACT(N-K)*FACT(K)). This is the classic method for calculating binomial coefficients with non-negative integers. - Negative N: For negative N, we use the generalized binomial coefficient formula. The
PRODUCT(ROW(INDIRECT("1:"&K))+N-1)computes the product of terms like (N \times (N+1) \times ... \times (N+K-1)), then dividing by (K!) (viaFACT(K)) gives the correct generalized coefficient.
Batch Calculation Adaptation
If you’re using this in a table (e.g., N in column A, K in column B starting at row 2), adjust the formula to reference your cells:
=IF(OR(B2<0,B2>10,A2<-10,A2>10),"Invalid Input",IF(B2=0,1,IF(A2>=0,FACT(A2)/(FACT(A2-B2)*FACT(B2)),PRODUCT(ROW(INDIRECT("1:"&B2))+A2-1)/FACT(B2))))
Drag this formula down the column, and it’ll automatically compute the binomial coefficient for every row of N/K pairs.
Example Test Values
| N | K | Result |
|---|---|---|
| 5 | 2 | 10 |
| -3 | 2 | 6 |
| 0 | 0 | 1 |
| -5 | 3 | 35 |
| 10 | 10 | 1 |
内容的提问来源于stack exchange,提问作者steveOw
相关产品推荐
相关产品推荐

