python - 数字是其数字总和的幂 Codewars

标签 python algorithm math

我被困在 Codewars Kata 中,我希望有人能帮助我(不要破坏解决方案)。 事实上,问题是我没有完全理解它应该如何工作,我明白了练习的想法,但事情有点困惑,尤其是在示例测试中。

以下是说明:

The number 81 has a special property, a certain power of the sum of its digits is equal to 81 (nine squared). Eighty one (81), is the first number in having this property (not considering numbers of one digit). The next one, is 512. Let's see both cases with the details.

8 + 1 = 9 and 9^2 = 81 512 = 5 + 1 + 2 = 8 and 8^3 = 512

我们需要创建一个函数 power_sumDigTerm(),它接收一个数字 n 并可能输出这个数字序列的第 n 项。我们上面介绍的案例意味着:

power_sumDigTerm(1) == 81
power_sumDigTerm(2) == 512

下面是示例测试:

test.describe("Example Tests")
test.it("n = " + str(1))
test.assert_equals(power_sumDigTerm(1), 81)
test.it("n = " + str(2))
test.assert_equals(power_sumDigTerm(2), 512)
test.it("n = " + str(3))
test.assert_equals(power_sumDigTerm(3), 2401)
test.it("n = " + str(4))
test.assert_equals(power_sumDigTerm(4), 4913)
test.it("n = " + str(5))
test.assert_equals(power_sumDigTerm(5), 5832)

我的主要问题是他们如何获得样本测试的结果。

最佳答案

一个好的加速技巧是不检查所有数字,任何这样的数字必须是整数 a 和 b 的 a^b 形式。如果您找到一种方法来枚举并检查它们,您将获得一个相当有效的解决方案。

关于python - 数字是其数字总和的幂 Codewars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48709246/

相关文章:

python - 使用张量输入时 Keras 模型预测会发生变化

python - 安装 pyopenssl 时遇到 cffi.ffiplatform.VerificationError

ruby - 尝试创建一个对生成器

c# - |x-y|之间差值最小的改进方程算法ax+by=c

python - 属性错误: 'tuple' object has no attribute 'readlines'

python - pyarrow 内存泄漏?

algorithm - 以数值方式求解具有二元变量的多元非线性方程组的最快方法是什么?

algorithm - 大O算法分析

c++ - 增量决策树 C++ 实现

java - 按给定四元数旋转单位 vector