Connecting Tech Pros Worldwide Forums | Help | Site Map

What does this program do?

Christopher Benson-Manica
Guest
 
Posts: n/a
#1: Nov 14 '05
(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.

Jarmo
Guest
 
Posts: n/a
#2: Nov 14 '05

re: What does this program do?


"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:brt5sp$fu3$1@chessie.cirr.com...[color=blue]
> (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;
> }[/color]

It wastes people's time reading it.


Toni Uusitalo
Guest
 
Posts: n/a
#3: Nov 14 '05

re: What does this program do?



"Jarmo" <jarmo@jarmo.com> wrote in message
news:3fe21d31$0$9385$ed9e5944@reading.news.pipex.n et...[color=blue]
> "Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
> news:brt5sp$fu3$1@chessie.cirr.com...[color=green]
> > (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;
> > }[/color]
>
> It wastes people's time reading it.
>[/color]
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


Dave Vandervies
Guest
 
Posts: n/a
#4: Nov 14 '05

re: What does this program do?


In article <brt5sp$fu3$1@chessie.cirr.com>,
Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote:[color=blue]
>(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;
>}[/color]

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 dj3vande@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
Kevin Goodsell
Guest
 
Posts: n/a
#5: Nov 14 '05

re: What does this program do?


Dave Vandervies wrote:
[color=blue]
> In article <brt5sp$fu3$1@chessie.cirr.com>,
> Christopher Benson-Manica <ataru@nospam.cyberspace.org> wrote:
>[color=green]
>>(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;
>>}[/color]
>
>
> 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.[/color]

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.
Malcolm
Guest
 
Posts: n/a
#6: Nov 14 '05

re: What does this program do?



"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote[color=blue]
>
> #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;
> }
>[/color]
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.


Old Wolf
Guest
 
Posts: n/a
#7: Nov 14 '05

re: What does this program do?


> int main(void)[color=blue]
> {
> 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;
> }[/color]

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 :)
Christopher Benson-Manica
Guest
 
Posts: n/a
#8: Nov 14 '05

re: What does this program do?


Old Wolf <oldwolf@inspire.net.nz> spoke thus:
[color=blue]
> 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).[/color]

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.
Old Wolf
Guest
 
Posts: n/a
#9: Nov 14 '05

re: What does this program do?


> > > for(i=j=k=1;--j||k;k=j?i%j?k:k-j:(j=i+=2));[color=blue][color=green]
> >
> > 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).[/color]
>
> 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...[/color]

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 :)
Closed Thread


Similar C / C++ bytes