Skip to content Skip to sidebar Skip to footer

How Do I Make 100 = 1? (explanation Within)

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

Solution 1:

You seem to have multiple issues.

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 instr(d))

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 % 9ifd== 0:
    d == 9

or perhaps the shorter but trickier

d = (val - 1) % 9 + 1

or the even-more-tricky

d = val % 9 or 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.

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

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.

Post a Comment for "How Do I Make 100 = 1? (explanation Within)"