I want to know what is the biggest number you can make from multiplying digits entered by the user like this: 5*6*7*2 OR 567*2 OR 67*25 ...etc so 5/6/7/2 should be entered by the user as variables, but how do I tell python to form a number from two variables (putting the two digits next to each other and treating the outcome as a number by itself).
-
2I would say it's `7652` (all digits concatenated), unless you can use exponentiation - then it's `2^5^6^7` (evaluated right-to-left). – John Dvorak Jan 22 '13 at 13:39
-
You say the digits are entered into variables, but are they stored in those variables as numbers or as strings? – Some programmer dude Jan 22 '13 at 13:40
-
1@JanDvorak is correct. The largest result will always be gained by concatenating all of the digits. – mbeckish Jan 22 '13 at 13:43
-
@mbeckish: That's right. What if you *have* to use at least one multiplication? Is the solution trivial in that case too? – NPE Jan 22 '13 at 13:50
-
@NPE - In that case, I'm pretty sure you should multiply the biggest digit by the rest of the digits concatenated, but I can't come up with a proof right now. – mbeckish Jan 22 '13 at 13:59
-
@EyadArafat do you accept all the digits concatenated as an answer to your problem? – Mike Jan 22 '13 at 15:26
-
1@mbeckish - `I'm pretty sure you should multiply the biggest digit by the rest of the digits concatenated` - That was my thought too at first, but it looks like that's not the biggest possible number. Check out the proof below. – Mike Jan 22 '13 at 15:27
5 Answers
how do I tell python to form a number from two variables (putting the two digits next to each other and treating the outcome as a number by itself)
Provided the two digits are stored in integer variables, the following will do it:
In [1]: v1 = 5
In [2]: v2 = 6
In [3]: v1 * 10 + v2
Out[3]: 56
This can be generalized to a sequence of digits:
In [7]: l = (1, 2, 6, 3)
In [8]: reduce(lambda x,y: x * 10 + y, l)
Out[8]: 1263

- 486,780
- 108
- 951
- 1,012
-
That will produce some unexpected results if the variables are strings. – Some programmer dude Jan 22 '13 at 13:38
-
@JoachimPileborg: Fair enough. I've added explicit wording about the types of `v1` and `v2`. – NPE Jan 22 '13 at 13:40
-
Isn't the reduce, just a convoluted way of doing: `int(''.join(map(str,l)))` ? – Jon Clements Jan 22 '13 at 14:03
-
1@JonClements: It's a numerical problem, so I prefer to work with numbers rather than strings. Of course, someone else might prefer to represent digits as strings. As for pure readability, I think that's in the eye of the beholder. – NPE Jan 22 '13 at 14:05
-
Why even bring them in as integers at all? If you are accepting the variables from a user, just take them in as a string and concatenate. Cuts the number of operations in half. This way is more extendable as well, because using eval() it would be easier to do multiple operations by swapping characters around, ie you have '567*2' then you can shift the asterisk to '56*72'. You would only need to permutate '5672*' to try all possibilities. – Kyle Jan 22 '13 at 14:06
-
@NPE fair enough - I just saw that "user inputs" data - which normally implies it's a string of some sort to start with anyway... – Jon Clements Jan 22 '13 at 14:06
-
Your code seems to be the most appropriate, but can you clarify it a bit,the digits must be entered by the user. – Jan 22 '13 at 14:07
so 5/6/7/2 should be entered by the user as variables, but how do I tell python to form a number from two variables (putting the two digits next to each other and treating the outcome as a number by itself).
Seems the root of your problem is capturing data from the user, combining it, and converting it:
>>> a = raw_input()
8
>>> b = raw_input()
3
>>> a
'8'
>>> b
'3'
>>> a + b
'83'
>>> int(a+b)
83
It's that easy.
Now as far as biggest number you can make from multiplying digits entered
goes... we can prove that with math if you'd like so you don't have a pile of combinations to try:
We can sort the digits a
>= b
>= c
>= d
First let's look at splitting the digits 3
and 1
. We need to compare a * bcd
, b * acd
, c * abd
, d * abc
.
Comparing a * bcd = 100ab + 10ac + ad
with b * acd = 100ab + 10bc + bd
we see the former is larger because a
>= b
. A similar argument will show that a * bcd
beats the others.
Similarly we can compare ac * bd = 100ab + 10(ad+bc) + bd
with ad * bc = 100ab + 10(ac+bd) + cd
. We would rather have more copies of the big a
, so the second wins.
Finally we need to compare a * bcd = 100ab + 10ac + ad
with ad * bc = 100ab + 10(ac+bd) + cd
. The second is the winner.
You probably took the input in a loop as an array, so if you have:
(a) arr[0] = '5' arr[0] = '7'
(b) arr[1] = '6' sort em => arr[1] = '6'
(c) arr[2] = '7' arr[2] = '5'
(d) arr[3] = '2' arr[3] = '2'
The largest would be:
int(arr[0] + arr[3]) * int(arr[1] + arr[2]) = 4680

- 47,263
- 29
- 113
- 177
I feel you have posted a mistake in your question. You ask for permutations? Are you sure?
If so, see @mbeckish's answer. It's pretty simple, and not a very good exercise in programming.
(it is, however, a good exercise in trick questions with riddle-like "gotchas" for solutions)
A better approach is to ditch the permutations requirement, so that the order of the input can actually affect the answer. For that approach, we get a much more interesting solution:
def largest_product(*args):
numbers = ''.join(map(str, args))
results = []
for i in range(1, len(numbers) - 1):
multicand = int(numbers[:i])
multiplier = int(numbers[i:])
m, n = multicand, multiplier
results.append(( m * n, "%s * %s" % (m, n)))
return max(results)
>>> largest_product(*range(8))
(827115, '12345 * 67')
-
This answer is incorrect. The largest result from 1234567 is 7654321 (assuming you can just concatenate all n digits). If you must multiply at least one digit, then the largest result is 7*654321 = 4,580,247. – mbeckish Jan 22 '13 at 14:21
-
By using permutations, user input means very little (as you have proven twice already). This approach solves a different problem, one where the result can have a little variation. I suspect OP is working on a problem for school, and has mis-posted the requirements. I could be wrong, so no hard feelings about the downvote. – yurisich Jan 22 '13 at 14:35
-
I agree, the question is vague. If you make any minor edit to your post, it will let me remove my downvote. – mbeckish Jan 22 '13 at 14:39
-
Any solution that has you trying all permutations of digits will be horribly inefficient, running in O(n!). Just 14 digits (and the multiply operator) would give around 1 trillion combinations!
An O(n lg n) solution would be:
- Sort the digits from high to low.
- Concatenate them into one string.
- Print the string.
If you must multiply at least one digit, then
- Sort.
- Take the highest digit and multiply by the concatenation of the remaining digits.
- Print the result.
If you must multiply at least one digit, then you might need to try all permutations (see @Mike's answer).

- 10,485
- 5
- 30
- 55
I assume you get the numbers as string, so you can simply strip them, join and translate to int:
string = "5*6*7*2"
value = int( "".join(string.split('*')) )
# value == 5672

- 5,947
- 1
- 27
- 26