Python implements the sum of fractional sequences

I will stop talking nonsense, just go to the code!

#! /usr/bin/env python
# coding:UTF-8"""
@ version: python3.x
@ author:Cao Xinjian
@ contact:[email protected]
@ software: PyCharm
@ file:1223.py
@ time:2018/12/2320:56"""
 
'''
There is a sequence of scores: 2/1,3/2,5/3,8/5,13/8,21/13...Find the sum of the first 20 terms of this sequence.
'''
"""
# method one
def g(n):if n <=2:return n
 else:returng(n-1)+g(n-2)
sum =0for i inrange(1,21):
 sum +=g(i+1)/g(i)print(sum)"""
 
# Method Two
numerator =2
denominator =1
sum =0while True:try:
 n =int(input("Please enter an integer:"))
 except ValueError:print("Input error, please enter an integer")else:for i inrange(n):
  sum += numerator / denominator
  numerator, denominator = numerator + denominator, numerator
 
 print(sum)break

Supplementary extension: Implementation of Python score addition

More or less everyone will find it troublesome to use a computer calculator to display scores, so it is excellent to use Python to add scores

a =input()
b = a.split(',')
 
def eu(a,b):if a < b:
 a, b = b, a
 r =1while r !=0:
 r = a % b
 a = b
 b = r
 return a
 
num1 = b[0].split('/')
num2 = b[1].split('/')
sum1 =int(num1[0])*int(num2[1])+int(num2[0])*int(num1[1])
sum2 =int(num1[1])*int(num2[1])
GCD =eu(sum1,sum2)
 
c =int(sum1/GCD)
d =int(sum2/GCD)if c%d ==0:print(int(c/d))else:print(str(c)+'/'+str(d))

In fact, the fractions module can solve the problem instantly

from fractions import Fraction
a,b =(input().split(','))
sum=Fraction(a)+Fraction(b)print(sum)

The above Python implementation of the sum of score sequences is all the content shared by the editor, I hope to give you a reference.

Recommended Posts

Python implements the sum of fractional sequences
Python implements the shuffling of the cards in Doudizhu
Python implements the source code of the snake game
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
Python implements the brick-and-mortar game
How Python implements the mail function
Python simply implements the snake game
Python handles the 4 wheels of Chinese
Python3 implements the singleton design pattern
Python simulation of the landlord deal
What is the use of Python
Python implements the steepest descent method
Python implements the actual banking system
The premise of Python string pooling
Secrets of the new features of Python 3.8
How Python implements the timer function
Python implements the aircraft war project
The father of Python joins Microsoft
Python implements horizontal stitching of pictures
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Python implements alternate execution of two threads
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
What is the scope of python variables
Two days of learning the basics of Python
What is the id function of python
Where is the pip path of python3
The essence of Python language: Itertools library
python implements the gradient method python the fastest descent method
What are the advantages of python language
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the prospect of python development
What is the function body of python
The specific method of python import library
python3 simply implements the combined design pattern
Solve the conflict of multiple versions of python
What is the function of adb in python
Detailed explanation of the principle of Python super() method
Python|Find the sum of factorials from 1 to n
The difference between the syntax of java and python
7 features of Python3.9
Python realizes the development of student management system
The meaning and usage of lists in python
Solve the problem of python running startup error
Can the value of the python dictionary be modified?
Detailed explanation of the usage of Python decimal module
How about learning python at the age of 27?
2.1 The Python Interpreter (python interpreter)
The consequences of uninstalling python in ubuntu, very
Python writes the game implementation of fishing master