Boost logo

Boost :

From: Brian McNamara (lorgon_at_[hidden])
Date: 2004-03-02 05:59:48


On Tue, Mar 02, 2004 at 05:31:36AM -0500, Brian McNamara wrote:
> Below I am attaching a revised version of your code and the FC++ code,
> where the FC++ version uses your primality test.

Just in case anyone construes this as "cheating" the FP paradigm
(calling out to code which uses side effects and loops), here is an
alternate version which is totally pure:

   struct Prime : public c_fun_type<int,bool> {
      bool operator()( int x ) const {
         //return is_prime(x); // cheating?
         lambda_var<1> C;
         return until( lambda(C)[ greater[C,x/2] %logical_or%
                                  equal[x %modulus% C,0] ],
                       inc, 2 ) > x/2;
      }
   } prime;

This version does run about 20% slower on my machine, but I think 20% is
tolerable whereas 32000% is clearly not. (In any case, I did not put any
thought into making this version run fast--I just transcribed the loop
into equivalent pure code the first way that sprang to mind.)

-- 
-Brian McNamara (lorgon_at_[hidden])

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk