Spoiler for C++ prime challenge
-
Posted intentionally in a separate area from the challenge itself, for what it's worth. This is my solution to the Prime Challenge twist (C++ challenge):
<font style="color: rgb(0, 0, 0); background-color: rgb(0, 0, 0);" size="1">// =============================================================
// Summary:
// =============================================================
// Divides - Test for divisibility
// Prime - Test for primality
// NextPrimeInRange - Looks for next prime number
// PrimeList - Collects the primes for reporting, and
//
generates the error triggering output
// SearchForPrimes - Starts the prime list
// =============================================================
// =============================================================
// Divides<a,b>::result is true if a divides b
// =============================================================
template <int a, int b, int isLess=false>
struct Divides
{
static const bool result = Divides< a, b-a, (b-a<a) >::result;
};
// Termination case
template <int a, int b>
struct Divides<a, b, true> { static const bool result=b==0;};
// =============================================================
// Prime<test>::result is true if test is prime
// =============================================================
template <int test,
int curtest=1+(test>>1),
bool knownComposite=false,
bool knownPrime=false>
struct Prime
{
static const bool result
= Prime< test, curtest-1,
Divides<curtest, test>::result,
curtest<2 >::result;
};
// Terminating case--if we searched everything, this is prime
template<int test, int curtest, bool knownComposite>
struct Prime<test, curtest, knownComposite, true>
{
static const bool result = true;
};
// Terminating case--if we found something that divides this,
// this is composite
template<int test, int curtest>
struct Prime<test, curtest, true>
{
static const bool result=false;
};
// =============================================================
// NextPrimeInRange<start,end>::result is the first prime
// between start and end, if there is one, or is zero.
// =============================================================
template<int start, int end, bool found=false,bool eos=false>
struct NextPrimeInRange
{
static const int result
= NextPrimeInRange<start+1, end,
Prime<start>::result,
(start>=end)
>::result;
};
template<int start,int end,bool found>
struct NextPrimeInRange<start,end,found,true>
{
static const int result=0;
};
template<int start,int end>
struct NextPrimeInRange<start, end, true, false>
{
static const int result=start-1;
};
// =============================================================
// Range is just a readability template for PrimeList
// (only serves to make errors readable in certain compilers,
// such as MSVC)
// =============================================================
template<int rStart, int rEnd>
struct Range
{
static const int end=rEnd;
};
// =============================================================
// PrimeList<RESULT, Range<,end> > is a list of primes.
// Each RESULT walks a prime, until the end of the range is
// reached, at which case PrimeList generates an error
// (by trying to access a private member). The error displays
// the resulting list of primes.
// =============================================================
template<int RESULT, typename InRange>
struct PrimeList
{
static const int result
= PrimeList<NextPrimeInRange<RESULT+1,InRange::end>::result,
InRange>::result;
};
template<typename EndOfSearch>
class PrimeList<0,EndOfSearch>
{
static const int result = 0;
};
// =============================================================
// SearchForPrimes<start,end> primes (pun intended) the search
// by finding the next prime after start, and starting a
// PrimeList.
// =============================================================
template<int start,int end>
struct SearchForPrimes
{
static const int result
= PrimeList<NextPrimeInRange<start,end>::result,
Range<start,end> >::result;
};
int Result = SearchForPrimes<100,200>::result;
int main() {return 0;}
</font>
-
Did you black it out on purpose or are the sinister (or kinky?) forum gods at it again?
-
Well, if you highlight the blacked-out text, you can see the code....
-
@Ulvhamne said:
Did you black it out on purpose or are the sinister (or kinky?) forum gods at it again?
On purpose--just believe it to generally be good form to make spoilers
unreadable via casual viewing. In my mind, it's easy to accidentally
click on a thread and glance at the spoiler, but it's pretty hard to
accidentally click on the thread and highlight the spoiler.