Unit 3: GCD and Primality testing - Subjective Questions
CSE329 — Prelude To Competitive Coding • Practice Questions with Detailed Answers
20 questions
Explain the Basic Euclidean algorithm for computing the GCD of two numbers. Illustrate with an example of finding .
The Euclidean algorithm is an efficient method to compute the Greatest Common Divisor (GCD) of two integers.
Principle: It is based on the property that:
The process repeats until the remainder becomes ; the last non-zero remainder is the GCD.
Steps:
- Divide the larger number by the smaller.
- Replace the larger number with the smaller, and the smaller with the remainder.
- Repeat until remainder is .
Example — :
The last non-zero remainder is , so .
Time complexity: , which makes it very efficient.
Describe the Extended Euclidean algorithm. How does it help in finding integers and such that ? Solve for , .
The Extended Euclidean algorithm not only computes but also finds integer coefficients and (called Bézout coefficients) satisfying:
Working principle:
- It extends the basic Euclidean algorithm by keeping track of the coefficients during back-substitution.
- If , then:
Example — , :
Back-substitution:
- So , : check ✓
Applications: Modular inverse computation, solving linear Diophantine equations, and RSA cryptography.
Explain how to find the total number of divisors of a number using its prime factorization. Compute the number of divisors of .
The total number of divisors of a number can be found from its prime factorization.
Formula: If a number is expressed as:
then the total number of divisors is:
Reasoning: Each prime can appear in a divisor with an exponent from to , giving choices.
Example — :
- Prime factorization:
- Number of divisors:
Thus, has 24 divisors.
Describe an efficient algorithm to find all prime factors of a number . Trace it for .
Prime factorization decomposes a number into its prime constituents.
Algorithm:
- Start with the smallest prime .
- While is divisible by the current factor , divide by and record .
- Increment and repeat up to .
- If at the end, itself is a prime factor.
Pseudocode:
for i = 2 to sqrt(N):
while N % i == 0:
print i
N = N / i
if N > 1:
print N
Example — :
- → factor
- → factor
- → factor
- is prime → factor
So, .
Time complexity: .
Explain why prime factorization can be done by iterating only up to the square root of the number. Justify mathematically.
Key idea: To find prime factors of , it is sufficient to check divisors only up to .
Mathematical justification:
- Suppose where .
- Then because if both and were greater than , their product would exceed :
which is a contradiction. - Therefore, at least one factor of any factor pair is .
Consequence:
- Once we remove all prime factors , if the remaining value is greater than , it must itself be a prime (the single large factor).
Benefit: Reduces time complexity from to .
Example: For , we only test divisors up to . None divide , so it is prime.
Define K-jagged numbers. Explain their significance and give examples.
K-jagged numbers (also related to k-rough numbers) are numbers whose smallest prime factor is greater than or equal to a given value . In some competitive contexts, a number is called k-jagged if all its prime factors are .
Definition: A positive integer is k-rough (k-jagged) if every prime factor of satisfies .
Key points:
- is considered k-rough for any (vacuously, it has no prime factors).
- Larger means fewer numbers qualify.
Examples ():
- are 5-rough since their smallest prime factor .
- is not 5-rough (has factor ).
Significance: Used in sieve-based problems, number theory analysis, and generating sequences with restricted small factors.
Define Stormer numbers. Explain the mathematical condition that characterizes them.
Störmer numbers (or arc-cotangent irreducible numbers) are positive integers for which the greatest prime factor of is greater than or equal to .
Definition:
A positive integer is a Störmer number if:
where denotes the greatest prime factor of .
Examples:
- : , greatest prime factor ✓ → Störmer number
- : , greatest prime factor ✓ → Störmer number
- : , greatest prime factor ✓
- : , greatest prime factor ? No → not a Störmer number
Significance: They are used in the theory of continued-fraction approximations of using Machin-like formulas.
Define a Frugal number and explain the condition using digit counts. Verify whether is a frugal number.
A Frugal number (also called an economical number) is a number that has more digits than the number of digits required to write its prime factorization (including exponents greater than ).
Condition: A number is frugal if:
where counts the digits, and the prime factorization is written with exponents (exponent omitted).
Example — :
- Number of digits in .
- Prime factorization: .
- Digits used in factorization: and → digits.
- Since , is a frugal number. ✓
Another Example — :
- Digits in .
- Factorization: → digits: and → digits.
- Since , is frugal.
Note: Frugal numbers are relatively rare and are also known as economical numbers.
Define P-smooth numbers. Describe how to find all P-smooth numbers in a given range .
A P-smooth number is a positive integer whose largest prime factor is less than or equal to .
Definition: An integer is P-smooth if every prime factor of satisfies:
Examples ():
- → 5-smooth ✓
- → 5-smooth ✓
- → not 5-smooth (has factor )
Algorithm to find P-smooth numbers in :
- For each number in :
- Find its largest prime factor by trial division.
- If the largest prime factor , mark as P-smooth.
Efficient sieve approach:
- Use a modified sieve: for each number, repeatedly divide out primes ; if the result becomes , the number is P-smooth.
Applications: Cryptography (factorization algorithms like Pollard's ), number theory, and analysis of algorithms.
State Lemoine's Conjecture and explain it with examples. How does it relate to Goldbach's conjecture?
Lemoine's Conjecture (also known as Levy's Conjecture) states that:
Every odd integer greater than can be expressed as the sum of a prime and twice a prime.
Mathematical form:
where and are primes and .
Examples:
- ; also
- ; also
Relation to Goldbach's Conjecture:
- Goldbach's (strong) conjecture states every even integer is the sum of two primes.
- Lemoine's conjecture can be seen as a strengthening related to odd numbers, and is a consequence-type refinement of Goldbach's ideas.
- Both remain unproven but verified computationally for large ranges.
Distinguish between the Basic Euclidean algorithm and the Extended Euclidean algorithm in terms of purpose, output, and applications.
Comparison of Basic vs Extended Euclidean Algorithm:
| Aspect | Basic Euclidean | Extended Euclidean |
|---|---|---|
| Purpose | Compute | Compute and coefficients |
| Output | A single value (GCD) | GCD plus such that |
| Equation | (Bézout's identity) | |
| Complexity | ||
| Applications | Simplifying fractions, GCD problems | Modular inverse, Diophantine equations, RSA, CRT |
Key difference:
- The basic version only tells us the GCD.
- The extended version additionally provides the linear combination expressing the GCD, which is essential for modular arithmetic and cryptography.
Example: For : Basic gives ; Extended gives with .
Explain how the Extended Euclidean algorithm is used to compute the modular multiplicative inverse. Find the inverse of modulo .
The modular multiplicative inverse of modulo is an integer such that:
It exists only if .
Using Extended Euclidean Algorithm:
- We solve .
- The coefficient (taken modulo ) is the modular inverse.
Example — inverse of modulo :
- Apply extended Euclidean to , :
- Back-substitution:
- So .
Verification: ✓
The modular inverse of is .
Derive the recursive relation used in the Extended Euclidean algorithm and explain how the coefficients are updated at each step.
Goal: Find such that .
Base case: When :
So , .
Recursive step: Suppose we have solved for the smaller subproblem:
Using the identity , substitute:
Rearranging:
Coefficient update rules:
Pseudocode:
extgcd(a, b):
if b == 0:
return (a, 1, 0)
(g, x1, y1) = extgcd(b, a % b)
x = y1
y = x1 - (a // b) * y1
return (g, x, y)
This produces both the GCD and Bézout coefficients in time.
Explain the Sieve of Eratosthenes for primality testing and how it can be adapted to find the smallest prime factor of every number up to .
The Sieve of Eratosthenes efficiently finds all primes up to .
Basic Sieve:
- Create a boolean array
isPrime[0..N]initialized totrue. - Mark and as not prime.
- For each from to : if
isPrime[i]is true, mark all multiples as not prime.
Time complexity: .
Smallest Prime Factor (SPF) Sieve:
To record the smallest prime factor of every number:
spf = array of size N+1, initialized to 0
for i = 2 to N:
if spf[i] == 0: // i is prime
for j = i to N step i:
if spf[j] == 0:
spf[j] = i
Usage: Once spf[] is built, any number can be factorized in by repeatedly dividing by spf[n].
Example: For : , , , . So .
Given the number , find its prime factorization, the total number of divisors, and the sum of divisors.
Step 1 — Prime factorization of :
So:
Step 2 — Total number of divisors:
Step 3 — Sum of divisors (using the formula ):
Summary:
- Prime factorization:
- Number of divisors: 30
- Sum of divisors: 2418
Compare K-jagged (k-rough) numbers and P-smooth numbers. Highlight their differences with examples.
Both concepts classify numbers based on the size of their prime factors, but from opposite directions.
| Aspect | K-jagged (k-rough) | P-smooth |
|---|---|---|
| Condition | Smallest prime factor | Largest prime factor |
| Restricts | Small prime factors | Large prime factors |
| Example ( / ) | ||
| Excludes | Numbers with small factors () | Numbers with large factors () |
Detailed examples:
- 5-rough: (smallest factor ) ✓; ✗ (factor )
- 5-smooth: (largest factor ) ✓; ✗ (factor )
Key insight:
- Rough numbers avoid small primes.
- Smooth numbers avoid large primes.
- They are essentially complementary notions in number theory, both widely used in sieve algorithms and cryptographic factorization.
Write an algorithm to determine whether a given number is prime using the method. Explain the optimizations used.
Primality testing determines whether is prime.
Optimized Algorithm:
isPrime(n):
if n <= 1: return false
if n <= 3: return true
if n % 2 == 0 or n % 3 == 0: return false
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return false
i = i + 6
return true
Optimizations explained:
- Check up to only: Any factor larger than pairs with a smaller factor, so testing beyond is redundant.
- Handle and separately: Then skip all even numbers and multiples of .
- 6k ± 1 rule: All primes greater than are of the form . So we test only and , incrementing by .
Time complexity: with a small constant factor.
Example: For : test (), , ... up to ; none divide , so is prime.
Verify whether is a Störmer number and check the first few Störmer numbers. Explain the verification process.
Recall: is a Störmer number if the greatest prime factor .
Verification for :
- Compute .
- Factorize: .
- Greatest prime factor .
- Check: ? Yes ✓
So is a Störmer number.
Checking the first few numbers:
| Factorization | Störmer? | ||||
|---|---|---|---|---|---|
| 1 | 2 | 2 | 2 | 2 | ✓ |
| 2 | 5 | 5 | 5 | 4 | ✓ |
| 3 | 10 | 2×5 | 5 | 6 | ✗ |
| 4 | 17 | 17 | 17 | 8 | ✓ |
| 5 | 26 | 2×13 | 13 | 10 | ✓ |
| 6 | 37 | 37 | 37 | 12 | ✓ |
| 7 | 50 | 2×5² | 5 | 14 | ✗ |
First few Störmer numbers:
Solve the following GCD-based problem: Find the GCD of an array and explain the property that allows extending GCD to multiple numbers.
Property: The GCD of multiple numbers can be computed iteratively using the associative property:
This works because the set of common divisors of all numbers is preserved when computing pairwise GCDs progressively.
Solution for :
- :
- :
- :
Result: .
Algorithm:
result = arr[0]
for i = 1 to n-1:
result = gcd(result, arr[i])
if result == 1: break // optimization
return result
Optimization: If the running GCD becomes , we can stop early since divides everything.
Explain the relationship and use it to compute the LCM of and . Discuss its use in solving problems.
Fundamental relationship: For any two positive integers and :
Reasoning: Using prime factorizations, GCD takes the minimum exponent of each prime and LCM takes the maximum. For each prime, sum of the two exponents, which reconstructs the product .
Computing LCM of and :
- First find :
- Apply the formula:
Verification via factorization:
- ,
- LCM ✓
Use in problems:
- Efficient LCM computation avoids costly factorization.
- To avoid overflow, compute as .
- Widely used in problems on cycles, scheduling, and fractions.
Explain the Basic Euclidean algorithm for computing the GCD of two numbers. Illustrate with an example of finding .
The Euclidean algorithm is an efficient method to compute the Greatest Common Divisor (GCD) of two integers.
Principle: It is based on the property that:
The process repeats until the remainder becomes ; the last non-zero remainder is the GCD.
Steps:
- Divide the larger number by the smaller.
- Replace the larger number with the smaller, and the smaller with the remainder.
- Repeat until remainder is .
Example — :
The last non-zero remainder is , so .
Time complexity: , which makes it very efficient.
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →