Thread: Find every possible permutation with length from 1 to n

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    8

    Question Find every possible permutation with length from 1 to n

    I need to be able to find every possible permutation using all possible values of a char. But I have to make it be able to form permutations from a length of 1 to variable N. For example, if N=3, I need it to be able to come up with

    0x00
    0x01
    .......
    0x00 0x00
    0x01 0x01
    .......
    0xff 0xff 0xfe
    0xff 0xff 0xff

    How could I do this. (I would like to avoid recursion, since N might be as large as 50 or 60 and using recursion would most likely cause a stack overflow)

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 0x00
    > 0x01
    > .......
    > 0x00 0x00
    But once you've worked it out for 1 byte, surely for N bytes, it's just a loop?

    > since N might be as large as 50 or 60
    2^60 is 1.152921505×10¹⁸
    *300 bytes just to print a single permutation.
    By my calculation, that's 345TB of output. What are you planning to do with it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    8
    I made a simple xor encryption program, and I need to decrypt a file that was encrypted with my program. But I forgot the key that I used, so I need to be able to generate every possible key that I may have used. I do have a tendency to use ridiculously long passwords for things though. (By the way, I have no way to get a file without decrypting it, because I made the encryption program so that it deletes the file to be encrypted once it is encrypted)

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps your time would be better employed researching how to break encryption systems by methods other than brute force.

    You'll be long dead before your algorithm gets anywhere near finished.
    Even if you manage an astonishing 1nS for each permutation, you're looking at over 35 YEARS to try all 60-bits worth.

    Do you still have the program (better yet, the source code) for the encryption?

    A simple algorithm, and program flaws, can significantly reduce the effort you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to find the permutation of a word
    By Clayton Cuteri in forum C Programming
    Replies: 15
    Last Post: 01-31-2013, 11:51 PM
  2. How do I find the length of this string
    By Ariyana in forum C Programming
    Replies: 2
    Last Post: 06-07-2011, 10:10 AM
  3. How to find out the length of a printf before it's printed
    By Richardcavell in forum C Programming
    Replies: 13
    Last Post: 04-08-2011, 03:26 PM
  4. C program to find inverses of a permutation
    By newbie123 in forum C Programming
    Replies: 6
    Last Post: 08-22-2010, 07:17 AM
  5. how do I find the length of and array using sizeof()
    By raymond1234 in forum C Programming
    Replies: 9
    Last Post: 04-27-2007, 02:38 AM