190404_Py_example6 (the area of a circle)

16 篇文章 0 订阅
12 篇文章 0 订阅

Python_example 6


program_1 calculate the area of a circle

  1. method_1_principle 在这里插入图片描述
  2. method_2_principle
    在这里插入图片描述在这里插入图片描述

conclusion :
1.math method compare to computer mind
2.use func perf_counter to analyze the code’s time-performance simply


  • method_1_code
#Prog_2 code of method 1 
pi1=0
cycletime=eval(input("please input your cycle times:"))
start1=perf_counter()
for i in range(cycletime+1):
    pi1 += 1/pow(16,i)*( 4/(8*i+1)- 2/(8*i+4) -\
                      1/(8*i+5) - 1/(8*i+6))
print("pi(1) is :{0} \nthe running time with method 1 is :{1:.5f}s".format(pi1,perf_counter()-start1))
  • method_2_code
##Program_1 calculate the area of a circle 
# signal "\" in python means change the line
#method2 
from random import random 
from time import perf_counter
darts=1000*1000
hits=0.0#why?
start=perf_counter()
for i in range(1,darts+1):
    x,y=random(),random()
    distance=pow(x**2+y**2,0.5)
    if distance <=1.0:
        #hits=hits+1
        hits+=1
pi=(hits/darts)*4
print("pi(2) is :{}".format(pi))
print("the running time with method 2 is :{:.5f}s".format(perf_counter()-start))

running result

pi(2) is :3.139604
the running time with method 2 is >:3.03336s
please input your cycle times:1000
pi(1) is :3.141592653589793 
the running time with method 1 is >:0.01043s
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值