Question: I can't seem to find much documentation on forEach. I have a dataset that is in key/value pairing. I am looking to do something like (pseudo code):

forEach key, sum the value
forEach key, max of the values
etc.

Answer: 
If repetition is allowed, then:

* the first permutation is `0000000000`
* the second permutation is `0000000001`
* the tenth permutation is `0000000009`
* the hundredth permutation is `0000000099`
* the thousandth permutation is `0000000999`
* the millionth permutation is `0000999999`

and so on.

***All of these are simply the number `n-1` padded with enough number of zeroes on the left to make the whole string of length 10.*** 

So to get the actual nth combination, all you would have to do is (below snippet in Python, you can convert to Java easily):

```
>>> def find_nth_combination(n):
...     print "0" * (10-len(str(n-1))) + str(n-1)
... 
>>> find_nth_combination(1)
0000000000
>>> find_nth_combination(100)
0000000099
>>> find_nth_combination(9062300000)
9062299999
>>> find_nth_combination(12300000)
0012299999

```

In case you want to solve this with repetition, you can [have a look here](https://github.com/ansh0l/Euler/blob/master/024/lexicographic_permutations.py) (code is in Python).

---

To get all permutations, simply go through all the numbers.

So, you will need to do something like:

```
for x in xrange(1, 1001):
    find_nth_combination(x)

```

which will output:

```
0000000000
0000000001
...
...
0000000997
0000000998
0000000999

```
**WITHOUT REPETITIONS:**

Since you only need 1000000000th permutation, you can brute-force this problem. Start with string: "0123456789" and use C++ equivalent of next\_permuation, for which you can get function here: <http://codeforces.ru/blog/entry/3980>. Just iterate one million times and you will arrive to solution.

There is fancier greedy solution which runs very fast and you can read about it here: <https://math.stackexchange.com/questions/60742/finding-the-n-th-lexicographic-permutation-of-a-string>

**WITH REPETITIONS:**

This problem is very simple with repetitions since it is equivalent to just taking number and padding it with 0s at beginning.

```
1st permuation: 000000001
15th permuation 000000015
etc.

```