[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

LOGO-L> factorials, once more



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]

--
Erich Neuwirth <neuwirth@smc.univie.ac.at>
Computer Supported Didactics Working Group, University of Vienna
Currently Visiting Professor at the 
National Institute of Multimedia Education, Chiba, Japan
Visit our SunSITE at http://sunsite.univie.ac.at
---------------------------------------------------------------
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