Sum of a Special Number Series

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to calculate the sum of the first N (N >= 6) terms of a special number series. This series is defined by the following pattern:

\frac{2}{1} + \frac{3}{2} + \frac{5}{3} + \frac{8}{5} + \frac{13}{8} + \frac{21}{13} + ...

The numerators of this series are the Fibonacci numbers (2, 3, 5, 8, 13, 21, ...), and the denominators are also the Fibonacci numbers (1, 2, 3, 5, 8, 13, ...).

👀 Preview

$ python3 sum_fib.py
Enter the value of n: 6
Sum of the special series: 10.00705

$ python3 sum_fib.py
Enter the value of n: 20
Sum of the special series: 32.66026

$ python3 sum_fib.py
Enter the value of n: 45
Sum of the special series: 73.11111

🎯 Tasks

In this project, you will learn:

  • How to understand the problem statement and requirements for the project
  • How to implement the sum_fib function to calculate the sum of the first N terms of the number series
  • How to test the sum_fib function by running the sum_fib.py script
  • How to explain the logic behind the sum_fib function and the underlying Fibonacci number series

🏆 Achievements

After completing this project, you will be able to:

  • Understand and solve problems related to number series and mathematical sequences
  • Implement functions to perform calculations on complex number series
  • Test and validate your code to ensure it meets the project requirements
  • Explain the logic behind your solutions and the underlying mathematical concepts

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-302776{{"`Sum of a Special Number Series`"}} python/numeric_types -.-> lab-302776{{"`Sum of a Special Number Series`"}} python/type_conversion -.-> lab-302776{{"`Sum of a Special Number Series`"}} python/conditional_statements -.-> lab-302776{{"`Sum of a Special Number Series`"}} python/for_loops -.-> lab-302776{{"`Sum of a Special Number Series`"}} python/tuples -.-> lab-302776{{"`Sum of a Special Number Series`"}} python/function_definition -.-> lab-302776{{"`Sum of a Special Number Series`"}} python/build_in_functions -.-> lab-302776{{"`Sum of a Special Number Series`"}} end

Understand the Problem

In this step, you will learn about the problem statement and the requirements for the project.

The problem is to calculate the sum of the first N (N >= 6) terms of the following number series:

\frac{2}{1} + \frac{3}{2} + \frac{5}{3} + \frac{8}{5} + \frac{13}{8} + \frac{21}{13} + ...

The requirements are:

  1. Complete the sum_fib function in the sum_fib.py file to calculate the sum of the first N terms.
  2. The output result should be rounded to five decimal places.

Implement the sum_fib Function

In this step, you will implement the sum_fib function to calculate the sum of the first N terms of the given number series.

  1. Open the sum_fib.py file located in the /home/labex/project directory.
  2. Implement the sum_fib function as follows:
def sum_fib(n):
    if n <= 0:
        return 0.0

    numerator = 2
    denominator = 1
    total = 0.0

    for _ in range(n):
        total += numerator / denominator
        numerator, denominator = numerator + denominator, numerator

    return round(total, 5)

The sum_fib function takes an integer n as input, which represents the number of terms to be summed. The function initializes the numerator and denominator variables to the first two terms of the series, and then iterates n times, adding the ratio of the current numerator and denominator to the total variable. Finally, the function returns the rounded total to five decimal places.

Test the sum_fib Function

In this step, you will test the sum_fib function by running the sum_fib.py script and providing different values of n.

  1. At the end of the sum_fib.py file, add the following code to test the implementation:
if __name__ == "__main__":
    n = int(input("Enter the value of n: "))
    result = sum_fib(n)
    print("Sum of the special series:", result)
  1. Open a terminal and navigate to the /home/labex/project directory.
  2. Run the sum_fib.py script using the following command:
python3 sum_fib.py
  1. When prompted, enter the value of n (e.g., 6, 20, 45) and press Enter.
  2. The script will output the sum of the first n terms of the number series, rounded to five decimal places.

Example output:

Enter the value of n: 6
Sum of the special series: 10.00705

Enter the value of n: 20
Sum of the special series: 32.66026

Enter the value of n: 45
Sum of the special series: 73.11111

Verify that the output matches the expected results.

Congratulations! You have successfully completed the "Sum of a Special Number Series" project.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like