Created
April 12, 2013 05:59
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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