#include #include "prelude.h" using std::cout; using std::endl; using namespace fcpp; #ifndef NUM #define NUM 1000 #endif struct Divisible : public CFunType { bool operator()( int x, int y ) const { return x%y==0; } } divisible; struct Factors : public CFunType > { OddList operator()( int x ) const { return filter( curry2(divisible,x), enumFromTo(1,x) ); } } factors; struct Prime : public CFunType { bool operator()( int x ) const { return factors(x)==cons(1,cons(x,NIL)); } } prime; struct Primes : public CFunType > { OddList operator()( int n ) const { return take( n, filter( prime, enumFrom(1) ) ); } } primes; #ifdef REAL_TIMING # include "timer.h" #else struct Timer { int ms_since_start() { return 0; } }; #endif int main() { Timer timer; cout << "Num primes = " << NUM << endl; int start = timer.ms_since_start(); List l = primes(NUM); cout << at( l, NUM-1 ) << endl; int end = timer.ms_since_start(); cout << "took " << end-start << " ms" << endl; cout << endl; }