# Find all permutations of a number

• 11-30-2008, 10:59 PM
matzahboy
Find all permutations of a number
I have a an integer, n, that is equal to 1379. I want to create an array of all the permutations of n (1397, 1739, 1793, 1937, 1973, 3179, etc...). How would I code this?

Note: I know how many permutations exist. Call that variable "tPerms"
• 12-01-2008, 12:21 AM
Fubarable
At your current level, I'd bet you'd be able to solve this without our help. Why not first give it a go? Also, myself, I'd use characters or String representations of the numbers since we're not really treating these things as numbers.
• 12-01-2008, 05:50 AM
Eranga
Quote:

Originally Posted by matzahboy
I have a an integer, n, that is equal to 1379. I want to create an array of all the permutations of n (1397, 1739, 1793, 1937, 1973, 3179, etc...). How would I code this?

Note: I know how many permutations exist. Call that variable "tPerms"

What you have tried so far, can you show us here?
• 12-01-2008, 11:45 PM
matzahboy
I found the following code (courtesy of Permutations.java). For the particular problem I'm trying to do, I need to find the 1000000th permutation. How would I modify this code to do that?

Code:

```// print N! permutation of the characters of the string s (in order)     public  static void perm1(String s) { perm1("", s); }     private static void perm1(String prefix, String s) {         int N = s.length();         if (N == 0) System.out.println(prefix);         else {             for (int i = 0; i < N; i++)               perm1(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, N));         }     }```
• 12-02-2008, 01:35 AM
CJSLMAN
hhhmmm...
• Do you know what that piece of code does?
• Do you know what the "prefix" and "s" variables are? What they represent?

CJSL
• 12-02-2008, 04:00 AM
emceenugget
I don't understand what the 1 millionth (or nth) permutation means. There is no order to permutations.
• 12-02-2008, 04:59 AM
matzahboy
I'm basically trying to solve problem 24 of project euler (Problem 24 - Project Euler)