Python Program for Finding the n’th Multiple in Fibonacci Series
Introduction
In this blog post, we will explore a Python program to find the n’th multiple of a given number in the Fibonacci series. The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones. By extending this series, we can create a sequence of multiples of a specified number.
Problem Statement
Given a number m
and an index n
, we want to find the n’th multiple of m
in the Fibonacci series. For example, if m
is 3, and n
is 4, the program should find the 4th multiple of 3 in the Fibonacci series.
Python Program
def fibonacci_multiple(m, n):
fib_series = [0, 1]
while len(fib_series) <= n:
fib_series.append(fib_series[-1] + fib_series[-2])
multiple_count = 0
index = 0
while multiple_count < n:
if fib_series[index] % m == 0:
multiple_count += 1
index += 1
return fib_series[index - 1]
# Example Usage
m_value = 3
n_value = 4
result = fibonacci_multiple(m_value, n_value)
print(f"The {n_value}th multiple of {m_value} in the Fibonacci series is: {result}")
Program Explanation
We start by generating the Fibonacci series until its length becomes greater than or equal to
n
. We initialize the series with the first two elements, 0 and 1.We then iterate through the Fibonacci series to find the n’th multiple of the given number
m
. Themultiple_count
variable keeps track of the multiples found, and theindex
variable represents the current index in the series.The loop continues until we find the n’th multiple, and the result is the value at the found index in the Fibonacci series.
Example Output
For the example with m_value = 3
and n_value = 4
, the output will be:
The 4th multiple of 3 in the Fibonacci series is: 21
Concluded
This Python program efficiently finds the n’th multiple of a given number m
in the Fibonacci series. It showcases the use of the Fibonacci series generation and how to identify multiples within the series. Understanding such algorithms helps build a strong foundation for problem-solving in programming.
As an extension, readers can explore variations of this program, such as finding multiples in different sequences or optimizing the algorithm for large values of n
. Additionally, experimenting with different input values will enhance your understanding of the program’s behavior. Happy coding!
One Response
[…] Python Program for n’th multiple of a number in Fibonacci Series […]