鱼C论坛

 找回密码
 立即注册
查看: 2568|回复: 4

求全部的代码

[复制链接]
发表于 2019-3-28 23:26:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

Input
一个数(<=1000)

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




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

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

多组测试数据。

Output
输出π的值。



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-31 10:38:35 | 显示全部楼层
  1. def prime(n):
  2.     flag = 1
  3.     for i in range(2,int(n/2)):
  4.         if n % i == 0 :
  5.             flag = 0
  6.         return flag
复制代码

  1. import prime_judge
  2. n = input ("input an integer less than 1000:")
  3. n = int(n)
  4. if n <= 1000:
  5.     if prime_judge.prime(n):
  6.         print ("%d is a prime"%(n))
  7.     else :
  8.         print ("%d is not a prime"%(n))
  9. else:
  10.     print("%d is bigger than 1000"%(n))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-31 12:19:41 | 显示全部楼层
求π(pi)的近似值看成求n的近似值,搞了好久都不对啊,很难受。改成求pi的近似值很快就算出来了。以下是代码
  1. n = input ("input an number n that 1 <= n <= 8 :")
  2. n = int(n)
  3. pi = 1
  4. i = 1
  5. j = 1
  6. while abs(1/i) >= 10**(-n):
  7.     i += 2
  8.     j += 1
  9.     if j % 2 == 0 :   
  10.         pi -= 1/i
  11.     else :
  12.         pi += 1/i
  13. pi = pi * 4
  14. print(pi)
  15. 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
>>>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-4-11 11:18:09 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-17 11:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表