Python arithmetic sequence calculation method

Calculation of the last term of the arithmetic sequence

Subject content:

Given the first two terms a1 and a2 of an arithmetic sequence, find the nth term

You can use the following statement to achieve the input of a non-negative integer n:

n=int(input())

**Input format: **

Three lines, containing three integers a1, a2, n

**Output format: **

An integer, the value of the nth term

Input sample:

1
4
100

**Sample output: **

298

My answer

*Idea 1: ** Arithmetic sequence, first find the difference m, there are many ways to calculate the value of the nth term, I use this a1 + m(n-1)

a1 =int(input())
a2 =int(input())
m = a2 - a1
n =int(input())
N = a1 + m*(n-1)print(N)

**Idea 2: **Let's toss the computer, let the computer use the dumb method, start counting from a1, and calculate n-1 times

a1 =int(input())
a2 =int(input())
n =int(input())
m = a2 - a1
for i inrange(n-1):
 a1 += m
print(a1)

Supplementary knowledge: python judgment arithmetic sequence

Not much nonsense, let's just look at the code!

import sys
n =int(sys.stdin.readline().strip())
s = sys.stdin.readline()
s =list(map(int, s.split(' ')))print(n)print(s)for i inrange(len(s)-1):for j inrange(i+1,len(s)):if s[i]= s[j]:
  s[i], s[j]= s[j], s[i]for j inrange(1,len(s)-1):if s[j]- s[j-1]== s[j+1]- s[j]:
 flag =1else:
 flag =0if flag ==1:print('Possible')else:print('Impossible')

The calculation method of the last item of the above python arithmetic sequence is all the content shared by the editor, I hope to give you a reference.

Recommended Posts

Python arithmetic sequence calculation method
Python magic method topic
Python object-oriented magic method
Python3.7 debugging example method
Arithmetic operators in python
Python error handling method
Python defines a function method
Python TCP packet injection method
Python tracking except information method
Python implements gradient descent method
Python right alignment example method
Method steps to increase python font
Summary of logarithm method in Python
Python segmentation sequence diagram visualization program
Python and js interactive call method
Detailed explanation of python sequence types
Python implementation of IOU calculation case
Python calculation of information entropy example
Python implements the steepest descent method
End the method of running python