Algo::Algo(int input_given[], int finalNum) { // Set up final size this -> goal = finalNum; // Set up permutations for numbers std::sort(input_given, input_given + 4); do { point p; p.num1 = input_given[0]; p.num2 = input_given[1]; p.num3 = input_given[2]; p.num4 = input_given[3]; this -> numPermutations.push_back(p); } while ( std::next_permutation(input_given, input_given + 4) ); // Set up permutations for operators int operatorCount = 4; int permutationLength = 3; std::string operatorBank[] = {OPERATIONS}; std::string permutation[] = {DEFAULTS}; int curIndex = 0; getPermutations(operatorBank, operatorCount, permutation, permutationLength, curIndex); }
static celix_status_t getPermutations(array_list_pt bundleIds, int from, int to, array_list_pt permutations) { celix_status_t status = CELIX_SUCCESS; int i = 0; if (from == to) { long* permutation = (long*) calloc(to + 1, sizeof(*permutation)); if (!permutation) { status = CELIX_ENOMEM; } else { for (; i <= to; i++) { permutation[i] = (long) arrayList_get(bundleIds, i); } arrayList_add(permutations, permutation); } } else { for (i = from; i <= to; i++) { long fromOrg = (long) arrayList_get(bundleIds, from); long iOrg = (long) arrayList_get(bundleIds, i); arrayList_set(bundleIds, from, (void*) iOrg); arrayList_set(bundleIds, i, (void*) fromOrg); status = getPermutations(bundleIds, from + 1, to, permutations); arrayList_set(bundleIds, from, (void*) fromOrg); arrayList_set(bundleIds, i, (void*) iOrg); } } return status; }
void getPermutations(const SETSET_I & in, SETSET_I & out) { // trivial case: if (in.empty()) return; // extract the first set: SET_I set = in[0]; SETSET_I smallerin(in); smallerin.erase(smallerin.begin()); // append the values of the first set to the output: SETSET_I newout; if (out.empty()) { for (unsigned int s = 0; s < set.size(); s++){ SET_I ns; ns.push_back(set[s]); newout.push_back(ns); } } else { for (unsigned int i = 0; i < out.size(); i++){ for (unsigned int s = 0; s < set.size(); s++){ SET_I ns = out[i]; ns.push_back(set[s]); newout.push_back(ns); } } } out = newout; // call again for smaller problem: getPermutations(smallerin,out); }
void getIndexPermutations(const SET_I & in, SETSET_I & out){ SETSET_I newin; for(unsigned int i = 0; i<in.size(); i++){ SET_I ns; for(int j = 0; j<in[i]; j++){ ns.push_back(j); } newin.push_back(ns); } getPermutations(newin, out); }
static void testExport(void) { celix_status_t status; array_list_pt bundleNames = NULL; array_list_pt bundlePermutations = NULL; array_list_pt rsaBundles = NULL; unsigned int i, size; arrayList_create(&bundleNames); arrayList_create(&bundlePermutations); arrayList_create(&rsaBundles); arrayList_add(bundleNames, (void*) DISCOVERY_CFG_NAME); arrayList_add(bundleNames, (void*) RSA_HTTP_NAME); arrayList_add(bundleNames, (void*) TOPOLOGY_MANAGER_NAME); status = getSpecifiedBundles(serverContext, bundleNames, rsaBundles); CHECK_EQUAL(CELIX_SUCCESS, status); CHECK_EQUAL(arrayList_size(rsaBundles), arrayList_size(bundleNames)); status = getPermutations(rsaBundles, 0, arrayList_size(rsaBundles) - 1, bundlePermutations); CHECK_EQUAL(CELIX_SUCCESS, status); size = arrayList_size(bundlePermutations); for (i = 0; i < size; ++i) { long* singlePermutation = (long*) arrayList_get(bundlePermutations, i); status = stopStartPermutation(serverContext, singlePermutation, arrayList_size(rsaBundles)); CHECK_EQUAL(CELIX_SUCCESS, status); /* we need to sleep here for a bit to ensure * that the client has flushed the old discovery * values */ sleep(2); // check whether calc service is available test1(); free(singlePermutation); } arrayList_destroy(bundlePermutations); arrayList_destroy(bundleNames); arrayList_destroy(rsaBundles); }
void Algo::getPermutations(std::string operatorBank[], int operatorCount, std::string permutation[], int permutationLength, int curIndex) { int i; //stop recursion condition if (curIndex == permutationLength) { injectPermutation(permutation, permutationLength); } else { for (i = 0; i < operatorCount; i++) { permutation[curIndex] = operatorBank[i]; getPermutations(operatorBank, operatorCount, permutation, permutationLength, curIndex + 1); } } }
unsigned long long main(unsigned long long argc, char **argv) { // project euler q24 int len = 10; int* digits = malloc(len * sizeof(int)); int i; for (i = 0; i < len; i++) { printf("%i", i); digits[i] = i; } printf("\n\n"); // get array of permutations unsigned long long* permutations = getPermutations(len, digits); printf("%I64d\n", permutations[999999]); // this overflows return 0; }
int main(int argc, char **argv) { char *buffer; size_t bufsize = 32; size_t characters; buffer = (char *)malloc(bufsize * sizeof(char)); printf("To get started, whos score do you want to beat?\n"); getline(&buffer, &bufsize, stdin); removeNewLine(buffer, bufsize); printf("Ok! Time to beat %s!\n", buffer); ///////////////////////////////////// Board board = {0}; Board nextBoard = {0}; while (true) { printBoard(&board); prettyPrintPieces(PIECES); printf("Enter 3 piece numbers:\n"); int pieces[3]; scanf("%d %d %d", &pieces[0], &pieces[1], &pieces[2]); Turn turns[20]; int nTurns = getPermutations(&board, pieces, turns); int t = getMaxTurnIndex(turns, nTurns); printf(">>>>>>>\n"); printTurn(&turns[t]); executeMove(&board, &nextBoard, &(turns[t].moves[0])); executeMove(&nextBoard, &board, &(turns[t].moves[1])); executeMove(&board, &nextBoard, &(turns[t].moves[2])); cleanRows(&nextBoard, &board); } }
unsigned long long* getPermutations(int n, int* arr) { int i; int j; int idx = 0; unsigned long long* perms; unsigned long long* c = malloc(sizeof(unsigned long long) * fact(n)); if (n > 2) // divide { for (i = 0; i < n; i++) { // remove element i from array // recurse w/ shortened arrays perms = getPermutations(n-1, removeElem(arr, arr[i], n)); //printf("%i: %i\n", i, n); // iterate through shortened arrays, appending i to the front of each for (j = 0; j < fact(n-1); j++) { c[idx] = joinIntInt(arr[i], perms[j], n); //printf("%i\n", c[idx]); idx++; } } // return c return c; } else // conquer { // arr[0], arr[1] // arr[1], arr[0] c[0] = joinIntInt(arr[0], arr[1], 2); c[1] = joinIntInt(arr[1], arr[0], 2); return c; } }
int main() { const size_t MAX_PRIMES = 100000; std::bitset<MAX_PRIMES> primes; fillSieveOfAtkin(primes); std::map<size_t, bool> checked; for (size_t n = 1000; n < 9999; n++) { if (!primes[n] || checked[n]) continue; std::string sNumber = std::to_string(n); std::sort(sNumber.begin(), sNumber.end()); int k = std::stoi(sNumber); if (checked[k]) continue; std::vector<size_t> perms = getPermutations(k); std::string res = analyseSequence(perms, checked, primes); if (res != "" && res != "148748178147") { std::cout << "Found: " << res << std::endl; return 0; } } return 0; }