pthon: 数列sequence of number

# encoding: utf-8
# 版权所有 2024 涂聚文有限公司
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:  数列: sequence of number
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : PyCharm 2024.3 python 3.11
# os        : windows 10
# database  : mysql 9.0 sql server 2019, poostgreSQL 17.0 oracle21C
# Datetime  : 2024/12/28 8:12
# User      : geovindu
# Product   : PyCharm
# Project   : Pysimple
# File      : mathdemo.py
# explain   : 学习

import math
import sys
import matplotlib.pyplot as plt
import numpy as np

def arithmeticSequence(start, step, length):
    """
    等差数列(arithmetic sequence),生成
   定义:等差数列是一种数列,其中任意两个相邻项的差是一个常数,这个常数被称为公差。设等差数列的首项为a 1 ,公差为d,则数列的通项公式为:a n =a 1 +(n−1)d
    :param start: a1
    :param step: d
    :param length: n
    :return:
    """
    return [start + step * i for i in range(length)]

def arithmeticsequence(a1, d, n):
    """
    等差数列(arithmetic sequence),生成
    :param a1:
    :param d:
    :param n:
    :return:
    """
    return [a1 + (i * d) for i in range(n)]

def arithmeticsum(a1, d, n):
    """
    求和  Arithmetic Sequence
    :param a1:
    :param d:
    :param n:
    :return:
    """
    return (n / 2) * (2 * a1 + (n - 1) * d)



def geometricSequence(start, ratio, length):
    """
     等比数列(geometric sequence)生成
    :param start:
    :param ratio:
    :param length:
    :return:
    """
    return [start * ratio for i in range(length)]

def geometricsequence(a1, r, n):
    """
    等比数列(geometric sequence) 生成
    :param a1:
    :param r:
    :param n:
    :return:
    """
    return [a1 * (r ** i) for i in range(n)]

def geometricsum(a1, r, n):
    """

    :param a1:
    :param r:
    :param n:
    :return:
    """
    if r == 1:
        return a1 * n
    return a1 * (1 - r ** n) / (1 - r)


def infinitegeometricsum(a1, r):
    """

    :param a1:
    :param r:
    :return:
    """
    if abs(r) >= 1:
        return float('inf')  # or raise an exception
    return a1 / (1 - r)

#Harmonic Sequence
#Formula: an= 1 / (a1 + (n−1) * d)
# 生成调和级数并计算其元素总和的函数
# 通过迭代实现
n = 5
AP = [0] * (n + 5)


def generateAP(a, d, n):
    """

    :param a:
    :param d:
    :param n:
    :return:
    """

    sum = 0
    for i in range(1, n + 1):

        # HP = 1/AP
        # In AP, ith term is calculated
        # by a+(i-1)d;
        AP[i] = (a + (i - 1) * d)

        # Calculating the sum.
        sum += float(1) / float((a + (i - 1) * d))
    return sum

# Function that uses riemann sum method to calculate
# the approximate sum of HP in O(1) time complexity


def sumApproximation(a, d, n):
    """

    :param a:
    :param d:
    :param n:
    :return:
    """
    return math.log((2 * a + (2 * n - 1) * d) /
                    (2 * a - d)) / d



#生成一个从1开始,公差为2,长度为10的等差数列
arithmeticseq = arithmeticSequence(1, 2, 10)
print(arithmeticseq)  # 输出: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
#生成一个从1开始,公比为2,长度为10的等比数列
geometricseq = geometricSequence(1, 2, 10)
print(geometricseq)  # 输出: [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]


#
a = 12
d = 12
#n = 5;

# 根据上述数据生成 AP
sum = generateAP(a, d, n)

#根据生成的 AP 产生 HP
print("Harmonic Progression :")
for i in range(1, n + 1):
    print("1 /", AP[i], end=" ")
print("")
str1 = ""
str1 = str1 + str(sum)
str1 = str1[0:4]

print("Sum of the generated harmonic",
      "progression :", str1)
sum = sumApproximation(a, d, n)

str1 = ""
str1 = str1 + str(sum)
str1 = str1[0:4]
print("Sum of the generated harmonic",
      "progression using approximation :", str1)



arrayexample = np.arange(1, 11)
squaresarray= [  1  , 4  , 9  ,16 , 25 , 36 , 49 , 64 , 81, 100]
#可视化数列
plt.plot(arrayexample, squaresarray)
plt.xlabel('Number')
plt.ylabel('Square')
plt.title('Number vs Square')
plt.show()

  

posted @ 2024-12-28 09:11  ®Geovin Du Dream Park™  阅读(20)  评论(0)    收藏  举报