Skip to content

Instantly share code, notes, and snippets.

@croepha
Created December 2, 2018 08:02
Show Gist options
  • Save croepha/e90d2b164c224f53b230e7b6ef2556be to your computer and use it in GitHub Desktop.
Save croepha/e90d2b164c224f53b230e7b6ef2556be to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <stdio.h>
typedef float (*op_t)(float, float);
float op_add (float l, float r) { return l + r; }
float op_sub (float l, float r) { return l - r; }
float op_subr(float l, float r) { return r - l; }
float op_mul (float l, float r) { return l * r; }
float op_div (float l, float r) { return l / r; }
float op_dvr (float l, float r) { return r / l; }
op_t ops [] = {op_add, op_sub, op_subr, op_mul, op_div, op_dvr};
char* ops_s[] = { "+", "-", "--", "*", "/", "\\"};
int op_count = 6;
int main() {
float nums[] = {1, 3, 4, 6};
std::sort(nums, nums + 4);
int iteration_count = 0;
do {
for (int o0=0; o0<op_count; o0++) {
for (int o1=0; o1<op_count; o1++) {
for (int o2=0; o2<op_count; o2++) {
iteration_count++;
float r = nums[0];
r = ops[o0](r, nums[1]);
r = ops[o1](r, nums[2]);
r = ops[o2](r, nums[3]);
if (r == 24 ) {
printf("(((%d %s %d) %s %d) %s %d) = %f\n",
(int)nums[0], ops_s[o0],
(int)nums[1], ops_s[o1],
(int)nums[2], ops_s[o2],
(int)nums[3], r);
fflush(stdout);
}
}
}
}
} while (std::next_permutation(nums, nums+4));
printf("iteration_count: %d\n", iteration_count);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment