Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Nth multiple of a number in Fibonacci Series in Python
Next article icon

Nth multiple of a number in Fibonacci Series in Python

Last Updated : 28 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Our task is to find the nth multiple of a number in the Fibonacci series which involves identifying Fibonacci numbers that are exactly divisible by a given number m. This means we will check each Fibonacci number one by one and select only those that are multiples of m. Once we find the nth such number, we stop and return it. For example, if n = 2 and m = 3, we need to find the second Fibonacci number that is divisible by 3. Looking at the sequence, the numbers that are multiples of 3 are 3, 21, 144, 987, .... The second such number is 21.

Using iterative approach

The iterative approach generates Fibonacci numbers sequentially and checks for divisibility by m. The method maintains a count of Fibonacci numbers that are divisible by m, stopping when the nth multiple is found. Since it only computes Fibonacci numbers as needed, it avoids unnecessary computations, making it optimal for large values.

Python
n, m = 4, 3   a, b, count = 0, 1, 0    while True:     a, b = b, a + b  # Compute next Fibonacci number          if b % m == 0:  # Check if divisible by m         count += 1                    if count == n:  # If nth multiple is found, print it             print(b)             break 

Output
987 

Explanation:

  • We initialize a = 0 and b = 1 as the first two Fibonacci numbers.
  • The loop generates Fibonacci numbers using a, b = b, a + b.
  • It checks if b is divisible by m. If yes, it increments count.
  • When count == n, we print b and stop the loop.

Table of Content

  • Using dynamic programming appproch
  • recursive approach with memorization

Using dynamic programming appproch

The dynamic programming method optimizes Fibonacci sequence computation by storing previously computed values in a list. This avoids redundant calculations and reduces the time complexity significantly compared to the recursive approach. The idea is to maintain a list of Fibonacci numbers and iterate through it to find the nth occurrence of a multiple of m.

Python
def fun(n, m):     fib = [0, 1]       count = 0  # Track multiples of m          while True:         fib.append(fib[-1] + fib[-2])  # Generate next Fibonacci         if fib[-1] % m == 0:              count += 1               if count == n:                  return fib[-1]    n, m = 4, 3  # 4th Fibonacci multiple of 3 print(fun(n,m)) 

Output
987 

Explanation: This approach initializes a list fib with the first two Fibonacci numbers. It then enters a loop, continuously appending the sum of the last two elements to generate the Fibonacci sequence. Each new Fibonacci number is checked for divisibility by m. If a multiple is found, the counter increments. When the counter reaches n, the function returns the required Fibonacci number.

Recursive approach with memorization

Recursive approach calculates Fibonacci numbers using a recursive function, enhanced with memorization to store already computed values. While this approach is conceptually straightforward, it is less efficient than iterative and dynamic programming methods because recursive calls introduce function call overhead.

Python
def fib(n, memo={0: 0, 1: 1}):     if n not in memo:         memo[n] = fib(n - 1, memo) + fib(n - 2, memo)  # Compute & store     return memo[n]  def fun(n, m):     count, idx = 0, 2  # Counter & index starting from the third Fibonacci number          while True:         value = fib(idx)  # Compute Fibonacci number                  if value % m == 0:              count += 1             if count == n:                   return value                    idx += 1  # Move to the next index  n, m = 4, 3  # 4th Fibonacci multiple of 3 print(fun(n, m)) 

Output
987 

Explanation: recursive fib(n) function to compute Fibonacci numbers, storing results in a dictionary (memo) to avoid redundant calculations. The main function initializes a counter and an index (idx = 2, starting from the third Fibonacci number). It iterates through Fibonacci numbers, checking for divisibility by m. If a multiple is found, the counter increments. When the counter reaches n, the function returns the Fibonacci number.


Next Article
Nth multiple of a number in Fibonacci Series in Python

K

kartik
Improve
Article Tags :
  • Python
Practice Tags :
  • python

Similar Reads

    How to Check if a Given Number is Fibonacci number - Python
    Fibonacci numbers are part of a famous sequence where each number is the sum of the two preceding ones, i.e. F(n) = F(n-1) + F(n-2). The sequence starts as:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...Notice that every number is equal to the sum of its previous 2 numbers. In this article, we will learn how t
    2 min read
    Find the position of number that is multiple of certain number
    In this type of question, a number is given and we have to find the position or index of all multiples of that number. For doing this question we use a function named numpy.argwhere(). Syntax: numpy.argwhere(array) Example 1:  Sometimes we need to find the indexes of the element that are divisible b
    1 min read
    Python | sympy.fibonacci() method
    With the help of sympy.fibonacci() method, we can find the Fibonacci number and Fibonacci polynomial in SymPy. fibonacci(n) - The Fibonacci numbers are the integer sequence defined by the initial terms F_0 = 0 , F_1 = 1 and the two-term recurrence relation F_n = F_{n-1} + F_{n-2} . Syntax: fibonacci
    2 min read
    NumPy - Fibonacci Series using Binet Formula
    All of us are familiar with Fibonacci Series. Each number in the sequence is the sum of the two numbers that precede it. So, the sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...... In this tutorial, we will implement the same using NumPy with the aid of Binet formula. Binet Formula fn = \left [\lef
    3 min read
    Print first m multiples of n without using any loop in Python
    While loops are the traditional way to print first m multiples of n, Python provides other efficient methods to perform this task without explicitly using loops. This article will explore different approaches to Printing first m multiples of n without using any loop in Python.Using List Comprehensio
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences