我如何使100 = 1? (内部解释) [英] How do I make 100 = 1? (explanation within)

查看:102
本文介绍了我如何使100 = 1? (内部解释)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我有了一个代码,该代码可以使用大于零且小于该值的数字来查找一个值的总和的组合数. 我需要更改值以扩展组合,以便它们不仅包含值.

Right now I have a code that can find the number of combinations of a sum of a value using numbers greater than zero and less than the value. I need to alter the value in order to expand the combinations so that they include more than just the value.

例如: 数字10产生结果: [1、2、3、4],[1、2、7], [1、3、6],[1、4、5], [1,9],[2,3,5],[2,8], [3,7],[4,6] 但是我需要将此范围扩展到包括任何也崩溃为1的数字.因为从本质上讲,我需要100 = n,因为数字内的各个数字之和= n.所以在这种情况下100 = 1,因为100-> 1 + 0 + 0 = 1 因此,数字1999也是列出值= 100的有效组合,因为1999 = 1 + 9 + 9 + 9 = 28,并且28 = 2 + 8 = 10,而10 = 1 + 0 = 1

For example: The number 10 yields the results: [1, 2, 3, 4], [1, 2, 7], [1, 3, 6], [1, 4, 5], [1, 9], [2, 3, 5], [2, 8], [3, 7], [4, 6] But I need to expand this to including any number that collapses to 1 as well. Because in essence, I need 100 = n in that the sum of the individual numbers within the digits = n. So in this case 100 = 1 because 100 --> 1+0+0 = 1 Therefore the number 1999 will also be a valid combination to list for value = 100 because 1999 = 1+9+9+9 = 28, and 28 = 2+8 = 10, and 10 = 1+0 = 1

现在我意识到这将产生一系列无限的组合,因此我将需要为要获取数据的范围设置限制.这是我用来查找组合的当前代码.

Now I realize that this will yield an infinite series of combinations, so I will need to set limits to the range I want to acquire data for. This is the current code I am using to find my combinations.

def a(lst, target, with_replacement=False):
    def _a(idx, l, r, t, w):
        if t == sum(l): r.append(l)
        elif t < sum(l): return
        for u in range(idx, len(lst)):
            _a(u if w else (u + 1), l + [lst[u]], r, t, w)
        return r
    return _a(0, [], [], target, with_replacement)

for val in range(100,101):
    s = range(1, val)
    solutions = a(s, val)
    print(solutions)
    print('Value:', val, "Combinations", len(solutions))

推荐答案

您似乎有多个问题.

要重复添加整数的十进制数字,直到用一位数字结尾,可以使用此代码.

To repeatedly add the decimal digits of an integer until you end with a single digit, you could use this code.

d = val
while d > 9:
    d = sum(int(c) for c in str(d))

这只是您描述的方式.但是,有一种更简单的方法.重复添加数字的小数位数称为投射出9 并导致数字的数字根.除要得到9而不是1的结果外,这几乎等于该数字除以9的余数.如此简单快捷的代码是

This acts in just the way you describe. However, there is an easier way. Repeatedly adding the decimal digits of a number is called casting out nines and results in the digital root of the number. This almost equals the remainder of the number when divided by nine, except that you want to get a result of 9 rather than 1. So easier and faster code is

d = val % 9
if d == 0:
    d == 9

或更短或更棘手的

d = (val - 1) % 9 + 1

或更棘手的

d = val % 9 or 9

要查找所有以7 结尾的数字(例如,或19的任何数字),您只希望除以其余的所有数字7通过9.因此,从7开始并继续添加9,您将获得所有这些值.

To find all numbers that end up at 7 (for example, or any digit from 1 to 9) you just want all numbers with the remainder 7 when divided by 9. So start at 7 and keep adding 9 and you get all such values.

您要使用的方法来查找7的所有分区,然后将它们排列成数字,这比必要的方法要复杂得多,而且速度慢.

The approach you are using to find all partitions of 7 then arranging them into numbers is much more complicated and slower than necessary.

要查找所有以16 结尾的数字(例如,或任何大于9的整数),则当前的方法可能是最好的.否则,很难避免直接通过16直接添加到725的数字.如果这确实是您的意思,请在您的问题中回答,然后我们可以进一步研究这种情况.

To find all numbers that end up at 16 (for example, or any integer greater than 9) your current approach may be best. It is difficult otherwise to avoid the numbers that directly add to 7 or to 25 without going through 16. If this is really what you mean, say so in your question and we can look at this situation further.

这篇关于我如何使100 = 1? (内部解释)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