Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program for nth multiple of a number in Fibonacci Series
The Fibonacci series is a sequence where each number is the sum of the two preceding ones: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34... In this article, we'll find the position of the nth multiple of a given number k in the Fibonacci series.
Problem Statement
Given a number k and a value n, we need to find the position of the nth multiple of k in the Fibonacci series. For example, if k=4 and n=5, we find the 5th number in the Fibonacci series that is divisible by 4.
Understanding the Approach
We generate Fibonacci numbers sequentially and check if each number is divisible by k. When we find the nth such multiple, we return its position in the series.
Implementation
def find_nth_multiple(k, n):
f1, f2 = 0, 1
position = 1
count = 0
# Check if first number (0) is divisible by k
if f1 % k == 0:
count += 1
if count == n:
return position
position += 1
# Generate Fibonacci numbers and check for multiples
while count < n:
# Check current Fibonacci number
if f2 % k == 0:
count += 1
if count == n:
return position
# Generate next Fibonacci number
f1, f2 = f2, f1 + f2
position += 1
return position
# Example usage
k = 4 # Find multiples of 4
n = 5 # Find the 5th multiple
result = find_nth_multiple(k, n)
print(f"Position of {n}th multiple of {k} in Fibonacci Series: {result}")
Position of 5th multiple of 4 in Fibonacci Series: 30
Step-by-Step Example
Let's trace through finding the 2nd multiple of 3 in the Fibonacci series ?
def find_with_trace(k, n):
f1, f2 = 0, 1
position = 1
count = 0
print(f"Finding {n}th multiple of {k}:")
print("Position | Fibonacci | Multiple?")
print("-" * 30)
# Check first number
if f1 % k == 0:
count += 1
print(f"{position:8} | {f1:9} | Yes (count: {count})")
if count == n:
return position
else:
print(f"{position:8} | {f1:9} | No")
position += 1
# Generate and check subsequent numbers
while count < n:
if f2 % k == 0:
count += 1
print(f"{position:8} | {f2:9} | Yes (count: {count})")
if count == n:
return position
else:
print(f"{position:8} | {f2:9} | No")
f1, f2 = f2, f1 + f2
position += 1
return position
# Find 2nd multiple of 3
result = find_with_trace(3, 2)
print(f"\nResult: Position {result}")
Finding 2nd multiple of 3:
Position | Fibonacci | Multiple?
------------------------------
1 | 0 | Yes (count: 1)
2 | 1 | No
3 | 1 | No
4 | 2 | No
5 | 3 | Yes (count: 2)
Result: Position 5
Key Points
- The algorithm generates Fibonacci numbers sequentially
- It counts multiples of k as it encounters them
- Returns the position when the nth multiple is found
- Time complexity depends on how far the nth multiple appears in the series
Conclusion
This approach efficiently finds the position of the nth multiple of any number k in the Fibonacci series by generating the sequence and counting divisible numbers. The algorithm is straightforward and handles edge cases like k=1 where every Fibonacci number is a multiple.
