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

LOGO-L> Re: factorials



Note that it does not help that much improving the compute_facts
because it is now dominated by the compare and fact lookups
once the cache was added.

Interestingly the code actually looks cleaner with this optimization.
It certainly helps performance but not as much as you might of
hoped for.

Also note the one line of code I changed below for the purist.
Although some purists might like it the way you had it.

Yehuda Katz wrote:
> 
> Hi,
> 
> The problem was, to remind, to find all 3-digit numbers thar equal the
> sum of their factorials.
> 
> Giving a second thought, you might find that there's a waste in
> calculating all those factorials, for the following reason:
> In calculating 6! - for example - you have to calculate 5! from the
> beginning, AFTER it has already been calculated and even added to the
> array!
> 
> So, you can build directly the array by using the fact that 0!=1, and
> that the Nth element of the array in N*(the N-1 element).
> 
> This leads to something like the following program:
> 
> ===================================================
> to main
> compute_facts
> solve
> end
> 
> to compute_facts
> make "fact_array(array 10 0)
> setitem 0 :fact_array 1
> for[i 1 9][
>     setitem :i :fact_array :i*item :i-1 :fact_array]

     setitem :i :fact_array :i*fact :i-1]

> end
> 
> to solve
> for[num 100 666][
> if equalp :num(sum fact item 1 :num
>                    fact item 2 :num
>                    fact item 3 :num)
>    [pr :num]]
> end
> 
> to fact :n
> op item :n :fact_array
> end
> ===================================================
> 
> What makes me wonder is: Could memory allocation to arrays in Logo be
> dynamic?
> 
> Regards...
> 
> [[Yehuda]]

-- 
===============================================================
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