473,322 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

What does this program do?

(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
8 2277
"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

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

"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
> 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
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
> > > 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
13
by: SailFL | last post by:
I have read threads here and there and I have looked at MS but I can not get a clear understanding of what .Net acutally is. I understand that C++ and Vb and other languages are being out a .Net. ...
14
by: Salad | last post by:
On the computer side of the businees there is me, the developer. Another person's role is that of the idea man...the person that knows the business and requirements and issues for the business. ...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
51
by: jacob navia | last post by:
I would like to add at the beginning of the C tutorial I am writing a short blurb about what "types" are. I came up with the following text. Please can you comment? Did I miss something? Is...
126
by: ramyach | last post by:
Hi friends, I need to write a parallel code in 'C' on the server that is running SGI Irix 6.5. This server supports MIPS Pro C compiler. I don't have any idea of parallel C languages. I looked...
37
by: deepu | last post by:
Please reply...
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
7
TRScheel
by: TRScheel | last post by:
I have seen this question appear many times in the forums, so I am writing this to hopefully help those who have this question. First, a history lesson! Your earlier languages compiled to assembly...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.