Math Fundamentals18 sections · 814 units
Open in Course

Count Good Numbers - Exponent Calculation

Half positions, ceiling trick

How many even positions? Index 0, 2, 4, ... up to n1n-1. If nn is even, you have n/2n/2 of each. If nn is odd, even positions get one extra.

Formula: even positions = n/2\lceil n/2 \rceil, odd positions = n/2\lfloor n/2 \rfloor. In code, ceiling is (n+1)/2(n + 1) / 2 (integer division), floor is n/2n / 2.

Check: n=4n = 4 gives 52×425^2 \times 4^2. n=5n = 5 gives 53×425^3 \times 4^2. Matches expectations.