CASE STUDY 02 · MATHEMATICS

Mathematics for Programming Counting + Logarithms

Password Combination Analyzer

Count possible strings under precise character rules, measure how policies change the search space and understand what the mathematics does—and does not—say about security.

01 · PROBLEM DEFINITION

A policy changes the sample space

Suppose a system allows lowercase letters, uppercase letters and digits in an eight-character password. How many strings are possible? What changes when characters cannot repeat? What if at least one character from every selected category is required? These are different counting problems and cannot share one careless formula.

The analyzer builds a mathematical search space from category sizes, length, repetition rules and category requirements. It then expresses the size in bits and estimates exhaustive-search time under an explicitly assumed guessing rate.

Product rule

Multiply independent choices at successive positions.

Inclusion–exclusion

Count strings that use every required category.

Logarithms

Convert a large count into an information measure.

Safety boundary: The tool analyzes abstract search spaces. It does not collect, test or crack real passwords and must not be treated as a complete security rating.
02 · BASIC COUNTING MODELS

Repetition decides between powers and permutations

Characters may repeat

N choices per position, length L ⇒ N^L strings

With 10 digits and length 4, each of four positions has 10 choices: 10×10×10×10 = 10,000. This includes strings such as 0000 and 1221.

Characters may not repeat

P(N,L) = N! / (N−L)! for L ≤ N

The first position has N choices, the next has N−1, and so on. For four distinct digits, the count is 10×9×8×7 = 5,040. When L>N, the count is zero because distinct selection is impossible.

Model question: Does “password length 8” mean exactly eight or up to eight? This case uses exactly L. A range requires summing the count for every allowed length.
03 · CHARACTER CATEGORIES

Adding alphabets changes N; requirements change the rule

Example categoryIllustrative sizeSymbol
Lowercase English letters26a
Uppercase English letters26b
Digits10c
Selected special symbols10d

If all four sets are available with repetition, N=72 and the unrestricted count is 72^L. But “available” is not the same as “must appear”. Requiring every category removes all strings that omit at least one category.

Length feasibility

If four non-empty categories are required, length must be at least four. This simple lower bound is an excellent pre-calculation check. Without repetition, the total length also cannot exceed the combined alphabet size.

04 · INCLUSION–EXCLUSION

Subtract missing categories without double-subtracting

For lowercase L, uppercase U and digits D, begin with all strings from the combined alphabet. Subtract strings with no lowercase, no uppercase and no digits. A string missing both lowercase and uppercase was subtracted twice, so add such intersections back. Finally subtract the strings missing all three.

valid = T
− (missing L + missing U + missing D)
+ (missing L∩U + missing L∩D + missing U∩D)
− missing L∩U∩D

Each term uses available^length when repetition is allowed or P(available,length) otherwise. The alternating sign depends on how many categories are omitted.

Small verification

Using one lowercase symbol {a}, one digit {1}, length 2 and requiring both gives two valid strings: a1 and 1a. Formula: 2² − 1² − 1² = 2. A good implementation must reproduce this enumerable case before being trusted with enormous counts.

05 · BITS & SEARCH TIME

Log base two answers repeated yes/no decisions

information measure H = log₂(S), where S is search-space size

If S=256, then H=8 because 2⁸=256. This is often called entropy only under a uniform random-choice assumption. Human-chosen passwords are not uniformly sampled: names, keyboard patterns and reused phrases can be much more likely than arbitrary strings.

Exhaustive-work estimates

worst-case guesses = S
average position under random ordering ≈ S/2
time = guesses / guesses_per_second

A guessing rate is not universal. Online authentication may be rate-limited; offline attacks depend on the stored hash, hardware and configuration. The program therefore labels the rate as an input assumption rather than presenting one dramatic number as fact.

06 · PROGRAMMATIC VERIFICATION

Complete Python implementation

The analyzer supports four selectable category sizes, exact length, repetition/no repetition, an optional “use every selected category” rule and a chosen guesses-per-second assumption. Python integers grow automatically, so even very large spaces remain exact.

programs/password-combination-analyzer.py
Open Compiler
Loading source…
Implementation insight: The program enumerates subsets of categories, not passwords. With c selected categories, inclusion–exclusion uses 2ᶜ terms; here c≤4, so it stays tiny even when S is enormous.
07 · INTERACTIVE CALCULATION TRACE

Trace two tiny required categories

  1. Choose an enumerable model.
  2. Count aa, a1, 1a, 11.
  3. Subtract first invalid set.
  4. Subtract second invalid set.
  5. Add intersection back; its contribution is zero.
  6. Apply inclusion–exclusion.
  7. Verify by direct enumeration.
  8. Convert count to information.
  9. Apply an explicit rate assumption.
Current state

Press Next to begin.

08 · MATHEMATICAL TESTS

Use cases small enough to enumerate

One category with repetition
N=10, L=4 must give 10,000. Requiring the only selected category changes nothing.
No repetition
N=10, L=4 must give 5,040; L=11 must give zero.
Two singleton categories
Sizes [1,1], L=2, require both: result must be 2 with repetition and also 2 without repetition.
Impossible category requirement
Four required non-empty categories with L=3 must produce zero valid strings.
Policy monotonicity
Requiring categories cannot increase the unrestricted count; allowing repetition cannot reduce the basic count.
09 · INTERPRETATION & LIMITATIONS

Combinatorial size is not observed security

Mathematics capturesMathematics alone misses
Number of policy-valid stringsHuman choice bias and reuse
Uniform information measureCommon-password dictionaries
Assumed exhaustive timeHash algorithm and rate limiting
Effect of length/category rulesPhishing, malware and credential leaks

For fixed category count, unrestricted counting is O(1) arithmetic conceptually. Required-category counting uses O(2ᶜ) terms. Large-integer arithmetic cost grows with the number of digits, so real runtime is not literally constant for arbitrarily huge numbers.

10 · PRACTICE & EXTENSIONS

Check the model choice

Which formula counts length L strings from N symbols when repetition is allowed?

Why add pairwise missing-category intersections back?

Extensions

  1. Count all permitted lengths from a minimum to maximum.
  2. Require at least two digits using complementary counting or generating functions.
  3. Compare uniform random generation with a deliberately biased toy generator.
  4. Plot log₂(search space) as length increases.
  5. Add exact custom category sizes while preventing overlapping symbols.
11 · INTERVIEW PREPARATION

State the counting assumptions

When do you use N^L?

When there are N choices at every one of L ordered positions and reuse is allowed.

When do you use permutations?

When positions are ordered but a selected symbol cannot be used again, producing N(N−1)… choices.

Why is inclusion–exclusion necessary?

Invalid sets such as “missing lowercase” overlap. Simple subtraction removes their intersections multiple times.

Does log₂(S) prove password entropy?

Only under a uniform choice model. A generation process with predictable preferences has lower effective uncertainty.

Why use arbitrary-precision integers?

Search spaces grow exponentially and quickly exceed fixed 64-bit ranges; exact big integers avoid overflow.

12 · KEY TAKEAWAY

Correct counting begins with precise rules

Powers, permutations and inclusion–exclusion answer different models. The strongest result is not the largest number—it is a count whose alphabet, length, repetition, requirements and interpretation are all explicit.