利用Python在PI中尋找生日

2021-08-17 02:01:07 字數 1641 閱讀 3740

(1)使用y-cruncher工具得待pi的億萬位的txt文件;

(2)**很簡單,首先是得到可操作的文件,然後提示使用者輸入要查詢的生日,預設的格式是year-mouth-day,例如94-01-01,先檢查輸入的生日格式是否正確,要是正確的話就從文件中進行查詢,若查詢到就返回其存在的個數,從查詢的結果來看,本人的生日出現的次數還是挺多的,但是要是查詢的8位的生日,就沒有那麼幸運了。

(3)本程式只能對特定格式的數進行查詢,要是想要查詢別的資料,稍微的更改下程式就可以了。

import sys

import re

def get_file():

#get the file to operate

f = open(r"k:\_\python\pi.txt", "r")

lines = f.read()

f.close()

return lines

def judge_birthday(number):

#judge the format of birthday is righy or not

dir_birthday =

if len(number) < 8:

number = '19' + number

year = int(number[:4])

mouth = int(number[4:6])

day = int(number[6:])

if (year > 2000) or (year < 1900):

return false

if (mouth > 12) or (mouth < 1):

return false

else:

day_correct = dir_birthday[number[4:6]]

if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):

day_correct += 1

if (day < 1) or (day > day_correct):

return false

return true

def search_in_pu(number, lines):

#search the number of birthday in the pi file

length = len(re.findall(number, lines))

if length < 1:

print("not found!")

else:

print(number + ": " + str(length))

if __name__ == "__main__":

lines = get_file()

print("enter the year that you want to search: ")

number = input()

flag = judge_birthday(number)

if flag:

search_in_pu(number, lines)

else:

print("the format of your birthday is not correct!")

檢視自己生日是否在pi中

pi是乙個小數點後很多的數,以下給出檢視自己的生日是否在pi中的例項 filename pi million digits.txt with open filename as file object lines file object.readlines pi string forline inli...

怎樣在目錄中尋找檔案

怎樣在目錄中尋找檔案 下面的 說明了怎樣在乙個給定的目錄中從上到下地搜尋整個目錄樹.本例子只將結果輸出到system debug screen.呼叫下面的類函式,搜尋完成之後,將出現乙個資訊框.void ctestview onsearch 函式 searchdirectory 在函式 onsear...

計算pi的近似值公式法 利用python計算圓周率

圓周率沒有精確的計算公式,所以只能用近似的方式計算它的近似值。我們運用蒙特卡羅方法,思路很簡單,在下面圖形中隨機拋置大量的點,計算落在1 4圓內的點的數量。為了得到pi值,由思路,我們知道需要引用random math以及time資料庫,具體 如下 pi.py from random import ...