Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Forth > Re: [SPOILER] R...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 6 of 18 Topic 4048 of 4053
Post > Topic >>

Re: [SPOILER] Re: Euler problem #187

by anton@[EMAIL PROTECTED] (Anton Ertl) May 10, 2008 at 01:11 PM

mhx@[EMAIL PROTECTED]
 (Marcel Hendrix) writes:
>anton@[EMAIL PROTECTED]
 (Anton Ertl) writes Re: [SPOILER] Re:
Euler problem #187
>
>> mhx@[EMAIL PROTECTED]
 (Marcel Hendrix) writes:
>> >anton@[EMAIL PROTECTED]
 (Anton Ertl) writes Re: [SPOILER] Re:
Euler problem #187
>[..]
>>>Your code, pasted into Gforth-fast, generates a number that is rejected
by 
>>>the Euler solution checker?
>
>> If I run it on a 64-bit system, the result is correct.  If I run it on
>> a 32-bit system, or if I run it on iForth, it is too high by 1.  It's
>> not obvious where the bug is.

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 have strong evidence that the 64-bit system (and the algorithm)
>are perfect?

Well, having it do the right thing for limit=30 and then getting the
result accepted by the Euler website was made me pretty confident, but
obviously that was overconfidence.

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

However, the reverse loop in SOLUTION can be replaced with a forward
loop here (which could be implemented with DO...LOOP), but if we want
to do the optimization I mentioned at the end of the program, we need
the reverse loop.

BTW, I now think that we can get a significant speedup by making
PRIMES stop when it reaches sqrt(plimit).

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2008:
http://www.complang.tuwien.ac.at/anton/euroforth/ef08.html




 18 Posts in Topic:
[SPOILER] Re: Euler problem #187
anton@[EMAIL PROTECTED]   2008-05-09 20:27:46 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-10 00:42:26 
Re: [SPOILER] Re: Euler problem #187
cac <cac@[EMAIL PROTEC  2008-05-09 16:23:19 
Re: [SPOILER] Re: Euler problem #187
anton@[EMAIL PROTECTED]   2008-05-10 09:43:00 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-10 12:45:19 
Re: [SPOILER] Re: Euler problem #187
anton@[EMAIL PROTECTED]   2008-05-10 13:11:43 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-10 19:29:58 
Counted loops (was: [SPOILER] Re: Euler problem #187)
anton@[EMAIL PROTECTED]   2008-05-10 17:34:43 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-10 20:10:28 
Re: [SPOILER] Re: Euler problem #187
Luca Masini <lmasini@[  2008-05-10 23:54:21 
Re: [SPOILER] Re: Euler problem #187
mhx@[EMAIL PROTECTED] (M  2008-05-11 00:58:55 
more euler problems, was Re: Euler problem #187
Albert van der Horst <  2008-05-12 14:21:47 
[SPOILER] Re: more euler problems
anton@[EMAIL PROTECTED]   2008-05-12 18:43:32 
Re: [SPOILER] Re: more euler problems
cac <cac@[EMAIL PROTEC  2008-05-12 13:26:55 
Re: [SPOILER] Re: more euler problems
anton@[EMAIL PROTECTED]   2008-05-13 10:14:55 
Re: [SPOILER] Re: more euler problems
cac <cac@[EMAIL PROTEC  2008-05-13 10:16:55 
Re: [SPOILER] Re: more euler problems
anton@[EMAIL PROTECTED]   2008-05-13 18:22:29 
Re: [SPOILER] Re: more euler problems
cac <cac@[EMAIL PROTEC  2008-05-13 12:25:24 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu May 15 22:06:08 CDT 2008.