- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为涉及组合学的应用程序制作实用程序类。
我做了两个尾递归的阶乘函数。第二个在计算组合数时提高了很多性能(我认为在英语中它们被称为n choose k,我使用 C(n,k) 表示法并制作了一个 nCk 函数)。
所以我测量了时间,并实现了我的目标。我的新组合函数比新函数快得多。
但是当谈到阶乘函数时,我发现我的新阶乘函数 'f' 比第一个 'fact' 慢一点。
有没有办法让这个“f”函数在性能上和“fact”一样好?
您可以在底部看到我机器中的主要测试执行和结果。
源代码
class Utils(object):
"""
Class with useful functions
"""
@staticmethod
def fact(i, _current_factorial=1):
'''
The factorial function
:param i: the number for calculating the factorial
:param _current_factorial: for internal use, it is the acumulated product
:return the result
'''
if i == 1:
return _current_factorial
else:
return Utils.fact(i - 1, _current_factorial * i)
@staticmethod
def f(i, k=None, _current_factorial=1):
'''
The factorial function
If k is given, it calculates the factorial but only with
the k-th first numbers.
Example:
f(6) = 6 · 5 · 4 · 3 · 2 · 1
f(6, 2) = 6 · 5
f(6, 3) = 6 · 5 · 4
f(9, 4) = 9 · 8 · 7 · 6
:param i: the number for calculating the factorial
:param k: optional, the number for calculating the factorial
:param _current_factorial: for internal use, it is the acumulated product
:return the result
'''
if k is None:
k = i
if i == 1 or k == 0:
return _current_factorial
else:
return Utils.f(i - 1, k - 1, _current_factorial=_current_factorial * i)
@staticmethod
def C(n, k=1):
'''
Statistical combinations 'nCk'
:param n: number of elements to be combined
:param k: number of elements to be taken for each combination
:return: the 'n choose k' mathematical result
'''
return Utils.fact(n) / (Utils.fact(k) * Utils.fact(n - k))
@staticmethod
def nCk(n, k=1):
'''
Statistical combinations 'nCk'
:param n: number of elements to be combined
:param k: number of elements to be taken for each combination
:return: the 'n choose k' mathematical result
'''
return Utils.f(n, k) / Utils.f(k)
if __name__ == "__main__":
NUM_TEST_ITERATIONS = 7500
MAX_NUMBER_WITHOUT_STACKOVERFLOW = 998
import time
start = time.process_time()
for i in range(NUM_TEST_ITERATIONS): Utils.f(MAX_NUMBER_WITHOUT_STACKOVERFLOW)
elapsed = time.process_time() - start
print('Function {} took {} to get result {}'.format('f', elapsed, Utils.f(MAX_NUMBER_WITHOUT_STACKOVERFLOW)))
start = time.process_time()
for i in range(NUM_TEST_ITERATIONS): Utils.fact(MAX_NUMBER_WITHOUT_STACKOVERFLOW)
elapsed = time.process_time() - start
print('Function {} took {} to get result {}'.format('fact', elapsed, Utils.fact(MAX_NUMBER_WITHOUT_STACKOVERFLOW)))
start = time.process_time()
for i in range(NUM_TEST_ITERATIONS): Utils.nCk(997, 4)
elapsed = time.process_time() - start
print('Function {} took {} to get result {}'.format('nCk', elapsed, Utils.nCk(997, 4)))
start = time.process_time()
for i in range(NUM_TEST_ITERATIONS): Utils.C(997, 4)
elapsed = time.process_time() - start
print('Function {} took {} to get result {}'.format('C', elapsed, Utils.C(997, 4)))
在屏幕上:
Function f took 7.854962716 to get result 402790050127220994538240674597601587306681545756471103647447357787726238637266286878923131618587992793273261872069265323955622495490298857759082912582527118115540044131204964883707335062250983503282788739735011132006982444941985587005283378024520811868262149587473961298417598644470253901751728741217850740576532267700213398722681144219777186300562980454804151705133780356968636433830499319610818197341194914502752560687555393768328059805942027406941465687273867068997087966263572003396240643925156715326363340141498803019187935545221092440752778256846166934103235684110346477890399179387387649332483510852680658363147783651821986351375529220618900164975188281042287183543472177292257232652561904125692525097177999332518635447000616452999984030739715318219169707323799647375797687367013258203364129482891089991376819307292252205524626349705261864003453853589870620758596211518646408335184218571196396412300835983314926628732700876798309217005024417595709904449706930796337798861753941902125964936412501007284147114260935633196107341423863071231385166055949914432695939611227990169338248027939843597628903525815803809004448863145157344706452445088044626373001304259830129153477630812429640105937974761667785045203987508259776060285826091261745049275419393680613675366264232715305430889216384611069135662432391043725998805881663054913091981633842006354699525518784828195856033032645477338126512662942408363494651203239333321502114252811411713148843370594801145777575035630312885989779863888320759224882127141544366251503974910100721650673810303577074640154112833393047276025799811224571534249672518380758145683914398263952929391318702517417558325636082722982882372594816582486826728614633199726211273072775131325222240100140952842572490801822994224069971613534603487874996852498623584383106014533830650022411053668508165547838962087111297947300444414551980512439088964301520461155436870989509667681805149977993044444138428582065142787356455528681114392680950815418208072393532616122339434437034424287842119316058881129887474239992336556764337968538036861949918847009763612475872782742568849805927378373244946190707168428807837146267156243185213724364546701100557714520462335084082176431173346929330394071476071813598759588818954312394234331327700224455015871775476100371615031940945098788894828812648426365776746774528000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Function fact took 6.757415364 to get result 402790050127220994538240674597601587306681545756471103647447357787726238637266286878923131618587992793273261872069265323955622495490298857759082912582527118115540044131204964883707335062250983503282788739735011132006982444941985587005283378024520811868262149587473961298417598644470253901751728741217850740576532267700213398722681144219777186300562980454804151705133780356968636433830499319610818197341194914502752560687555393768328059805942027406941465687273867068997087966263572003396240643925156715326363340141498803019187935545221092440752778256846166934103235684110346477890399179387387649332483510852680658363147783651821986351375529220618900164975188281042287183543472177292257232652561904125692525097177999332518635447000616452999984030739715318219169707323799647375797687367013258203364129482891089991376819307292252205524626349705261864003453853589870620758596211518646408335184218571196396412300835983314926628732700876798309217005024417595709904449706930796337798861753941902125964936412501007284147114260935633196107341423863071231385166055949914432695939611227990169338248027939843597628903525815803809004448863145157344706452445088044626373001304259830129153477630812429640105937974761667785045203987508259776060285826091261745049275419393680613675366264232715305430889216384611069135662432391043725998805881663054913091981633842006354699525518784828195856033032645477338126512662942408363494651203239333321502114252811411713148843370594801145777575035630312885989779863888320759224882127141544366251503974910100721650673810303577074640154112833393047276025799811224571534249672518380758145683914398263952929391318702517417558325636082722982882372594816582486826728614633199726211273072775131325222240100140952842572490801822994224069971613534603487874996852498623584383106014533830650022411053668508165547838962087111297947300444414551980512439088964301520461155436870989509667681805149977993044444138428582065142787356455528681114392680950815418208072393532616122339434437034424287842119316058881129887474239992336556764337968538036861949918847009763612475872782742568849805927378373244946190707168428807837146267156243185213724364546701100557714520462335084082176431173346929330394071476071813598759588818954312394234331327700224455015871775476100371615031940945098788894828812648426365776746774528000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Function nCk took 0.021283503999999454 to get result 40921610765.0
Function C took 13.261998684000002 to get result 40921610765.0
最佳答案
我最终删除了类代码并使用了 math.factorial,但我用“fact”名称导入了它。
我还在源代码文档中解释的公式中使用了快捷方式
这比原来的好多了。谢谢
源代码(为了简洁省略了主要部分):
# Importing math.factorial with shorter name
from math import factorial as fact
from functools import reduce
from operator import mul
def C(n, k=1):
'''
Statistical combinations 'nCk'
:param n: number of elements to be combined
:param k: number of elements to be taken for each combination
:return: the 'n choose k' mathematical result
'''
'''
Original formula: fact(n) / (fact(k) * fact(n-k))
Performance shortcut (about 40% faster):
1.- Calculates the numerator as the product over the first k-th elements of fact(n)
Let's call this f'(n,k), we store it in fnkProduct variable
Example:
n = 6, k = 2 => f'(n,k) = 30 = 6 · 5
n = 6, k = 3 => f'(n,k) = 120 = 6 · 5 · 4
n = 9, k = 4 => f'(n,k) = 3024 = 9 · 8 · 7 · 6
2.- Then divide it by fact(k)
New formula: f'(n,k) / fact(k)
'''
# C(n,k) == C(n,n-k), but the second is best when k is much bigger
k = n - k if k > (n // 2) else k
fnkProduct = reduce(mul, range(n, n - k, -1), 1)
return fnkProduct // fact(k)
在屏幕上(这是我在决定 1500 万次迭代之前所做的最后一次比较)
Function C took 0.01638609299999999 to get result 40921610765.0
Function nCk took 0.016534930000000003 to get result 40921610765.0
关于Python:是否有可能使这个尾递归阶乘更快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369377/
在本教程中,您将借助示例了解 JavaScript 中的递归。 递归是一个调用自身的过程。调用自身的函数称为递归函数。 递归函数的语法是: function recurse() {
我的类(class) MyClass 中有这段代码: public new MyClass this[int index] { get {
我目前有一个非常大的网站,大小约为 5GB,包含 60,000 个文件。当前主机在帮助我将站点转移到新主机方面并没有做太多事情,我想的是在我的新主机上制作一个简单的脚本以 FTP 到旧主机并下载整个
以下是我对 AP 计算机科学问题的改编。书上说应该打印00100123我认为它应该打印 0010012但下面的代码实际上打印了 3132123 这是怎么回事?而且它似乎没有任何停止条件?! publi
fun fact(x: Int): Int{ tailrec fun factTail(y: Int, z: Int): Int{ if (y == 0) return z
我正在尝试用c语言递归地创建线性链表,但继续坚持下去,代码无法正常工作,并出现错误“链接器工具错误 LNK2019”。可悲的是我不明白发生了什么事。这是我的代码。 感谢您提前提供的大力帮助。 #inc
我正在练习递归。从概念上讲,我理解这应该如何工作(见下文),但我的代码不起作用。 请告诉我我做错了什么。并请解释您的代码的每个步骤及其工作原理。清晰的解释比只给我有效的代码要好十倍。 /* b
我有一个 ajax 调用,我想在完成解析并将结果动画化到页面中后调用它。这就是我陷入困境的地方。 我能记忆起这个功能,但它似乎没有考虑到动画的延迟。即控制台不断以疯狂的速度输出值。 我认为 setIn
有人愿意用通俗易懂的语言逐步解释这个程序(取自书籍教程)以帮助我理解递归吗? var reverseArray = function(x,indx,str) { return indx == 0 ?
目标是找出数组中整数的任意组合是否等于数组中的最大整数。 function ArrayAdditionI(arr) { arr.sort(function(a,b){ return a -
我在尝试获取 SQL 查询所需的所有数据时遇到一些重大问题。我对查询还很陌生,所以我会尽力尽可能地描述这一点。 我正在尝试使用 Wordpress 插件 NextGen Gallery 进行交叉查询。
虽然网上有很多关于递归的信息,但我还没有找到任何可以应用于我的问题的信息。我对编程还是很陌生,所以如果我的问题很微不足道,请原谅。 感谢您的帮助:) 这就是我想要的结果: listVariations
我一整天都在为以下问题而苦苦挣扎。我一开始就有问题。我不知道如何使用递归来解决这个特定问题。我将非常感谢您的帮助,因为我的期末考试还有几天。干杯 假设有一个包含“n”个元素的整数数组“a”。编写递归函
我有这个问题我想创建一个递归函数来计算所有可能的数字 (k>0),加上数字 1 或 2。数字 2 的示例我有两个可能性。 2 = 1+1 和 2 = 2 ,对于数字 3 两个 poss。 3 = 1+
目录 递归的基础 递归的底层实现(不是重点) 递归的应用场景 编程中 两种解决问题的思维 自下而上(Bottom-Up) 自上而下(Top-
0. 学习目标 递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。本节主要介绍递归的基本概念以及如何构建递归程序。
我有一个问题一直困扰着我,希望有人能提供帮助。我认为它可能必须通过递归和/或排列来解决,但我不是一个足够好的 (PHP) 程序员。 $map[] = array("0", "1", "2", "3")
我有数据 library(dplyr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg, qsec) %>% h
在 q 中,over 的常见插图运算符(operator) /是 implementation of fibonacci sequence 10 {x,sum -2#x}/ 1 1 这确实打印了前 1
我试图理解以下代码片段中的递归调用。 static long fib(int n) { return n <= 1 ? n : fib(n-1) + fib(n-2); } 哪个函数调用首先被
我是一名优秀的程序员,十分优秀!