pthon: 数列sequence of number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# 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 @   ®Geovin Du Dream Park™  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2021-12-28 sql: paging in SQL Server
2015-12-28 csharp: json to csharp
2014-12-28 csharp:Learn how to post JSON string to generic Handler using jQuery in ASP.Net C#.
2012-12-28 Csharp: listview control binding database from datatable
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示