anton@[EMAIL PROTECTED]
(Anton Ertl) writes Re: [SPOILER] Re:
Euler problem #187
> I found and fixed the bug:
> : count-p2 { n1 -- n2 }
> 0 limit 1- n1 / 3 - 2/ 1+ n1 3 - 2/ 0 max +do
> flags i + c@[EMAIL PROTECTED]
+ loop ;
> Without the "0 MAX", for n1=2, this accessed element -1 of FLAGS; this
> element corresponds to the number 1, but is obviously not initialized.
> Apparently it happened that 32-bit Gforth and iForth had a 1 there and
> 64-bit Gforth had a 0 there. Strange, there could have been 254 other
> values there.
You are accessing the header of allocated memory here. With iForth for
Windows, there's debug info from C's heap manager there. Unloading and
reloading the program a few times will produce a different address,
therefore a different header, and therefore a different result
(because memory is not deallocated upon unloading in this program).
[..]
>> I can not read the code with these funny loops.
> Read up on them in
>
http://www.complang.tuwien.ac.at/forth/gforth/Docs-html/Counted-Loops.html
> They are just improved versions of the regular counted loops. I guess
> I would have made another error or two with the regular counted loops.
They may be technically superior, but having to look up the 8 (?)
variants every time undoes any possible benefit for me. I really do
not understand what 'problem' the Gforth manual sees in
: test 0 3 do i . -1 +loop ; test 3 2 1 0 ok
-marcel
-- ----
FORTH> in
Euler187 -- How many composite integers, n < 10^8, have precisely two, not
-- necessarily distinct, prime factors?
ok
FORTH> flags 8 - 16 dump
$0E090028 00 00 00 00 20 00 09 0E--01 01 01 00 01 01 00 01
ok
FORTH> euler187
There are 17427272 composite integers, n < 10^8, having precisely two, not
necessarily distinct, prime factors.
0.250 seconds elapsed. ok


|