AP in Python using function

Question:

Write a program that generates a series using a function which takes first and last values of the series and then generates four term that are equidistant. in python.

Asked By: Kushvah

||

Answers:

You can do that this way;
Also, you can specify how many terms you want in between first and last using the third argument.
Use simple mathematics.

def ap (arg1, arg2, d):
    diff = arg2 - arg1
    for i in range(d):
         yield arg1 + i*(diff/(d+1)) # division by d+1 

>>> ap (1, 6, 4))
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
Answered By: Vicrobot
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.