Results 1 to 7 of 7
  1. #1
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default 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"

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by matzahboy View Post
    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?

  4. #4
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

    Default

    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?

    Java 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));
            }
    
        }

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default hhhmmm...

    • Do you know what that piece of code does?
    • Do you know what the "prefix" and "s" variables are? What they represent?


    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    I don't understand what the 1 millionth (or nth) permutation means. There is no order to permutations.

  7. #7
    matzahboy is offline Member
    Join Date
    Nov 2008
    Posts
    67
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 11-03-2008, 02:38 PM
  2. Replies: 2
    Last Post: 08-24-2008, 12:30 AM
  3. Replies: 8
    Last Post: 04-22-2008, 03:21 AM
  4. Find nth root of a number
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-03-2008, 06:51 AM
  5. PLEASE!!!help me to find mistake
    By sasha20 in forum New To Java
    Replies: 1
    Last Post: 01-11-2008, 10:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •