AI Skill Report Card
Using Math Integer Operations
Quick Start14 / 15
Pythonimport math # Greatest common divisor / least common multiple math.gcd(48, 18) # 6 math.lcm(4, 6) # 12 # Integer square root (floor of sqrt) math.isqrt(50) # 7 # Factorial math.factorial(5) # 120 # Combinatorics math.comb(5, 2) # 10 (combinations) math.perm(5, 2) # 20 (permutations) # Integer bit length (255).bit_length() # 8 (255).bit_count() # 8 (number of set bits)
Recommendation▾
This is a reference doc for well-known standard library functions rather than a methodology skill — it lacks a real workflow with decision points or non-obvious problem-solving guidance, which caps completeness.
Workflow10 / 15
Progress:
- Identify whether the task needs exact integer math (avoid float rounding issues)
- Choose the correct function:
gcd/lcmfor divisibility,isqrtfor roots,factorial/comb/permfor counting - Validate input types — these functions require non-negative integers (raise
ValueErroron negatives where applicable) - Use built-in
intmethods (bit_length,bit_count) for binary-level analysis instead of manual bit-shifting loops - Test edge cases: zero, one, and large numbers (Python ints are arbitrary precision, so no overflow — but performance may degrade)
Recommendation▾
Examples are trivial function calls rather than realistic scenarios where choosing the right approach matters (e.g., a task combining multiple functions or a tricky edge case like gcd(0,0)).
Examples13 / 20
Example 1: Input: Find the GCD and LCM of 24 and 36. Output:
Pythonmath.gcd(24, 36) # 12 math.lcm(24, 36) # 72
Example 2: Input: Compute the integer square root of 99 without floating-point error. Output:
Pythonmath.isqrt(99) # 9 (since 9*9=81 <= 99 < 100=10*10)
Example 3: Input: Count how many ways to choose 3 items from 7 (combinations), and how many ordered arrangements (permutations). Output:
Pythonmath.comb(7, 3) # 35 math.perm(7, 3) # 210
Example 4: Input: Determine the number of bits needed to represent 1023 in binary. Output:
Python(1023).bit_length() # 10
Recommendation▾
Add a section on when NOT to use these (e.g., very large factorials causing performance issues, or when to fall back to numpy for vectorized integer math).
Best Practices
- Prefer
math.isqrt()overint(math.sqrt(n))for exact results on large integers — floating-pointsqrtloses precision beyond ~2^53. - Use
math.gcd(*args)with multiple arguments to compute GCD across more than two numbers at once. - Use
math.comb/math.perminstead of manually computing factorials and dividing — they're faster and avoid intermediate overflow-like slowdowns. - For bitwise integer inspection, prefer
int.bit_length()andint.bit_count()over string conversion (bin()) or manual loops. - Remember Python integers have arbitrary precision — no need for overflow checks, but be mindful of performance with very large numbers (e.g.,
factorial(100000)).
Common Pitfalls
- Don't pass negative numbers to
math.isqrt(),math.factorial(),math.comb(), ormath.perm()— they raiseValueError. - Don't use
math.sqrt(n) ** 2 == nto check perfect squares — usemath.isqrt(n) ** 2 == ninstead to avoid float precision errors. - Don't confuse
math.comb(n, k)(order doesn't matter) withmath.perm(n, k)(order matters). - Don't manually implement GCD/LCM with loops — the built-in
math.gcd/math.lcmare optimized (C-implemented) and handle edge cases like zero correctly (gcd(0, 0) == 0). - Don't assume
bit_length()counts set bits — it returns the number of bits needed to represent the number, not the population count (usebit_count()for that).