Newsgroups: comp.lang.forth
From: an...@mips.complang.tuwien.ac.at (Anton Ertl)
Date: Wed, 07 May 2008 20:50:24 GMT
Local: Wed, May 7 2008 4:50 pm
Subject: Re: Euler problem #63
William James <w_a_x_...@yahoo.com> writes: I find that the conversion between integer and FP, with a double-cell >On May 5, 11:49 am, an...@mips.complang.tuwien.ac.at (Anton Ertl) >> : n-digits { n -- n2 } >> \ number of n-th powers with n digits >> \ for n=1, it does not count 0 and 1 (I correct for the 1 below) >> \ this works by just seeing what the lowest x is that fits in >> \ 10^(n-1), and then rounding x up to an integer >> n 1- 0 d>f n 0 d>f f/ falog f>d drop 9 swap - ; >Clever, but the stack makes it hard to follow the code. intermediate step makes it a little bit convoluted. One can fix that with factoring (and using FLOOR, which I have hitherto overlooked): : u>f 0 d>f ; >> : solve ( -- ) With N-DIGITS1, it becomes a little easier: >> 1 1 begin ( n sum ) >> over n-digits dup 0> while >> + swap 1+ swap repeat >> drop nip ; >Again, the stack makes it very difficult to follow. : solve ( -- r ) Or you can use locals to reduce the stack load: : solve ( -- r ) But given the tone of your posting, I guess you think that the stack 1) Get some more practice in reading Forth or other stack-based 2) Conclude that stack-based languages are not for you and stick with - anton You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
| ||||||||||||||