Main page > Java

AddDigits.java—Add number permutations

This page offers a program which is given a list of decimal digits (they do not have to be unique). It then creates all permutations, including decimal fractions.

It was inspired by Stefan Niggemeier's article on German TV station 9 Live: 11 + 1 = 14, Frankfurter Allgemeine Sonntagszeitung, October 13, 2003, issue 41, page 35. The station's quiz games sometimes include questions that aren't exactly precise with regard to phrasing, to put it mildly.

The article describes one particular question:

Addieren Sie alle möglichen Zahlen: 1, 1, 2, 3, 4.

Which means Add all possible numbers. The obvious answer would be 1 + 1 + 2 + 3 + 4 = 11. The actual answer is 1802153.2592.

The solution is to add all permutations created from all or some of those five digits, including decimal fractions like 11.2 or 43.211. In the end, you'll have 669 unique numbers to add.

I created a small Java program AddDigits (short for add digit permutations) to compute the sum. Call it like that:

java AddDigits 1 1 2 3 4

to get the following output:

Sum: 1802153.2592 (669 numbers).

Should there ever be another question of the type Addieren Sie alle möglichen Zahlen, just run the program:

AddDigits.java (6 KB)

Be prepared, however, that the meaning of the sentence Addieren Sie alle möglichen Zahlen may have changed in the meantime. Maybe fractions are no longer supposed to be included. Only Fortuna will know.