其他分享
首页 > 其他分享> > 圆周率中包含你的生日吗

圆周率中包含你的生日吗

作者:互联网

圆周率中包含你的生日吗

我一直想知道自己的生日是否包含在圆周率中。为此我们可将圆周率的前1000000位保存起来,用自己生日去匹配这前1000000位,就可得到想要的结果。以上方式用 Python 实现比较简洁,源码如下:

with open('pi_million_digits.txt') as flie_object:
    lines=flie_object.readlines()

pi_string=''
for line in lines:
    pi_string+=line.strip()

birthday=input("Enter your birthday, in the from mmddyy:")
if birthday in pi_string:
    print('Your brithday appers in the frist million digits of pi!')
else:
    print('Your brithday does not apper in the frist million digits of pi.')

这里我们将圆周率前100万位保存到一个叫 pi_million_digits.txt 的文本文件中,然后让Python读取它,与用户输入的字符串进行匹配,并给出匹配结果。整个代码非常简洁。效果如下:
圆周率中包含你的生日吗
啊,很高兴我们的生日(当然是我瞎编的)真的可以在圆周率前100万位中,但无论如何这也是一次有意思的实验不是吗?你也可以试试!

u5bf

什么你说要资源文件,额,正在上传中

标签:digits,string,包含,圆周率,million,生日,pi
来源: https://blog.csdn.net/weixin_51149598/article/details/120632533