Numerical series

Save all functions into a file named numerical_series.py.

  1. A numerical series is defined as:

    \(x_1 = 1\)

    \(x_{i} = \begin{cases} x_{i-1} + 5 , & \mbox{if } i-1 \mbox{ is even} \\ 2x_{i-1} , & \mbox{if } i-1 \mbox{ is odd} \\ \end{cases}\)

    1. Write the function even_odd_1() that given an integer n, returns the average of the n first values of the defined numerical series. Examples:

    >>> round(even_odd_1(10), 2)
    48.3
    >>> round(even_odd_1(20), 2)
    913.2
    >>> round(even_odd_1(5), 2)
    8.6
    >>> round(even_odd_1(8), 2)
    26.25
    

    Note

    More tests are provided in file even_odd_1.txt

    2. Write the function even_odd_2() that given a value xmax, returns the average of the values of the defined numerical series, lower than xmax. Example:

    >>> round(even_odd_2(185), 2)
    48.3
    >>> round(even_odd_2(6135), 2)
    913.2
    >>> round(even_odd_2(20), 2)
    8.6
    >>> round(even_odd_2(90), 2)
    26.25
    

    Note

    More tests are provided in file even_odd_2.txt

  2. Given two integers a and b, such that a>0 and 0< b< 10, the following numerical series is defined:

    \(x_1 = a\)

    \(x_i = \begin{cases} 2x_{i-1}, & \mbox{if } x_{i-1} \mbox{ is multiple of b or its last digit is b} \\ 3x_{i-1} +b, & \mbox{if } x_{i-1} \mbox{ is not multiple of b and its last digit is not b}\\ \end{cases}\)

    1. Write the function sum_terms_1() that takes the values of a, b and n, and returns the addition of the n first terms of the given numerical series Examples:

    >>> sum_terms_1(1, 2, 10)
    59038
    >>> sum_terms_1(4, 3, 20)
    7864309
    >>> sum_terms_1(4, 5, 5)
    774
    >>> sum_terms_1(5, 1, 8)
    1275
    

    Note

    More tests are provided in file sum_terms_1.txt

    1. Write the function sum_terms_2() that takes the values of a, b and xmax, and returns the addition of the terms of the given numerical series, lower than xmax. Examples:

      >>> sum_terms_2(1, 2, 40000)
      59038
      >>> sum_terms_2(4, 3, 4000000)
      7864309
      >>> sum_terms_2(4, 5, 600)
      774
      >>> sum_terms_2(5, 1, 700)
      1275
      

      Note

      More tests are provided in file sum_terms_2.txt

Solution

A solution of these functions is provided in file numerical_series.py