Skip to content

Instantly share code, notes, and snippets.

@drusepth
Created April 12, 2013 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drusepth/5369782 to your computer and use it in GitHub Desktop.
Save drusepth/5369782 to your computer and use it in GitHub Desktop.
C++ Integer Digit Permutations. Coded this up when I thought I was working with permutable primes rather than circular primes. Thought I'd salvage my time and share it for anyone looking for something similar in the future.
vector<int> digitPermutationsOf(int n) {
vector<int> permutations;
// Convert integer to char array of digits
char digits[16]; // Max 16 digits allowed for ints
int size = sprintf(digits, "%d", n);
// Iterate through the digit array and save each permutation
std::sort(digits, digits + size);
do {
permutations.push_back(atoi(digits));
} while (std::next_permutation(digits, digits + size));
// Return all permutations of n
return permutations;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment