Horner's Rule and Rabin-Karp
Explain the Horner’s rule.
Horner’s Rule (or Horner’s Method) is an efficient algorithm used to evaluate polynomials at
a given value of x and to simplify polynomial expressions for computation. It reduces the
number of multiplications and additions, making it especially useful for computer-based
calculations.
Standard Polynomial Form
A polynomial of degree n is usually written as:
P (x) = an xn + an−1 xn−1 + … + a1 x + a0
Horner's Form
Horner’s Rule rewrites the polynomial in a nested form:
P (x) = (...((an x + an−1 )x + an−2 )x + … + a1 )x + a0
This form allows you to evaluate the polynomial with only n multiplications and n additions
(as opposed to the more expensive naive method).
Example
Let’s take a polynomial:
P (x) = 2x3 + 3x2 + x + 5
Naive Method (for x = 2):
P (2) = 2(2)3 + 3(2)2 + 1(2) + 5 = 16 + 12 + 2 + 5 = 35
Horner's Method:
Rewrite as:
P (x) = ((2x + 3)x + 1)x + 5
Now plug in x = 2:
((2 ⋅ 2 + 3) ⋅ 2 + 1) ⋅ 2 + 5 = ((4 + 3) ⋅ 2 + 1) ⋅ 2 + 5 = (7 ⋅ 2 + 1) ⋅ 2 + 5 = (14 + 1) ⋅ 2 + 5 =
Benefits
1/8
Faster computation
Fewer operations (especially multiplications)
Useful for implementations on digital computers and embedded systems
Applications
Polynomial evaluation
Efficient implementation in compilers and numerical libraries
Used in synthetic division
Let me know if you'd like a Python or C++ implementation example.
give python implementaion
Here's a simple Python implementation of Horner's Rule for evaluating a polynomial at a
given value of x:
🧠 Horner’s Rule in Python
python
def horner(coeffs, x):
"""
Evaluate a polynomial at x using Horner's Rule.
Parameters:
coeffs : list of coefficients (from highest degree to constant term)
x : the value at which to evaluate the polynomial
Returns:
The result of the polynomial evaluation
"""
result = 0
for coefficient in coeffs:
result = result * x + coefficient
return result
🧪 Example Usage
python
2/8
# Polynomial: 2x^3 + 3x^2 + 1x + 5
coefficients = [2, 3, 1, 5]
x_value = 2
print("Result:", horner(coefficients, x_value)) # Output should be 35
✅ Explanation
This code evaluates the polynomial:
P (x) = 2x3 + 3x2 + 1x + 5
at x = 2, using Horner’s method, giving the result: 35.
Let me know if you'd like to also calculate derivatives or use this for plotting.
Calculate the total number of spurious and valid hits in the
text T= 862346237846786127 when looking for the pattern
P=23, by Rabin-Karp String matcher. Assume the working
modulo q=11
To compute the total number of spurious and valid hits using the Rabin-Karp string
matching algorithm for:
Text T = 862346237846786127
Pattern P = 23
Modulo q = 11
We'll use the Rabin-Karp hashing method with modulo arithmetic and sliding window.
🔧 Step-by-Step Process
Let’s define:
Pattern P length = 2
We’ll look at every 2-digit substring of T
Convert all substrings and P to integer values, and compute hash modulo 11
3/8
✅ Step 1: Compute Hash of Pattern P = 23
Hash(P ) = 23 mod 11 = 1
✅ Step 2: Generate all 2-digit substrings of T
T = 862346237846786127
Length of T = 18
Number of 2-digit substrings = 18 − 2 + 1 = 17
Substrings:
i Substring Hash mod 11
0 86 86 % 11 = 9
1 62 62 % 11 = 7
2 23 23 % 11 = 1 ✅
3 34 34 % 11 = 1 ✅
4 46 46 % 11 = 2
5 62 62 % 11 = 7
6 23 23 % 11 = 1 ✅
7 37 37 % 11 = 4
8 78 78 % 11 = 1 ✅
9 84 84 % 11 = 7
10 46 46 % 11 = 2
11 67 67 % 11 = 1 ✅
12 78 78 % 11 = 1 ✅
13 86 86 % 11 = 9
4/8
i Substring Hash mod 11
14 61 61 % 11 = 6
15 12 12 % 11 = 1 ✅
16 27 27 % 11 = 5
✅ Step 3: Compare hashes with pattern hash = 1
We see matches (same hash) at indices:
i = 2 → substring = 23 → valid hit
i = 3 → substring = 34 → spurious hit
i = 6 → substring = 23 → valid hit
i = 8 → substring = 78 → spurious hit
i = 11 → substring = 67 → spurious hit
i = 12 → substring = 78 → spurious hit
i = 15 → substring = 12 → spurious hit
✅ Final Answer
Valid hits: 2 (at i = 2 and i = 6)
Spurious hits: 5 (at i = 3, 8, 11, 12, 15)
Total matches (hash matches): 7
✔️ Answer:
Valid Hits = 2
Spurious Hits = 5
5/8
Calculate the total number of spurious and valid hits in the
text T= 78612 786787782346237846 when looking for the
pattern P=86, by Rabin-Karp String matcher. Assume the
working modulo q=11.
Let's solve the problem using the Rabin-Karp string matching algorithm.
Given:
Text T = 78612786787782346237846
Pattern P = 86
Modulo q = 11
Step 1: Pattern length and hash
Length of pattern m =2
Compute the hash of the pattern P = 86:
Hash(P ) = 86 mod 11 = 9
Step 2: Generate all 2-digit substrings of T
The length of T is 21.
Number of 2-digit substrings = 21 − 2 + 1 = 20
We will check each 2-digit substring, compute its hash mod 11, and compare it with the
pattern hash (which is 9).
Step 3: Slide through the text
6/8
i Substring Hash mod 11 Match Type
0 78 78 % 11 = 1 No
1 86 86 % 11 = 9 ✅ Valid
2 61 61 % 11 = 6 No
3 12 12 % 11 = 1 No
4 27 27 % 11 = 5 No
5 78 78 % 11 = 1 No
6 86 86 % 11 = 9 ✅ Valid
7 67 67 % 11 = 1 No
8 78 78 % 11 = 1 No
9 87 87 % 11 = 10 No
10 77 77 % 11 = 0 No
11 78 78 % 11 = 1 No
12 82 82 % 11 = 5 No
13 23 23 % 11 = 1 No
14 34 34 % 11 = 1 No
15 46 46 % 11 = 2 No
16 62 62 % 11 = 7 No
17 23 23 % 11 = 1 No
18 78 78 % 11 = 1 No
19 84 84 % 11 = 7 No
7/8
Step 4: Identify valid vs spurious hits
Hash matches (i.e., hash ≡ 9 mod 11): only at i = 1 and i = 6
Substrings at these positions:
i = 1 → "86" → ✅ valid
i = 6 → "86" → ✅ valid
So both matches are valid.
✅ Final Answer
Valid Hits = 2
Spurious Hits = 0
Total Matches (hash matches) = 2
Let me know if you’d like code to automate this!
8/8