By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

What does this program do?

P: n/a
(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;
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:br**********@chessie.cirr.com...
(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.
Nov 14 '05 #2

P: n/a

"Jarmo" <ja***@jarmo.com> wrote in message
news:3f**********************@reading.news.pipex.n et...
"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:br**********@chessie.cirr.com...
(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.

hehheh yep. It's shame that human mind is so playful and doesn't focus on
some really important like... well like this
http://freshmeat.net/projects/libcaca/
;-)

with respect,
Toni Uusitalo
Nov 14 '05 #3

P: n/a
In article <br**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
(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

--
Dave Vandervies dj******@csclub.uwaterloo.ca
But that's only because, underneath my mean, callous exterior, there's
a mean, callous interior waiting to be unleashed.
--Richard Heathfield
Nov 14 '05 #4

P: n/a
Dave Vandervies wrote:
In article <br**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
(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.


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #5

P: n/a

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote

#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.
Nov 14 '05 #6

P: n/a
> 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 :)
Nov 14 '05 #7

P: n/a
Old Wolf <ol*****@inspire.net.nz> spoke thus:
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...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8

P: n/a
> > > for(i=j=k=1;--j||k;k=j?i%j?k:k-j:(j=i+=2));

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...


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 :)
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.