✦ New Age Learning 🔍
Home › Library › AI for New Learner- Senior 1 › Chapter 5: Logic, Algorithms & Complexity

📖 Chapter 5 of 12 · AI for New Learner- Senior 1 · Class 9

Chapter 5: Logic, Algorithms & Complexity

In this chapter

  • Daksh from Indore — the bug-hunting champion.
  • Boolean logic: the AND, OR, NOT every AI is built from.
  • Sorting and searching — the algorithms behind every fast app.
  • Big-O notation — why a small idea makes AI 1000× faster.
  • When a ‘slow’ algorithm sinks a perfect AI.

The Big Idea

AI is not just about clever models. The MATH part is what predicts. The LOGIC part is what DECIDES. An AI doctor uses logic to choose: ‘IF the X-ray is unclear AND age > 60, send to specialist’. Algorithms are the recipes that turn data into decisions. The fastest algorithm wins — even when a slower one is more ‘accurate’. Speed is a feature.

Did you know?

Indian programmers WIN top global competitive-programming contests every year. Codeforces top spots are traded between world rivals and Indian coders from IIIT-H. Algorithms are the world’s most level playing field — a kid from Indore can outrank an MIT PhD.

Builder Profile: Daksh

Indore, MP · Daksh, age 14

Daksh shows how O(n²) code explodes while O(1) and O(n) stay flat — speed is a feature.

Daksh is from Indore. Last year he discovered competitive programming — and his life changed. Every evening after school, he opens Codeforces or CodeChef and tries one problem. Most days he fails. Some days he solves. That single hour of struggle has made him the fastest debugger in his school.

Last month, his school’s AI club built a model to spot copied homework. It worked on 100 essays in 2 seconds. Then they tested on 10,000 essays. It took 6 HOURS. The seniors panicked. Daksh looked at the code, said three words — ‘O(n squared)’ — and rewrote it. Now it runs in 12 seconds.

“The model wasn’t wrong,” he says. “The algorithm was slow. That is what every senior engineer fixes day after day. Faster code is honest code. Honest code is what India runs on.”

⊕ MATH MINUTE Truth Tables

Every Boolean operator gives YES (True) or NO (False) for every combination. AND is True only if BOTH inputs are True. OR is True if AT LEAST ONE is True. NOT flips True to False and back.

T AND F = F | T OR F = T | NOT T = F

WHY THIS MATTERS

Every AI decision is a stack of Boolean conditions: ‘IF (income > 25000) AND (age ≥ 21) AND (no defaults) → APPROVE’. Loan AI at HDFC, fraud AI at Paytm, attendance AI at your school — all built on Boolean logic. Master AND/OR/NOT and you can READ any AI’s decision rules.

💻 CODE LAB Loan AI from Boolean rules

# A tiny but real loan decision AI
def approve(age, salary, has_pan):
    eligible = (age >= 21) and (salary >= 25000) and has_pan
    risky = (age < 23) or (salary < 30000)
    if eligible and not risky:
        return 'APPROVED'
    elif eligible:
        return 'MANAGER REVIEW'
    else:
        return 'DECLINED'

print(approve(25, 50000, True))   # APPROVED
print(approve(22, 28000, True))   # MANAGER REVIEW

Big-O — how fast is your AI?

Big-O describes how work grows with input size n. O(1) is constant — same speed for any n (a dict lookup). O(log n) — doubling n adds just one step (binary search). O(n) — doubling n doubles the work (a single loop). O(n log n) — good sorting. O(n²) — doubling n means 4× the work; dangerous (comparing every pair). O(2ⁿ) — adding one to n DOUBLES the work; the crash zone.

Daksh shows how O(n²) code explodes while O(1) and O(n) stay flat — speed is a feature.

✍ WORKED EXAMPLE How Long for 1,000,000 Students?

Problem: A school AI compares every student to every other to find duplicates. 1,000 students take 1 second. How long for 1,000,000 students if the algorithm is O(n²)?

  1. Time scales as n². If n grows 1000× (1,000 → 1,000,000), time grows 1000² = 1,000,000×.
  2. 1 second × 1,000,000 = 1,000,000 seconds.
  3. Convert to days: 1,000,000 ÷ 86,400 &asymp; 11.6 days.
