Skip to content

Instantly share code, notes, and snippets.

@ercas
Last active September 10, 2015 22:30
Show Gist options
  • Save ercas/b96a7860532e3c6f6b3d to your computer and use it in GitHub Desktop.
Save ercas/b96a7860532e3c6f6b3d to your computer and use it in GitHub Desktop.
/* reimplementation of simpsons-bruteforce.lua in c for practice */
#include <stdio.h>
#define INCREMENT 5
#define a_part 210
#define a_whole 300
#define b_part 180
#define b_whole 300
int main(void) {
unsigned int a_full_part, a_full_whole, b_full_part, b_full_whole;
unsigned int a_part_part, a_part_whole, b_part_part, b_part_whole;
float a_full_proportion, a_part_proportion, b_full_proportion, b_part_proportion;
for (a_full_part = 0; a_full_part <= a_part; a_full_part = a_full_part + INCREMENT) {
a_part_part = a_part - a_full_part;
for (a_full_whole = 0; a_full_whole <= a_whole; a_full_whole = a_full_whole + INCREMENT) {
a_part_whole = a_whole - a_full_whole;
for (b_full_part = 0; b_full_part <= b_part; b_full_part = b_full_part + INCREMENT) {
b_part_part = b_part - b_full_part;
for (b_full_whole = 0; b_full_whole <= b_whole; b_full_whole = b_full_whole + INCREMENT) {
b_part_whole = b_whole - b_full_whole;
if ((a_part_whole > 0) && (a_full_whole > 0) && (b_part_whole > 0) && (b_full_whole > 0)) {
a_full_proportion = (float)a_full_part / a_full_whole;
a_part_proportion = (float)a_part_part / a_part_whole;
b_full_proportion = (float)b_full_part / b_full_whole;
b_part_proportion = (float)b_part_part / b_part_whole;
if ((a_full_proportion < 1) && (a_part_proportion < 1) && (b_full_proportion < 1) && (b_part_proportion < 1)
&& (b_full_proportion > a_full_proportion) && (b_part_proportion > a_part_proportion)) {
printf("a full: %4d/%4d, b full: %4d/%4d\n",a_full_part,a_full_whole,b_full_part,b_full_whole);
printf("a part: %4d/%4d, b part: %4d/%4d\n",a_part_part,a_part_whole,b_part_part,b_part_whole);
printf("\n");
}
}
}
}
}
}
}
#!/usr/bin/lua
--[[
quick and messy script to bruteforce chapter 3 exercise #40, the simpsons
paradox problem, of the stats: modeling the world textbook
how this script works:
1. iterate through all possible proportions for company a full time, company
a part time, company b full time, and company b part time by iterating
through all possible combinations of their numerators and denominators
2. if:
b full time proportion > a full time proportion
and:
b part time proportion > a part time proportion
then print the individual numerators and denominators. these are the
answers to the question.
--]]
a_part=210
a_whole=300
b_part=180
b_whole=300
increment=5
function printf(s,...)
return io.write(string.format(s,...))
end
function isinteger(num)
if math.floor(num)/num == 1 then
return true
else
return false
end
end
-- no zeroes
function checknumbers(...)
for _,num in pairs({...}) do
if --[[not isinteger(num) or]] num == 0 then
return false
end
end
return true
end
-- no >1 proportions
function checkproportions(...)
for _,proportion in pairs({...}) do
if proportion > 1 then
return false
end
end
return true
end
for a_full_part=0,a_part,increment do
a_part_part=a_part-a_full_part
for a_full_whole=0,a_whole,increment do
a_part_whole=a_whole-a_full_whole
for b_full_part=0,b_part,increment do
b_part_part=b_part-b_full_part
for b_full_whole=0,b_whole,increment do
b_part_whole=b_whole-b_full_whole
a_full_proportion=a_full_part/a_full_whole
a_part_proportion=a_part_part/a_part_whole
b_full_proportion=b_full_part/b_full_whole
b_part_proportion=b_part_part/b_part_whole
--print(check(a_full_part,a_full_whole,b_full_part,b_full_whole,a_part_part,a_part_whole,b_part_part,b_part_whole),a_full_part,a_full_whole,b_full_part,b_full_whole,a_part_part,a_part_whole,b_part_part,b_part_whole)
if (b_full_proportion > a_full_proportion) and (b_part_proportion > a_part_proportion)
and checknumbers(a_full_part,a_full_whole,b_full_part,b_full_whole,a_part_part,a_part_whole,b_part_part,b_part_whole)
and checkproportions(a_full_proportion,a_part_proportion,b_full_proportion,b_part_proportion) then
printf("a full: %4d/%4d, b full: %4d/%4d\n",a_full_part,a_full_whole,b_full_part,b_full_whole)
printf("a part: %4d/%4d, b part: %4d/%4d\n",a_part_part,a_part_whole,b_part_part,b_part_whole)
printf("\n")
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment