📖 Scenario: Imagine you are organizing a sports event where players are assigned numbers. You want to find pairs of players whose numbers have a special relationship based on divisibility. This is a common real-world example where math and number theory help solve problems efficiently.
🎯 Goal: You will create a list of player numbers, set a divisor, find all players whose numbers are divisible by that divisor, and print the filtered list. This shows how math concepts like divisibility appear in data structure and algorithm problems.
📋 What You'll Learn
Create an array called playerNumbers with these exact values: [12, 15, 20, 25, 30]
Create a variable called divisor and set it to 5
Use a for loop with variable number to iterate over playerNumbers and collect numbers divisible by divisor into a new array called divisiblePlayers
Print the divisiblePlayers array
💡 Why This Matters
🌍 Real World
Filtering data based on numeric conditions is common in scheduling, resource allocation, and game logic.
💼 Career
Understanding how math and number theory integrate with programming helps in coding interviews and solving algorithmic challenges efficiently.
Progress0 / 4 steps
1
Create the list of player numbers
Create an array called playerNumbers with these exact values: [12, 15, 20, 25, 30]
DSA Typescript
Hint
Use const to create the array and include all numbers exactly as shown.
2
Set the divisor value
Create a variable called divisor and set it to 5
DSA Typescript
Hint
Use const to create the divisor variable and assign the value 5.
3
Find numbers divisible by the divisor
Use a for loop with variable number to iterate over playerNumbers and collect numbers divisible by divisor into a new array called divisiblePlayers
DSA Typescript
Hint
Use the modulo operator % to check if the number is divisible by divisor (remainder zero).
4
Print the filtered list
Print the divisiblePlayers array
DSA Typescript
Hint
Use console.log(divisiblePlayers) to print the array.
Practice
(1/5)
1. Why do many Data Structures & Algorithms problems involve concepts from Math and Number Theory?
easy
A. Because they help solve problems involving patterns, divisibility, and optimization
B. Because Math is easier than programming
C. Because Number Theory is unrelated to algorithms
D. Because Math concepts slow down the algorithm
Solution
Step 1: Understand the role of Math in DSA
Math and Number Theory provide tools to analyze patterns, divisibility, and properties of numbers which are common in algorithm problems.
Step 2: Connect Math to problem solving
These concepts help optimize solutions and solve problems efficiently, such as finding primes, gcd, or modular arithmetic.
Final Answer:
Because they help solve problems involving patterns, divisibility, and optimization -> Option A
Quick Check:
Math aids pattern and optimization in DSA = Because they help solve problems involving patterns, divisibility, and optimization [OK]
Hint: Math helps find patterns and optimize algorithms fast [OK]
Common Mistakes:
Thinking Math is unrelated to algorithms
Assuming Math makes problems slower
Ignoring Number Theory concepts
2. Which of the following is the correct TypeScript syntax to calculate the greatest common divisor (GCD) of two numbers a and b using Euclid's algorithm?
easy
A. function gcd(a: number, b: number): number { return Math.max(a, b); }
B. function gcd(a: number, b: number): number { while (a !== 0) { let temp = b; a = b % a; b = temp; } return b; }
C. function gcd(a: number, b: number): number { return a + b; }
D. function gcd(a: number, b: number): number { return b === 0 ? a : gcd(b, a % b); }
Solution
Step 1: Identify Euclid's algorithm structure
Euclid's algorithm uses recursion and modulo: gcd(a, b) = gcd(b, a % b) until b is zero.
Step 2: Match syntax with algorithm
function gcd(a: number, b: number): number { return b === 0 ? a : gcd(b, a % b); } correctly implements recursion with base case b === 0 returning a.
Final Answer:
function gcd(a: number, b: number): number { return b === 0 ? a : gcd(b, a % b); } -> Option D
Quick Check:
Recursive gcd with modulo = function gcd(a: number, b: number): number { return b === 0 ? a : gcd(b, a % b); } [OK]
Hint: Use recursion and modulo for GCD in TypeScript [OK]
Common Mistakes:
Using addition instead of modulo
Returning max instead of gcd
Incorrect loop conditions
3. What is the output of the following TypeScript code that uses modular arithmetic to find the remainder of a large exponentiation?
const mod = 1000000007;
function modExp(base: number, exp: number, mod: number): number {
if (exp === 0) return 1;
let half = modExp(base, Math.floor(exp / 2), mod);
let result = (half * half) % mod;
if (exp % 2 !== 0) result = (result * base) % mod;
return result;
}
console.log(modExp(2, 10, mod));
medium
A. 1000000007
B. 512
C. 1024
D. 0
Solution
Step 1: Understand modular exponentiation
The function calculates (2^10) % 1000000007 efficiently using recursion and squaring.
Step 2: Calculate 2^10 modulo 1000000007
2^10 = 1024, which is less than mod, so result is 1024.
Final Answer:
1024 -> Option C
Quick Check:
2^10 mod 1000000007 = 1024 [OK]
Hint: Use fast exponentiation to compute large powers modulo [OK]
Common Mistakes:
Confusing exponentiation with multiplication
Ignoring modulo operation
Incorrect base cases in recursion
4. Identify the bug in the following TypeScript function that tries to check if a number is prime:
function isPrime(n: number): boolean {
if (n <= 1) return false;
for (let i = 2; i <= n / 2; i++) {
if (n % i === 0) return false;
}
return true;
}
medium
A. The loop should run until i <= Math.sqrt(n)
B. The modulo operator % is used incorrectly
C. The function should return true for n <= 1
D. The loop should run until i < n, not n / 2
Solution
Step 1: Analyze the loop range for prime checking
Checking divisors up to n/2 is inefficient; checking up to sqrt(n) is enough.
Step 2: Correct the loop condition
Change loop condition to i <= Math.sqrt(n) to improve performance and correctness.
Final Answer:
The loop should run until i <= Math.sqrt(n) -> Option A
Quick Check:
Prime check loop limit = sqrt(n) [OK]
Hint: Check divisors only up to square root of n [OK]
Common Mistakes:
Looping too far causing inefficiency
Returning true for non-prime numbers
Misusing modulo operator
5. You want to find the number of integers between 1 and 1000 that are divisible by 3 or 5 but not both. Which approach using Number Theory concepts is best?
hard
A. Count multiples of 3 and 5, then subtract the count of multiples of 15
B. Count multiples of 3 and 5, then subtract twice the count of multiples of 15
C. Count multiples of 3 only
D. Count multiples of 5 only
Solution
Step 1: Use inclusion-exclusion principle
Count multiples of 3 and 5 separately, then subtract multiples of their LCM (15) to avoid double counting.
Step 2: Adjust for numbers divisible by both
Since we want numbers divisible by 3 or 5 but not both, subtract twice the count of multiples of 15.
Final Answer:
Count multiples of 3 and 5, then subtract twice the count of multiples of 15 -> Option B
Quick Check:
Use inclusion-exclusion with double subtraction for both divisibility [OK]
Hint: Use inclusion-exclusion and subtract twice for both divisors [OK]
Common Mistakes:
Subtracting multiples of 15 only once
Counting only one divisor's multiples
Ignoring overlap of multiples
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.