What does this program do?

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

(don't worry, it isn't homework or anything... just a brain-teaser if
you're so inclined)

#include <stdlib.h>

int main(void)
{
unsigned long i,j,k;

for(i=j=k=1;--j||k;k=j?i%j?k:k-j:(j=i+=2));
return EXIT_SUCCESS;
}
 
J

Jarmo

Christopher Benson-Manica said:
(don't worry, it isn't homework or anything... just a brain-teaser if
you're so inclined)

#include <stdlib.h>

int main(void)
{
unsigned long i,j,k;

for(i=j=k=1;--j||k;k=j?i%j?k:k-j:(j=i+=2));
return EXIT_SUCCESS;
}

It wastes people's time reading it.
 
D

Dave Vandervies

(don't worry, it isn't homework or anything... just a brain-teaser if
you're so inclined)

#include <stdlib.h>

int main(void)
{
unsigned long i,j,k;

for(i=j=k=1;--j||k;k=j?i%j?k:k-j:(j=i+=2));
return EXIT_SUCCESS;
}

Since you're not outputting anything, the options are "infinite loop" or
"spin wheels and eventually return", with varying amounts of time that the
"spin wheels" part could take. The "spin wheels and eventually return"
option can be converted by a sufficiently aggressive optimizer to "return
immediately", though the general case for that may require solving the
halting problem.

It looks to me like it's an infinite loop.

(I'll give some other people a chance to see it and agree or disagree
with me and, if I remember, post my analysis later.)


dave
 
K

Kevin Goodsell

Dave said:
Since you're not outputting anything, the options are "infinite loop" or
"spin wheels and eventually return", with varying amounts of time that the
"spin wheels" part could take.

While I don't see any obvious way for it to occur in this particular
example, undefined behavior resulting in who-knows-what could be another
option for a program with this basic structure. But since the variables
are unsigned, and are given determinate values, and it doesn't appear to
violate any of the rules regarding sequence points and modifying values,
I don't think there can be any undefined behavior.

-Kevin
 
M

Malcolm

Christopher Benson-Manica said:
#include <stdlib.h>

int main(void)
{
unsigned long i,j,k;

for(i=j=k=1;--j||k;k=j?i%j?k:k-j:(j=i+=2));
return EXIT_SUCCESS;
}
It is extremely easy to write expressions which are parsable by computer but
almost impossible for humans to understand. This is particularly the case in
C, where constructs like the for() loop allow arbitrary code to be placed in
any of the three sub-expressions.
 
O

Old Wolf

int main(void)
{
unsigned long i,j,k;

for(i=j=k=1;--j||k;k=j?i%j?k:k-j:(j=i+=2));
return EXIT_SUCCESS;
}

If I can answer without giving spoilers, it inefficiently
attempts to solve a problem suggested by Descartes in 1638
(but is actually an infinite loop, as current systems have
woefully too few bits in their registers).

It also makes one think about such things as optimization
and the halting problem -- arguably a greater goal than
the first one I mentioned :)
 
C

Christopher Benson-Manica

Old Wolf said:
If I can answer without giving spoilers, it inefficiently
attempts to solve a problem suggested by Descartes in 1638
(but is actually an infinite loop, as current systems have
woefully too few bits in their registers).

Well done :) Just out of curiousity, how did you figure it out? I
must admit, I neither wrote this nor figured out what it did on my own
- I got it from a web page discussing the halting problem...
 
O

Old Wolf

for(i=j=k=1;--j||k;k=j?i%j?k:k-j:(j=i+=2));
Well done :) Just out of curiousity, how did you figure it out? I
must admit, I neither wrote this nor figured out what it did on my own
- I got it from a web page discussing the halting problem...

I added a printf to display i,j,k at each step (incidentally,
producing megabytes of output per second), then stared at it
until I saw what it was doing, and then looked the problem up on
wolfram.com to find the background info and latest status of it :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top