Iterating a Diophantine equation over a list of itself in python -
i'm self-studying mit open courseware introduction computer science , programming. problem set 2 involves diophantine equations based on counting sums of chicken nugget boxes (6, 9, or 20).
the way thought creating algorithm creating virtual measuring stick (like in wood shop), sizes of measurements (values) noted on stick , transferred piece.
if imagined being used on number line, point out initial values, mark spots and, then, i'd move 0 point first mark came across , make new marks, , repeat that, going forwards.
so, take formula of sums: x=0, a=x+6, b=x+9, c=x+20, each appended list, looks this: [6,9,20]. want do, have x switch next greatest value in list, creating new values other variables appended list. next value x 6, changing other variables , resulting in list looks this: [6,9,20,12,15,26]. then, 9, , on.
my first problem duplicates , order of list causing infinite loops. corrected adding line created set of list turned list again. worked mostly.
but, , here problem, doesnt fill list values possible. doesnt put 48 list, example, obvious multiple of 6.
my guess problem has fact i'm iterating on list being edited and/or appended to.
i know there algorithms redesign assignment using, because did research , saw how others coded answers problem, , understand now, i'd still fix own way of programming solution problem. here's code far:
## name initial variables in diophantine equations x=0 a, b, c= x+6, x+9, x+20 ## create list catch values, aka fish fish = [a,b,c] ## here goes while x <= 60: in fish: x = fish = list(sorted(fish)) a, b, c= x+6, x+9, x+20 ## option see doing print x,a,b,c ## catch values list fish.append(a) fish.append(b) fish.append(c) ## rid of duplicate numbers in list fish = list(set(fish)) a, b, c= x+6, x+9, x+20 else: print fish ## create new list of values not caught equation lastmissingfish = list(set(range(0,50))-set(fish)) ## print last number not reached equation print lastmissingfish [-1]
modifying same collection iterating on not idea, because iteration expects underlying collection not modified. in particular, iterating on list in python while modifying seems have...quirky results. try changing explicitly index list , increment index instead, so:
## create list catch values, aka fish fish = [0] ## here goes x, = 0, 0 while x <= 60 , < len(fish): x = fish[i] a, b, c = x+6, x+9, x+20 ## catch values list fish.append(a) fish.append(b) fish.append(c) ## rid of duplicate numbers in list. fish = sorted(set(fish)) = + 1 for reference, here's how i'd solve it. numbers want add set can expressed multiple of 6 + multiple of 9 + multiple of 20. let's iterate on of them, create list out of our set elements less or equal max:
def solvechickenboxes(max, x, y, z): sums = set() in range((max/x)+1): b in range((max/y)+1): c in range ((max/z)+1): sums.add(a*x + b*y + c*z) return [x x in sums if x <= max] solvechickenboxes(50, 6, 9, 20) [0, 6, 9, 12, 15, 18, 20, 21, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 40, 41 , 42, 44, 45, 46, 47, 48, 49, 50]
Comments
Post a Comment