980404453 发表于 2019-3-28 23:26:14

求全部的代码

Description
写一个判断素数的函数,在主函数输入一个整数,输出是否是素数的消息。

Input
一个数(<=1000)

Output
如果是素数输出prime 如果不是输出not prime




Description
求π的近似值,计算公式为π/4=1-1/3+1/5-1/7+...,直到当前项的绝对值恰好小于(10^-n)为止(该项不计入总和)。

Input
输入n。(1<=n<=8)

多组测试数据。

Output
输出π的值。



杨扬阳羊洋 发表于 2019-3-31 10:38:35

def prime(n):
    flag = 1
    for i in range(2,int(n/2)):
      if n % i == 0 :
            flag = 0
      return flag


import prime_judge
n = input ("input an integer less than 1000:")
n = int(n)
if n <= 1000:
    if prime_judge.prime(n):
      print ("%d is a prime"%(n))
    else :
      print ("%d is not a prime"%(n))
else:
    print("%d is bigger than 1000"%(n))

杨扬阳羊洋 发表于 2019-3-31 12:19:41

求π(pi)的近似值看成求n的近似值,搞了好久都不对啊,很难受。改成求pi的近似值很快就算出来了。以下是代码
n = input ("input an number n that 1 <= n <= 8 :")
n = int(n)
pi = 1
i = 1
j = 1
while abs(1/i) >= 10**(-n):
    i += 2
    j += 1
    if j % 2 == 0 :   
      pi -= 1/i
    else :
      pi += 1/i
pi = pi * 4
print(pi)
print(j)

顺便把测试结果也发一下好了
=========== RESTART: F:/python3/file cord/Approximate summation.py ===========
input an number n that 1 <= n <= 8 :1
2.9760461760461765
6
>>>
=========== RESTART: F:/python3/file cord/Approximate summation.py ===========
input an number n that 1 <= n <= 8 :2
3.1611986129870506
51
>>>
=========== RESTART: F:/python3/file cord/Approximate summation.py ===========
input an number n that 1 <= n <= 8 :3
3.143588659585789
501
>>>
=========== RESTART: F:/python3/file cord/Approximate summation.py ===========
input an number n that 1 <= n <= 8 :4
3.1417926135957908
5001
>>>
=========== RESTART: F:/python3/file cord/Approximate summation.py ===========
input an number n that 1 <= n <= 8 :5
3.141612653189785
50001
>>>
=========== RESTART: F:/python3/file cord/Approximate summation.py ===========
input an number n that 1 <= n <= 8 :6
3.1415946535856922
500001
>>>
=========== RESTART: F:/python3/file cord/Approximate summation.py ===========
input an number n that 1 <= n <= 8 :7
3.1415928535897395
5000001
>>>
=========== RESTART: F:/python3/file cord/Approximate summation.py ===========
input an number n that 1 <= n <= 8 :8
3.1415926735902504
50000001
>>>

980404453 发表于 2019-4-11 11:17:38

杨扬阳羊洋 发表于 2019-3-31 12:19
求π(pi)的近似值看成求n的近似值,搞了好久都不对啊,很难受。改成求pi的近似值很快就算出来了。以下是 ...

感谢

980404453 发表于 2019-4-11 11:18:09

杨扬阳羊洋 发表于 2019-3-31 10:38


感谢 大佬
页: [1]
查看完整版本: 求全部的代码