### Kristen Lew Homework 2 for Experimental Math ## Spring 2012, post if you wish. # Help: show available functions Help:=proc(): print( ` GC(x,L,n), Parent(x), kCousins(x,k) (CW(x), CWlist(L)) `): end: ### Problem 2 ## GC(x, L, n): The general Collatz function GC:=proc(x, L, n) local i: for i from 1 to nops(L) do if i=n mod nops(L) then RETURN(subs(x=n,L[i])): elif 0=n mod nops(L) then RETURN(subs(x=n,L[nops(L)])): fi: od: end: ### Problem 3 # Optional for Novices ### Problem 4 # Optional for Novices ### Problem 5 ## Parent(x): inputs a positive rational number and outputs its unique parent. Parent:=proc(x) local a, b: if numer(x) < denom(x) then a:=numer(x): b:=denom(x)-numer(x): else b:=denom(x): a:=numer(x)-denom(x): fi: a/b: end: ### Problem 6 ## kCousins(x,k): inputs a fraction x and a positive integer k # and outputs the list of k-th degree cousins of x. kCousins:=proc(x,k) local cousins, i, j, gp, GP: gp:=x: # find the (k-2)th great grandparent for j from 1 to k do gp:=Parent(gp): j=j+1: od: # find the sibling of the (k-2)th great grandparent GP:=CW(Parent(gp)): if GP[1]=gp then cousins:=[GP[2]]: else cousins:=[GP[1]]: fi: # find cousins for i from 1 to k do cousins:=CWlist(cousins): od: cousins: end: ## CW(x): The two children (given a pair) of the fraction x CW:=proc(x) local a,b: a:=numer(x): b:=denom(x): [a/(a+b),(a+b)/b]: end: ## CWlist(L): the list, in order of all the Calkin-Wilf children # of the list of fractions L CWlist:=proc(L) local i: [seq( op(CW(L[i])), i=1..nops(L) ) ]: end: