#### Archived

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

# what does this do?

This topic is 6088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 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 on other sites
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 on other sites
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 on other sites
  #include //// 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 on other sites
when I tried it, it didn''t work. I got a whole lot of 0s and a 2

##### Share on other sites
quote:
Original post by masonium
when I tried it, it didn''t work. I got a whole lot of 0s and a 2

Works for me. I compiled it in Borland C++ 5.0 (I use it for quick compiles since it doesn''t require that you make a project).

[Resist Windows XP''s Invasive Production Activation Technology!]

##### Share on other sites
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 on other sites
b-c is the second parameter to the for loop, so when it evaluates to 0 the for loop terminates, i.e. when b=c in this case

c may not change, but b does

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

  #include 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

• 33
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631352
• Total Posts
2999486
×