小编Mik*_*keP的帖子

找到能整除一系列数字的最大 10 次方

我正在尝试缩小一组数字以输入 DP 子集和算法。(如果数字太大,它就会爆炸。) 具体来说,我需要找到可以除以数字而不损失精度的 10 的最大幂。我有一个工作例程,但由于它经常循环运行,我希望有一种比我想出的蛮力方法更快的方法。我的数字恰好是小数。

from decimal import Decimal
import math

def largest_common_power_of_10(numbers: list[Decimal]) -> int:
    """
    Determine the largest power of 10 in list of numbers that will divide into all numbers
    without losing a significant digit left of the decimal point
    """
    min_exponent = float('inf')
    for num in numbers:
        if num != 0:
            # Count the number of trailing zeros in the number
            exponent = 0
            while num % 10 == 0:
                num //= 10
                exponent += …
Run Code Online (Sandbox Code Playgroud)

python math

4
推荐指数
1
解决办法
216
查看次数

标签 统计

math ×1

python ×1