luserXtrog said:
I wondered, but was too embarrassed to ask.
I was able to wade through all the pieces, but couldn't
grasp the whole. I you feel an urge to explain, be
assured: ears are open!
Well, if you insist ...
The assignment, in essence, is to print the value 110. Rather than
computing it as 2+4+6+8+10+12+14+16+18+20, I decided to start with the
prime factorization of 110: 2*5*11. Those happen to be the first,
third, and fifth (i.e., alternating) prime numbers.
(Embarrassing confession: I initially miscomputed the result as 210,
which is the product of the first 4 primes, 2*3*5*7; I was going to
use that, but fortunately I redid the sum. Sometimes it's the simple
things that trip you up.)
The function p, as the name nearly suggests, returns a result
that indicates whether its argument is a prime number, using a
rather inefficient but straightforward method. But rather than
just returning 0 or 1, I decided to have it return a null or
non-null function pointer -- which can be used the same way in a
conditional context. The non-null result is the address of main.
The null result is a rather elaborate null pointer constant, which,
after deleting the parentheses (which I should have done before
posting the code) is:
+'/'/+'/'+-'/'/'/'
I started with the infamous constant expression '/'/'/', which is the
numeric value of the '/' character divided by itself, which is 1 (just
using '-'-'-' would have been too easy). Subtracting it from itself
gives '/'/'/' - '/'/'/', which is 0, which is therefore a null pointer
constant. Then I threw in a couple of unary "+" operators. The whole
thing is equivalent to "return 0;".
In the main program, s is the sum (which is actually computed as a
product), and t is a toggle (its low-order bit alternates between 0
and 1). The initializer for t has the side effect of setting s to 1,
which is where it needs to start. I loop over all integers from 2
to 13, selecting just the primes, and then use the alternating
low-order bit of t to select every other prime. So the program
ends up computing 2*5*11, which is the result we were looking for.
One could argue that this doesn't actually meet the requirement,
since it doesn't actually "CALCULATE THE SUM" -- but it does perform
a calculation whose result is the correct sum.
It was probably too much to hope that the original poster would
try to turn this in; by the time I posted it was pretty obvious
what we were up to.