Archived

This topic is now archived and is closed to further replies.

Sheep

what does this do?

Recommended Posts

Sheep    152
Hello everyone. I was just wondering if you guys could rewrite the program that I found somewhere to compute pi? I looked over it a million times, and all of these null for statements and commas inside for statements are confusing me(I dont get what they are doing...) If anyone of you can rewrite this for me, well, thanks.
  #include <stdio.h>

int a=10000,b,c=2800,d,e,f[2801],g;
void main()
{
	for(;b-c;)f[b++]=a/5;
    for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,
    f[b]=d%--g,d/=g--,--b;d*=b);  
Thanks.

Share this post


Link to post
Share on other sites
Arild Fines    968
This looks like an entry to an obfuscation contest. If you''re seriously want an algorithm to compute pi, I suggest you keep on searching.

"A society without religion is like a crazed psychopath without a loaded .45"

Share this post


Link to post
Share on other sites
TerranFury    142

const double PI = atan(1) * 4;


See the following site for info on how to calculate everything yourself:
http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibpi.html

Share this post


Link to post
Share on other sites
Oluseyi    2109
  
#include <stdio.h>
//

// Overly verbose "de-obfuscation"

//

// Interestingly, that first line wouldn''t compile on some

// compilers because they require declarations to occur before

// initializations in the same statement.

int a = 10000;
int b;
int c = 2800;
int d;
int e;
int f[2801];
int g;
//

void main()
{
// array intialization:

for( ; // no initial condition specified

b-c; // when b = c, this evaluates to zero - false

) // increment occurs within loop body

{
f[b]=a/5; // set bth element to a/5

b = b+1; // increment b

}
//

// determination of pi:

for( ; // no intial condition

d=0, g=c*2; // comma operator executes both "sub-statements"

c-=14 ) // decrement c by 14

{
// as before, the comma operator was used to relocate the

// loop body to the increment portion of the loop

printf("%.4d",e+d/a);
e=d%a;
//

// by now you should be familiar with the comma operator

for(b=c;
// this loop probably terminates when d evaluates

// to zero (? anyone who thinks otherwise, please

// post)

d+=f[b]*a; // d = d + (f[b] * a)

d*=b)
{
f[b]=d%--g; // g = g - 1; f[b] = remainder of d / g

d/=g--; // d = d / g; g = g - 1

--b; // b = b -1

}
}
}


The code relies on the comma operator to syntactically place multiple statements in one, but that gets expanded at compile time. It also employs operator precedence, associativity and execution order to concatenate assignments and pre- or post-increments.

Share this post


Link to post
Share on other sites
Sheep    152
Ok, I get most of it. Its just this part that I dont get
  
// array intialization:

for( ; // no initial condition specified

b-c; // when b = c, this evaluates to zero - false

) // increment occurs within loop body

{
f[b]=a/5; // set bth element to a/5

b = b+1; // increment b

}

I dont get how the for statement works. You say that when b=c then "this" evaluates to zero. What evaluates to zero? b-c? If so, then when will this happen, because the loop does not do anything to "c"? Also, when will the loops stop? when b=c? I know my question probably makes no sense, but that is only because I have no clue what this loop does at ALL. NOTE: I am not looking at this for a way to compute pi...just a challenging program that I dont have no clue how to understand. Thanks again.

Share this post


Link to post
Share on other sites
Sheep    152
Ohhhhhhhhh!!! Ok, now I get it. With that information given, I have greatly simplified the program to this:

  
#include <stdio.h>

int a=10000,b,c=2800,d,e,f[2801],g;

void main()
{
while((b-c) != 0)
{
f[b]=a/5;
b = b + 1;
}
while(true)
{
d = 0;
g=c*2;
if(g == 0)
break;
b = c;
while(true)
{
d= d + f[b]*a;
g = g - 1;
f[b] = d % g;
d = d / g;
g = g - 1;
--b;
if(b == 0)
break;
d*= b;
}
c = c - 14;
printf("%.4d",e+d/a);
e = d % a;
}
}


I am so proud of myself

Share this post


Link to post
Share on other sites