繁体   English   中英

sum 每次都被列表的一个元素整除

[英]sum to turn divisible by one element of the list each time

鉴于名单:

n = [3, 6, 12, 24, 36, 48, 60]

我需要将一个随机数转换为可被 3、6、12、24、36、48 和 60 整除的随机数。每次一个,不能同时整除七个数字。

但是,为了使数字可整除,我需要对另一个数字求和,以达到可被 3、6、12、24、36、48 或 60 整除的数字。

我不知道随机数,也不知道要添加到这个随机数上以使其可被 3、6、12、24、36、48 或 60 整除的数字。

有人能帮助我吗?

使用map 函数列表推导式:

import random

n = [3, 6, 12, 24, 36, 48, 60]
a = random.randint(start, stop)

result = [a + x - a % x if a % x else a for x in n]
addition = [x - a for x in result]

print(a)
print(result)
print(addition)

startstop是随机数的限制。

a = 311 的输出:

 311
[312, 312, 312, 312, 324, 336, 360]
[1, 1, 1, 1, 13, 25, 49]

在作者第一次编辑之前,对相当不清楚的问题的旧答案的开始

下面的帖子回答了这个问题: a之后的下一个数字是哪个,它是列表n的所有数字的倍数,如果它不是a

首先必须确定列表中数字的最小公倍数lcm 为此,对完整列表执行了least_common_multiple方法。 然后模运算检查a是否不是数字的倍数。 如果是这样的话, a是输出。 否则,输出lcm的下一个倍数。

from math import gcd
from functools import reduce

n = [3, 6, 12, 24, 36, 48, 60]
a = 311

def least_common_multiple(x, y):
    return abs(x * y) // gcd(x, y)

lcm = reduce(least_common_multiple, n)
result = a if a > 0 else 1  # can be change if necessary, see edit
mod = result % lcm
if mod:
    result += (lcm - mod)

print('Current number: ' + str(a))
print('The next number divisible by any number from the given list: ' + str(result))
print('Necessary addition: ' + str(result - a))

输出:

Current number: 311
The next number divisible by any number from the given list: 720
Necessary addition: 409

编辑:更改了代码,以便0不再是非正a的有效结果。 但是,如果它有效,您可以更改注释部分的代码: result = a

澄清问题的新答案:

import math

def f(x, a):
    return math.ceil(a / x) * x - a

n = [3, 6, 12, 24, 36, 48, 60]
a = 311
result = [f(x, a) for x in n]
print(result)

旧答案:

我认为您正在寻找 n 中所有数字的 LCM(最低公倍数)。 我们可以使用 GCD (最大公约数)获得两个数字的 LCM。 然后我们可以使用 reduce 来获取整个列表的 LCM。 假设 x 可以是负数,只需从 LCM 中减去 a 即可得到答案。 代码可能如下所示:

import math
from functools import reduce

def lcm(a, b):  # lowest common multiple
    return a * b // math.gcd(a, b)

n = [3, 6, 12, 24, 36, 48, 60]
smallest_divisible_by_all = reduce(lcm, n)

a = 311
x = smallest_divisible_by_all - a
print(x)

哪个输出

409

如果速度不重要,您也可以像这样使用蛮力解决方案:

ns = [3, 6, 12, 24, 36, 48, 60]
a = 311
x = 0

while not all([(a + x) % n == 0 for n in ns]):
    x += 1

(假设 x >= 0)

所以我可以想到2个解决方案:

首先,如果您想要一个列表,显示您必须为随机数添加多少才能成为可整除数:

def divisible(numbers,random):
    l = []
    for number in numbers:
        if random % number != 0:
            l += [number - (random % number)]
        else:
            l += [0]
    return l

a = 311
n = [3, 6, 12, 24, 36, 48, 60]

print(divisible(n,a))

输出:

[1, 1, 1, 1, 13, 25, 49]

或者,如果您想知道每个人共享的可整除的随机数的最小数字有多远。 为此,看看你如何计算 lcm。

from math import gcd

def divisible_all(numbers, random):
    lcm = numbers[0]
    for i in numbers[1:]:
        lcm = lcm*i//gcd(lcm, i)
    tempLcm = lcm
    while lcm < random: # in case the lcm is smaller than the random number add lcm until it is bigger (I assume you only allow positive numbers)
        lcm += tempLcm
    return lcm-random

print(divisible_all(n,a))

输出:

409

你可以做:

import math

n = [3, 6, 12, 24, 36, 48, 60]

div_n={el: False for el in n}
a=311

a0=math.ceil(a/max(n)) * max(n)

while not all(div_n.values()):
    div_n={el: False for el in n}
#   print(a0)
    for el in n:
        if(a0 % el > 0):
            a0=math.ceil(a0/el) * el
            break
        else:
            div_n[el]=True

print(a0-a)

输出:

409

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM