Re: MLM Numbers Game
My code with fuller comments:
function get_tiers(integer pop,sequence tiers)
--tiers is a sequnce of ratios, first element is ratio of
--lowest level of leaders to grunts, second is ratio
--of second-lowest level of leaders to lowest level, etc.
--returns a sequence one longer than tiers, with the number of
--each rank from grunts on up.
--No error checking is done.
--Comments below for the {20,10,10,5} test case
integer num_tiers
num_tiers=length(tiers)
if num_tiers=0 then return {pop} end if
tiers[1]+=1
-- for level 1 (ratio of 20) 20+1 = 21 needed to support 1
for i=2 to num_tiers do
tiers[i]=tiers[i]*tiers[i-1]+1
-- for level 2 (ratio 10) 10*21+1 = 211 needed to support 1
-- for level 3 (ratio 10) 10*211+1 = 2111 needed to support 1
-- for level 4 (ratio 5) 5*2111+1 =10556 needed to support 1
end for
for i=num_tiers to 1 by -1 do
tiers[i]=floor(pop/tiers[i])
-- first pass calculates level 4's
-- second pass calulates level 3's based on population
-- reduced by number of level 4's, etc.
pop-=tiers[i]
-- reduce population
end for
-- original code was:
-- tiers=floor(pop/tiers)
-- for i=num_tiers to 1 by -1 do
-- pop-=tiers[i]
-- end for
-- this calculates all the numbers at once using Euphoria sequences, but
-- contains a subtle error:
-- 2110 population will support 10 level 1's, but 2321 will not
-- support 11 level 1's: 1 extra population is needed for the level 2!
return pop & tiers
end function
-- Mike Nelson
|
Not Categorized, Please Help
|
|