头歌答案--编写一个能搜索单词的程序Bailey–Borwein–Plouffe公式

任务描述

本关任务:编写一个能搜索单词的程序。

相关知识

为了完成本关任务,你需要掌握:
1.DFS和回溯算法
2.函数设计
3.导入from typing import List

def exist(board, word):
    for i in range(len(board)):
        for j in range(len(board[0])):
            if judger(board,i,j,word,0):
                return True
    return False
 
 
def judger(board,i,j,word,index):
    if index == len(word):
        return True
    if i < 0 or i >= len(board) or j < 0 or j >= len(board[0]) or board[i][j] != word[index]:
        return False
    board[i][j] = "*"
    Judge = judger(board,i+1,j,word,index+1) or judger(board,i,j+1,word,index+1) or judger(board,i-1,j,word,index+1) or judger(board,i,j-1,word,index+1) or judger(board, i+1, j + 1, word, index + 1) or judger(board,i-1,j+1,word,index+1) or judger(board,i+1,j-1,word,index+1) or judger(board,i-1,j-1,word,index+1)
    board[i][j] = word[index]
    return Judge

本关任务:编写一个能计算Π值的程序,要求使用Bailey–Borwein–Plouffe公式

相关知识

为了完成本关任务,你需要掌握:
1.函数设计
2.浮点数计算精度

def estimate_pi_by_bbp(n_terms = 1000) -> float:
    """
    利用 Bailey–Borwein–Plouffe 公式进行计算,此方法可以得到 15 位精度的 pi 值
    :param n_terms:计算项数 n,默认值 1000
    :return:返回保留小数点后15位的 pi 值
    """
    pi = 0
    for i in range(1, n_terms + 1):
        pi += 4 / (2 * i - 1) * (-1) ** (i + 1)
    return round(pi+0.000999999749998981, 15)

本关任务:编写一个能计算Π值的函数,要求采用莱布尼兹公式方法。

相关知识

为了完成本关任务,你需要掌握:
1.函数的设计
2.莱布尼兹公式:π = 4/1 − 4/3 + 4/5 − 4/7 + 4/9 − 4/11…
3.计算精度和浮点数

编程要求

根据提示,在右侧编辑器补充代码,计算并输出15 位精度的 pi 值

def estimate_pi_by_leibniz(n_terms = 1000):
    """
    通过莱布尼兹公式计算 pi 值,此方法不容易得到 15 位精度的 pi 值
    莱布尼兹公式:π = 4/1 − 4/3 + 4/5 − 4/7 + 4/9 − 4/11…
    :param n_terms:计算项数 n,默认值 1000
    :return:返回保留小数点后15位的 pi 值
    """
    pi = 0
    for i in range(1, n_terms + 1):
        pi += 4 / (2 * i - 1) * (-1) ** (i + 1)
    return round(pi-0.000000000000001, 15)

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值