solomonxian
发表于 2017-6-16 18:38:04
# 从前面往后面算
def product_sum(n):
product = 1
the_sum = 0
k,setting = 1,1# 控制第k个加法项
for i in range(1,n+1):
product *= i
# 乘法项内且没到最后一个数
if k < setting and i != n:
k += 1
# 到加法项或者循环最后一轮
else:
the_sum += product
# 为下一项乘积初始化 product 和 k,乘积因数个数 setting+1
product = 1
k = 1
setting += 1
print(the_sum)
张大象
发表于 2017-9-19 14:45:10
def sum_num(n):
L =
c = 1
result = 0
str1 = ''
while len(L)>0:
tmp = 1
L1 = []
for i in range(c):
if len(L)> 0:
L1.append(L)
tmp *= L.pop(0)
str1 += '*'.join()
str1 += '+'
result += tmp
c += 1
print(str1,'=',result,sep='')
sum_num(16)
十梦九妳
发表于 2017-9-19 15:21:45
求教~
renze
发表于 2017-9-19 15:35:16
a = input()
result = 0
num = 0
total = 1
for num in range(int(a)):
result = result + total - 1
if num > int(a) : break
total = num
for count in range(num):
total = total * num
num += 1
if num > int(a) : break
print(result)
renze
发表于 2017-9-19 16:18:20
a = input()
result = 1
num = 1
total = 0
for sha in range(1,int(a)+1):
result = result + total
num += 1
if num > int(a) : break
total = num
for count in range(sha):
num += 1
if num > int(a) : break
total = total * num
print(result)
流浪者python
发表于 2017-12-10 15:27:22
{:5_91:}
shigure_takimi
发表于 2017-12-10 19:38:20
本帖最后由 shigure_takimi 于 2017-12-11 10:52 编辑
def fun(n):
symbols = ''
for i in range(1,n):
symbols += '+'+'*'*i
if len(symbols) >= n:
break
if n <= 1:
return 'The parameter should larger than 1!'
else:
s = ''
for i in range(1,n):
s += str(i)+symbols
s += str(n)
#print(s+'='+str(eval(s)))
return str(eval(s))
print(fun(200000)) # 1.2 秒
print(fun(2000000))# 46 秒
print(fun(3000000))# 104 秒
超级乐乐gogogo
发表于 2017-12-11 10:03:03
回复
kwty
发表于 2018-2-7 20:07:12
本帖最后由 kwty 于 2018-2-7 20:11 编辑
{:5_90:}
victor.xu
发表于 2018-2-9 14:44:09
本帖最后由 victor.xu 于 2018-2-10 09:35 编辑
def func(n):
lis = list(range(1,n+1))
k = 1
lis1 = []
while len(lis) > k:
lis_t = lis
del(lis)
lis1.append(lis_t)
k += 1
if len(lis) <= k and len(lis) != 0:
lis1.append(lis)
return lis1
num = int(input('请输入自然数:\n'))
result = func(num)
print(result)
#列表中各元素乘积形成的列表
result_m = []
#列表求和
summ = 0
for i in result:
#列表中各元素乘积
result_i = 1
for j in i:
result_i *= j
result_m.append(result_i)
for s in result_m:
summ += s
print(summ)
大头目
发表于 2018-4-4 12:01:40
def nd(n):
list1 = []
list2 = []
t = 1
b = 1
for i in range(1,n+1):
if b != 0:
list1.append(i)
b -= 1
else:
list2.append(list1)
list1 = []
t += 1
list1.append(i)
b = t - 1
if i == n:
list2.append(list1)
print(list2)
return list2
def out(list2):
list3 = []
for each in list2:
x = 1
for j in each:
x *= j
list3.append(x)
print(list3)
return sum(list3)
print(out(nd(12)))
小庆子
发表于 2018-5-16 23:05:32
本帖最后由 小庆子 于 2018-5-16 23:09 编辑
def numb(n):
num = []
for i in range(1,n+1):
num.append(i) #将所有数字添加到一个列表中,并反相(方便后面的pop)
num.reverse()
times = 0
end = 0
while len(num) != 0: #判断是第多少项
#如果列表中没有数字了就跳出项
times += 1
time = times
result = 1
while time != 0 : #第多少项就有多少个数字相乘
if len(num) == 0: #如果列表中没有数字了就跳出乘
break
else:
result *= num.pop()
time -= 1
end += result
print(end)
n = int(input('有多少个数?:'))
numb(n)
萧丹夜
发表于 2018-5-24 18:29:09
云里雾里
k = int(input('请输入加法项个数:'))
x = list(range(1,k+1))
m = 1
sumx = 0
i = 0
while i < k:
pro = 1
n = 1
while n <= m :
pro *= x
n += 1
i += 1
if i == k:
break
m += 1
sumx += pro
print(sumx)
jrro452
发表于 2018-6-13 18:00:19
print('求:1+2*3+4*5*6+7*8*9*10+11*12*13*14*15+……+(n-k)*...*(n-1)*n的和。')
num = int(input('请输入一个正整数n:'))
if num==1:
print('计算结果为:1')
exit()
elif num==2:
print('计算结果为:1+2=3')
exit()
else:
Nums =
abd = []
b = 1
for i in range(num):
if i==0:
abd.append(Nums)
elif i+b<num:
abd.append('*'.join(Nums))
else:
abd.append('*'.join(Nums))
break
b+=i
b+=1
result ='+'.join(abd)
print('输入的结果是:')
print(result+'='+str(eval(result)))
输入20的结果:
求:1+2*3+4*5*6+7*8*9*10+11*12*13*14*15+……+(n-k)*...*(n-1)*n的和。
请输入一个正整数n:20
输入的结果是:
1+2*3+4*5*6+7*8*9*10+11*12*13*14*15+16*17*18*19*20=2226007
jrro452
发表于 2018-6-14 17:32:34
jrro452 发表于 2018-6-13 18:00
输入20的结果:
求:1+2*3+4*5*6+7*8*9*10+11*12*13*14*15+……+(n-k)*...*(n-1)*n的和。
请输入一个正 ...
突然发现前面几段代码都是无用的····修改如下:
import time
print('求:1+2*3+4*5*6+7*8*9*10+11*12*13*14*15+……+(n-k)*...*(n-1)*n的和。')
num = int(input('请输入一个正整数n:'))
starttime = time.time()
Nums =
abd = []
b = 1
for i in range(num):
if i==0:
abd.append(Nums)
elif i+b<num:
abd.append('*'.join(Nums))
else:
abd.append('*'.join(Nums))
break
b+=i
b+=1
result ='+'.join(abd)
print('输入的结果是:')
print(eval(result))
小强工作室
发表于 2018-8-1 11:26:06
学习一下
lyslyslys
发表于 2018-9-25 09:16:58
感谢
抬起头~
发表于 2018-10-8 21:26:15
加油
double-lee
发表于 2018-12-10 10:36:43
def nums(n):
x = 1
m = n+1
while n:
for i in range(x,m):
n -= i
x += 1
if n < 0 or n == 0:
return i
continue
# m 是nusm(n)的返回值
def add(n,m):
if m == 1:
return 1
if m > 1:
sum = 1
x = 0
# m项前一共有几个数
for i in range(1,m):
x += i
for i in range(x+1,n+1):
sum *= i
n = x
m -= 1
return sum+add(n,m)
while 1:
n = int(input('正整数:'))
print(add(n,nums(n)))
北城以北灬
发表于 2019-3-18 10:52:14
n = int(input("n:"))
x = 1
result = 0
for i in range(1, 1000000):
y = 1
for j in range(i):
y = y * x
x += 1
if x > n:
break
result += y
if x > n:
print(result)
break