03

Phase 1 - Level 3: Math and Number Logic

Chapter 3 • Intermediate

75 min

Phase 1 - Level 3: Math and Number Logic

Introduction

This level combines conditional thinking with mathematical operations. You'll work with digits, number properties, and mathematical relationships.

Working with Digits

To extract digits from a number:

  • Use modulo (%) to get last digit
  • Use integer division (//) to remove last digit
  • Repeat to process all digits

Number Properties

Common checks:

  • Perfect Square: Check if a number is a perfect square
  • Digit Analysis: Extract and analyze individual digits
  • Number Range: Check if number falls in specific ranges
  • Mathematical Relationships: Check arithmetic/geometric progressions

Key Techniques

  1. Digit Extraction: num % 10 gives last digit, num // 10 removes last digit
  2. Perfect Square Check: Check if square root is an integer (without using sqrt)
  3. Digit Comparison: Compare digits within a number
  4. Range Validation: Check if number is within [min, max]

Common Patterns

  • Extract digits and check properties
  • Compare digits within a number
  • Check mathematical relationships
  • Validate number ranges

Hands-on Examples

Check if All Digits are Distinct

# Take a 3-digit number
num = int(input("Enter a 3-digit number: "))

# Extract digits
digit1 = num // 100
digit2 = (num // 10) % 10
digit3 = num % 10

# Check if all distinct
if digit1 != digit2 and digit2 != digit3 and digit1 != digit3:
    print("All digits are distinct")
else:
    print("Digits are not all distinct")

Extract each digit using integer division and modulo, then compare all pairs to ensure they are different.