简体   繁体   English

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

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

Given the list:鉴于名单:

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

I need to turn a random number divisible by 3, after by 6, after by 12, after by 24, after by 36, after by 48 and after by 60. One at each time, not the divisible for the seven numbers simultaneously.我需要将一个随机数转换为可被 3、6、12、24、36、48 和 60 整除的随机数。每次一个,不能同时整除七个数字。

But, to make the number divisible, I need to sum another number to reach the number divisible by 3, 6, 12, 24, 36, 48 or 60.但是,为了使数字可整除,我需要对另一个数字求和,以达到可被 3、6、12、24、36、48 或 60 整除的数字。

I don't know the random number neither the number to add to this random number in order to turn it divisible by 3, 6, 12, 24, 36, 48 or 60.我不知道随机数,也不知道要添加到这个随机数上以使其可被 3、6、12、24、36、48 或 60 整除的数字。

Can someone help me, please?有人能帮助我吗?

Using the map function list comprehension:使用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)

start and stop are the limits for your random number. startstop是随机数的限制。

Output for a = 311: a = 311 的输出:

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

Begin of the old answer to the rather unclear question before the first edit by the author.在作者第一次编辑之前,对相当不清楚的问题的旧答案的开始

The following post answers the question: Which is the next number after a , which is a multiple of all numbers of the list n , if it is not a ?下面的帖子回答了这个问题: a之后的下一个数字是哪个,它是列表n的所有数字的倍数,如果它不是a

First the least common multiple lcm of the numbers from the list must be determined.首先必须确定列表中数字的最小公倍数lcm For this purpose, the method least_common_multiple is executed for the complete list.为此,对完整列表执行了least_common_multiple方法。 The modulo operation then checks whether a is not already a multiple of the numbers.然后模运算检查a是否不是数字的倍数。 If this is the case, a is output.如果是这样的话, a是输出。 Otherwise, the next multiple of lcm is output.否则,输出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))

Output:输出:

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

Edit : Changed the code so that 0 is no longer a valid result for a non-positive a .编辑:更改了代码,以便0不再是非正a的有效结果。 However, if it is valid, you can change the code at the commented part with: result = a .但是,如果它有效,您可以更改注释部分的代码: result = a

New answer for the clarified question:澄清问题的新答案:

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)

Old answer:旧答案:

I think you're looking for the LCM (lowest common multiple) of all numbers in n.我认为您正在寻找 n 中所有数字的 LCM(最低公倍数)。 We can get the LCM for two numbers using the GCD (greatest common divisor).我们可以使用 GCD (最大公约数)获得两个数字的 LCM。 We can then use reduce to get the LCM for the whole list.然后我们可以使用 reduce 来获取整个列表的 LCM。 Assuming x can be negative, simply subtract a from the LCM to get your answer.假设 x 可以是负数,只需从 LCM 中减去 a 即可得到答案。 The code could look like this:代码可能如下所示:

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)

which outputs哪个输出

409

If speed is not important, you could also do a brute force solution like this:如果速度不重要,您也可以像这样使用蛮力解决方案:

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

(assuming that x >= 0) (假设 x >= 0)

So I can think of 2 solutions:所以我可以想到2个解决方案:

First if you want a list which show how much you have to add for the random number to get to a divisible number:首先,如果您想要一个列表,显示您必须为随机数添加多少才能成为可整除数:

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))

Output:输出:

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

Or if you want to know how far is the smallest number from the random number that everyone shares as divisible.或者,如果您想知道每个人共享的可整除的随机数的最小数字有多远。 For this take a look how you calculate lcm.为此,看看你如何计算 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))

Output:输出:

409 409

You can do:你可以做:

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)

Output:输出:

409

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

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