Math problem
Kay C Lan
lan.kc.macmail at gmail.com
Sun Feb 19 01:58:07 EST 2012
On Sun, Feb 19, 2012 at 12:22 PM, Geoff Canyon Rev <gcanyon+rev at gmail.com>wrote:
> (and to return hours, so it matches the output of the other
> two).
>
OK, now I see where I was getting confused. I was focused on decimal hours
as per the OP, but you were outputting seconds.
I've tweaked your solution only slightly as you solution only worked for
quarter hour increment, whilst both mine and Paul's would work for any
required increment . I simply replace your fixed 4 with the 3600/increment
that both Paul and I were using.
With this correction it seems Paul slips into the lead:
For 1000000 cycles
K's solution = 2378ms
Paul's solution = 2021ms
Geoff's solution = 2283ms
To include a variable increment I've used an array rather than the simple
list you used in your script, which seems to have slowed things down, as I
got similar times to your original output, but still I'm surprised it's
twice as slow.
What I would like to point out, is following this thread and the many
variations that have been offered, when finally distilled down the final
three solutions really only involve very basic math.
on mouseUp
put 1000000 into tRepeats
put 1329494400 into tStartTime
--create an array of variable end times and increments
repeat with i = 1 to tRepeats
put (1329494400 + random(36000)) into aTime[i]["End"]
switch random(5)
case 1
put 300 into aTime[i]["Increment"]
break
case 2
put 600 into aTime[i]["Increment"]
break
case 3
put 900 into aTime[i]["Increment"]
break
case 4
put 1200 into aTime[i]["Increment"]
break
case 5
put 1800 into aTime[i]["Increment"]
break
end switch
end repeat
--K solution
put the millisec into tStartClock
repeat for each key i in aTime
put round(((aTime[i]["End"] + (aTime[i]["Increment"]/2) - 1 - \
tStartTime)/aTime[i]["Increment"]),0)/(3600/aTime[i]["Increment"]) & cr
after tStore2
end repeat
put the millisec - tStartClock into tTotalTime1
--Paul's maxless solution
put the millisec into tStartClock
repeat for each key i in aTime
put round(((aTime[i]["End"] -tStartTime)/aTime[i]["Increment"])+\
0.4999,0)/(3600/aTime[i]["Increment"]) & cr after tStore3
end repeat
put the millisec - tStartClock into tTotalTime2
if (tStore2 <> tStore3) then
put "Paul's solution doesn't = K's" & cr after tErrors
end if
--Geoff's revised any increment solution
put the millisec into tStartClock
repeat for each key i in aTime
put (aTime[i]["End"] - tStartTime + aTime[i]["Increment"] - 1) \
div aTime[i]["Increment"] / (3600/aTime[i]["Increment"]) & cr after
tStore4
end repeat
put the millisec - tStartClock into tTotalTime3
if (tStore2 <> tStore4) then
put "Geoff's solution doesn't = K's" & cr after tErrors
end if
if (tStore3 <> tStore4) then
put "Geoff's solution doesn't = Paul's" & cr after tErrors
end if
put "For " & tRepeats & " cycles" & cr into R
put "K's solution = " & tTotalTime1 & "ms" & cr after R
put "Paul's solution = " & tTotalTime2 & "ms" & cr after R
put "Geoff's solution = " & tTotalTime3 & "ms" & cr after R
put R & tErrors
end mouseUp
More information about the use-livecode
mailing list