Show answer

✓ Answer: Almost 12 DAYS. A faster O(n log n) algorithm finishes in about 20 seconds.

⚠ COMMON MISTAKE

✗ Mistake: Writing your own sort with nested loops (bubble sort). It’s O(n²) and crashes on big data.

✓ Fix: Use Python’s built-in sorted() or list.sort(). Both use Timsort — O(n log n) — battle-tested by millions. The fastest line of code is the one someone else already wrote and tested.

Solved illustrations

1. What is (True AND False) OR (NOT False)?

Show answer

Answer: Step 1: True AND False = False. Step 2: NOT False = True. Step 3: False OR True = True. Final: TRUE.

2. Why does a school timetable AI sometimes take hours?

Show answer

Answer: Generating a valid timetable is a constraint problem — checking every combination is O(2ⁿ). For 30 teachers × 40 periods that is astronomical. Real systems use ‘heuristics’ (smart shortcuts) that find good-enough answers in about O(n²).

3. What is the Big-O of Python’s len(my_list)?

Show answer

Answer: O(1) — constant. Python keeps a counter as the list grows, so len() does NOT loop through. That’s why we use len() instead of writing our own counter.

4. Why is binary search so much faster than linear search?

Show answer

Answer: Linear search checks every item: O(n) — average 50,000 checks for 1 lakh items. Binary search halves the list each step: O(log n) — only 17 checks! A 3000× speedup. But it requires the data to be sorted first.

5. Can a slow algorithm ruin a great AI model?

Show answer

Answer: Yes — completely. A face-recognition model with 99% accuracy that takes 10 seconds per face is useless at an airport. Speed makes accuracy USABLE.

Questions — Chapter 5

A. Boolean workout

  1. (True AND True) AND (NOT False) =
  2. (False OR True) AND False =
  3. NOT (True AND True) =
  4. (5 > 3) AND (4 == 4) =
  5. (10 < 5) OR (NOT (3 == 3)) =

B. Big-O thinking

  1. Write the Big-O of: looping through every item once.
  2. Write the Big-O of: looping through every PAIR of items.
  3. Which is faster for 1 million items: O(n²) or O(n log n)? By how much?
  4. Why does an O(2ⁿ) algorithm ‘crash’ on n = 50?
  5. Give one real Indian app where speed matters more than accuracy.

Ship-It Challenge

Solve ONE problem on Codeforces or CodeChef. Note its complexity. Show a friend.

Check your understanding — Chapter 5

Tap an answer to check it instantly. These are for practice — nothing is saved or sent.

Q1. What is (True AND False) OR (NOT False)?

  1. False
  2. True
  3. Error
Show answer

B. True

Q2. Why is binary search so much faster than linear search?

  1. It uses a faster computer
  2. It halves the list each step — O(log n) — so 1 lakh items need only ~17 checks vs ~50,000
  3. It skips most items randomly
Show answer

B. It halves the list each step — O(log n) — so 1 lakh items need only ~17 checks vs ~50,000

Q3. What is the Big-O of Python's len(my_list)?

  1. O(n) — it counts every item
  2. O(1) — Python keeps a counter, so it's instant
  3. O(n squared)
Show answer

B. O(1) — Python keeps a counter, so it's instant

Q4. Can a slow algorithm ruin a great AI model?

  1. No, accuracy is all that matters
  2. Yes — a 99%-accurate face model that takes 10 seconds per face is useless at an airport
  3. Only if the model is wrong
Show answer

B. Yes — a 99%-accurate face model that takes 10 seconds per face is useless at an airport

All chapters in AI for New Learner- Senior 1

  1. Chapter 1: Hello Python, Hello AI
  2. Chapter 2: The Maths AI Needs
  3. Chapter 3: Vectors & Words
  4. Chapter 4: Probability & Confidence
  5. Chapter 5: Logic, Algorithms & Complexity
  6. Chapter 6: Building Your First Real AI
  7. Chapter 7: Working with Real Indian Data
  8. Chapter 8: Your First Real ML Model
  9. Capstone Build-It Projects
  10. Final Book Exam
  11. AI + Python + Math Glossary
  12. Career & College Pathways

Practise for Class 9