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