Python

 

Introduction

Sequences are an important aspect of mathematics and computer science.
Among the most common types of sequences are the Fibonacci sequence and arithmetic progression (AP).
In this program, we will analyze these sequences using Python, and allow users to explore the patterns and behavior of these number series.

The objective of this program is to help users generate and analyze both Fibonacci and Arithmetic Progression sequences, and understand how these sequences evolve mathematically.

Python Code for Number Sequence Analyzer


def generate_fibonacci(n):
    fibonacci_sequence = [0, 1]
    for i in range(2, n):
        next_number = fibonacci_sequence[-1] + fibonacci_sequence[-2]
        fibonacci_sequence.append(next_number)
    return fibonacci_sequence

def generate_arithmetic_progression(start, step, n):
    ap_sequence = [start + i * step for i in range(n)]
    return ap_sequence

def main():
    print("Number Sequence Analyzer")
    print("1. Fibonacci Sequence")
    print("2. Arithmetic Progression Sequence")
    choice = int(input("Select sequence type (1/2): "))

    if choice == 1:
        n = int(input("Enter the number of Fibonacci terms you want to generate: "))
        fibonacci = generate_fibonacci(n)
        print("Fibonacci Sequence:", fibonacci)

    elif choice == 2:
        start = int(input("Enter the starting number: "))
        step = int(input("Enter the common difference: "))
        n = int(input("Enter the number of terms you want to generate: "))
        ap = generate_arithmetic_progression(start, step, n)
        print("Arithmetic Progression Sequence:", ap)

    else:
        print("Invalid choice. Please select either 1 or 2.")

if __name__ == "__main__":
    main()
            

Explanation of Program Structure

The program is structured with two main functions:

  • generate_fibonacci(n): This function generates the Fibonacci sequence up to the nth term. It initializes the sequence with the first two terms [0, 1] and iteratively computes the subsequent terms.
  • generate_arithmetic_progression(start, step, n): This function generates the Arithmetic Progression (AP) sequence starting from a given number, with a specified common difference, for n terms.

The main function first presents the user with a choice of which sequence to generate. Based on the user’s choice, it calls the appropriate function to generate and display the sequence. The user can input the number of terms or the parameters (starting number and common difference) for the Arithmetic Progression.

The program is designed to handle basic input validation for valid choices and terms for sequence generation.

How to Run the Program

To run the program, you will need Python installed on your computer. Follow these steps:

  1. Install Python from the official website: https://www.python.org.
  2. Open your preferred code editor or IDE (e.g., VS Code, PyCharm, etc.).
  3. Create a new Python file (e.g., number_sequence_analyzer.py) and copy the code provided above into this file.
  4. Run the Python file using the command line or your IDE’s “Run” button.
  5. The program will prompt you to select either Fibonacci or Arithmetic Progression and provide the necessary inputs.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)