我很好奇为什么它的增加速度比在python中获取功能要快得多(尽管从我读过的内容来看,这在很多其他语言中也是如此).例如,它要快得多
x*x
Run Code Online (Sandbox Code Playgroud)
比
x**2
Run Code Online (Sandbox Code Playgroud)
我认为**算子更通用,也可以处理分数幂.但是,如果这就是为什么它如此慢,为什么它不执行int指数检查然后只是进行乘法?
编辑:这是我试过的一些示例代码...
def pow1(r, n):
for i in range(r):
p = i**n
def pow2(r, n):
for i in range(r):
p = 1
for j in range(n):
p *= i
Run Code Online (Sandbox Code Playgroud)
现在,pow2只是一个简单的例子,显然没有优化!
但即便如此,我发现使用n = 2且r = 1,000,000,则pow1需要~2500ms而pow2需要~1700ms.
我承认,对于大的n值,pow1确实比pow2快得多.但这并不太令人惊讶.
我有一个零和一元组,例如:
(1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1)
Run Code Online (Sandbox Code Playgroud)
事实证明:
(1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1) == (1, 0, 1, 1) * 3
Run Code Online (Sandbox Code Playgroud)
我想要一个函数f,使得if s是一个零和一的非空元组,f(s)是最短的子元素r,s == r * n对于某些正整数n.
所以,例如,
f( (1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1) ) == (1, 0, 1, 1)
Run Code Online (Sandbox Code Playgroud)
f在Python中编写函数的方法是什么?
编辑:
我目前使用的天真方法
def f(s):
for i in range(1,len(s)): …Run Code Online (Sandbox Code Playgroud) 我用 Python 编写了代码来计算 10000 以下的友好数字的总和:
def amicable(a, b):
total = 0
result = 0
for i in range(1, a):
if a % i == 0:
total += i
for j in range(1, b):
if b % j == 0:
result += j
if total == b and result == a:
return True
return False
sum_of_amicables = 0
for m in range (1, 10001):
for n in range (1, 10001):
if amicable(m, n) == True and m != n:
sum_of_amicables = …Run Code Online (Sandbox Code Playgroud) 我有一个数学/Python 问题,如何将平方根中的大数更改为小数(但仍保留该值),例如 \xe2\x88\x9a243 更改为 9\xe2\x88\x9a3?
\n我还有一个问题,如何在我的代码中使用它?
\n代码:
\nimport math\ndl=int(input("Podaj d\xc5\x82ugo\xc5\x9b\xc4\x87: "))\nc=input("Kt\xc3\xb3ry bok: przypdl, przypkr, przec: ")\ndef find(x):\n global przypkr\n global przec\n global przypdl\n if c=="przec":\n przec=x\n przypkr=x/2\n przypdl=przypkr*math.sqrt(3)\n elif c=="przypkr":\n przypkr=x\n przypdl=x*math.sqrt(3)\n przec=x*2\n else:\n przypdl=x\n przypkr=x/math.sqrt(3)\n przec=przypkr*2\n print(f'przeciwprostok\xc4\x85tna: {przec}, kr\xc3\xb3tsza przyprostok\xc4\x85tna: {przypkr}, d\xc5\x82u\xc5\xbcsza przyprostok\xc4\x85tna: {przypdl} \xe2\x88\x9a{przypdl*przypdl}')\nfind(dl)\n\ndef obw():\n print(f'Obw\xc3\xb3d r\xc3\xb3wna si\xc4\x99: {przec + przypkr + przypdl} lub {przec + przypkr} + \xe2\x88\x9a{przypdl*przypdl}')\n\nobw()\nRun Code Online (Sandbox Code Playgroud)\n 免责声明:在SO上也有与此类似的问题,但是 它们要么都不解决算法的效率问题,要么用另一种语言编写。看到这个关于python效率的答案,看看它是否可以帮助您回答我的问题。
因此,我需要最有效的方法来找到任意给定数量的所有因素,这些因素可以快速处理非常大的数量。我已经有一些可以迭代的代码,但是要花很长时间才能处理超过6个字符的数字。
编辑:根据要求,这是我执行此操作的一些无效方法(为清楚起见,省略了错误检查)
真的很乱:
@IBAction func findFactorsButton(_ sender: AnyObject) {
if let _ = textField.text, !textField.text!.isEmpty {
counter = 1
factors = []
repeat {
counter += 1
if Int(textField.text!)! % counter == 0 {
factors.append(String(counter))
} else {
continue
}
} while counter != Int(textField.text!)
factors.removeLast()
outputLabel.text = factors.joined(separator: ", ")
} else {
outputLabel.text = ""
}
}
Run Code Online (Sandbox Code Playgroud)
更少混乱的解决方案(操场):
func calculateFactors(n: Int) -> String {
var …Run Code Online (Sandbox Code Playgroud) 如何1636695303948070935006594848413799576108321023021532394741645684048066898202337277441635046162952078575443342063780035504608628272942696526664263794691在python中找到数字因子?
它不应该是素数.任何因素除了1和数量 - 可接受.
我从这里查看过没有结果的解决方案.
天真的解决方案如:
def factor(n):
i = 2
limit = n / 2
while i <= limit:
if n % i == 0:
return i
i += 1
return 1
Run Code Online (Sandbox Code Playgroud)
也行不通.
python ×5
algorithm ×3
performance ×2
c++ ×1
math ×1
numbers ×1
python-2.7 ×1
sequence ×1
square-root ×1
swift ×1