Erich Neuwirth wrote: > > hello yehuda and george, > > this time, i can't resist. > > i have been following your factorials discussion, issues of speed > and so on. > > the main speed issue in this projects is not programming related, > but simple math. > > 7!=5040 > > so there i no use in trying any digits above 6. > > so instead of > 899 tests, > only > > 6*7*7 = 294 tests are needed. > > so we have reduced the runtime to 1/3 before > writing the first line of code. > > of course, with this method, a simple for loop > is infeasible, > instead, > > (crossmap [list ?1 ?2 ?3] iseq 1 7 iseq 0 7 iseq 0 7) > creates the digit sequences of length 3. > > for any list of 3 digits we want > factval (sum of factorials) > equal numval > (numeric vale) > > filetre selects these triplets nicely. > > so, enclosed is the code. > > could you please compare runtime with your solutions? > > just do > > make.factlist 6 > solve > > here os my code > > -=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > > to fact :n > output item :n + 1 :factlist > end > > to factlist.helper :n > if :n = 0 [output [1]] > local "shorterlist > make "shorterlist factlist.helper :n - 1 > output fput :n * first :shorterlist :shorterlist > end > > to factval :l > output apply "sum map [fact ?] :l > end > > to iseq :a :b > if :a > :b [output []] > output fput :a iseq :a + 1 :b > end > > to make.factlist :n > make "factlist reverse factlist.helper :n > end > > to numval :l > if emptyp :l [output 0] > output (last :l) + 10 * numval butlast :l > end > > to solve > output filter [[x] equalp factval :x numval :x] ~ > (crossmap [(list ?1 ?2 ?3)] iseq 1 6 iseq 0 6 iseq 0 6) > end > > Make "factlist [1 1 2 6 24 120 720 5040 40320 362880] > Hmmm, You've at least twice as much code. It's lot more complex (IMHO). And it runs about half as fast !!! to timeit make "start timemilli make.factlist 9 show solve show (timemilli - :start) / 600 end timeit [[1 4 5]] 3.84833333333333 Why did this happen? I think for 2 reasons. One is you used functions that are intuitively direct but still expensive for the system to execute them (probably for the reason below [crossmap is a library procedure, more code to parse]). Second is what I mentioned in an earlier post. In Logo, parsing smarter code often backfires because it takes longer to parse. I don't disagree that it is a smarter solution. If the core test was more expensive your reduction in executing those test would payoff. That is, you did more work to reduce the number of tests than it takes to execute unnecessary tests themselves. -- =============================================================== George Mills email: mills@softronix.com http://www.softronix.com The www page contains some very powerful educational software. Our single most important investment is our kids. --------------------------------------------------------------- Please post messages to the Logo forum to logo-l@gsn.org. Mail questions about the list administration to logofdn@gsn.org. To unsubscribe send unsubscribe logo-l to majordomo@gsn.org.
Global SchoolNet Foundation -
Linking Kids Around the World!
Copyright GSN - All Rights Reserved
- Comments
& Questions
Visit GSN's
Global
Schoolhouse for more exciting learning resources!
Search our Site
-
Home