[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# How does this work? Q1

Posted on 2007-10-02
Medium Priority
363 Views
First of two similar posts:
__________________
How does this work? - Pi
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
#include <stdio.h>

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

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);
}

As an addendum, I'm not sure how we could check if the result actually IS pi, unless it's somehow implicit in the method.
0
Question by:Synthetics
• 3
• 3
• 2
• +1

LVL 46

Expert Comment

ID: 20001258
Hi Synthetics,

From a C standpoint, I'm not sure that it does.

Look at the second 'for' statement.  The loop condition is compound.

d=0,g=c*2

I'm not aware of that being legal.

Kent
0

LVL 5

Author Comment

ID: 20001618
works for me - outputting:

31415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185

(on linux from: CC filename.c
which I think is the standard C compiler... cc for C++, gcc for GNU C and g++ for GNU C++)
0

LVL 53

Assisted Solution

Infinity08 earned 180 total points
ID: 20001715
At least the 800 digits are indeed the first 800 digits of PI. So, the code works ...

It is however heavily obfuscated (did you get this from an obfuscated code contest ??).

Here's an attempt at making the code easier to understand :

#include <stdio.h>

int main(void) {
int i = 0;
int next = 0;
int base = 2800;
int buf[2801] = { 0 };

for (i = 0; i < base; ++i) {
buf[i] = 2000;
}

while (i = base * 2) {
int cur = 0;
int j = base;
do {
cur += buf[j] * 10000;
buf[j] = cur % --i;
cur /= i--;
if (--j) cur *= j;
} while (j);
printf("%.4d", next + cur / 10000);
next = cur % 10000;
base -= 14;
}

return 0;
}

The exact algorithm is not simple, and I haven't deciphered it completely, but it looks quite interesting ...

>> Look at the second 'for' statement.  The loop condition is compound.

afaik, that's not against the C standard (didn't check) ... I don't see why it wouldn't be allowed.
0

LVL 5

Author Comment

ID: 20001770
Yes I think it was from an obfuscated code contest. My university lecturer presented it to us (don't worry, I'm not required to work it out - it just intrigued me, as did the second one I'll post when I get my points back from my mis-posting) and he mentioned something like that.
0

LVL 22

Accepted Solution

grg99 earned 320 total points
ID: 20001887
Here's an untangled version of it:
#include <stdio.h>

#define Loops 2800

int ScaleFactor,Inx,LoopLimit,d,e, Table[ Loops + 1 ],Evens;

int main(int argc, char* argv[])
{
ScaleFactor = 10000; LoopLimit= Loops;

for( Inx = 0 ; Inx < LoopLimit; Inx++  ) Table[ Inx ] = ScaleFactor / 5;

d = 0;

for( ; LoopLimit > 0; LoopLimit -= 14 ) {
Evens = LoopLimit * 2;

for( Inx = LoopLimit;       Inx > 1;  --Inx      ) {
d += Table[Inx] * ScaleFactor;
Evens--;
Table[ Inx ] = d % Evens;
d = d / Evens * Inx;
Evens--;
}

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

return 0;
}

It seems to be based on the curious algorithms that can calculate the Xth digit of Pi, called BBP type formulas:

http://mathworld.wolfram.com/BBP-TypeFormula.html

0

LVL 46

Expert Comment

ID: 20002668

Ok, then I'll bite.

What does the expression    d=0,g=c*2     do as the loop's condition test?

Kent
0

LVL 53

Expert Comment

ID: 20002818
>> What does the expression    d=0,g=c*2     do as the loop's condition test?

It initializes d to 0, then it sets g to c*2, and finally, it starts the next iteration as long as g != 0.

Remember that the value of an expression with a comma operator is the value of the last argument. Or in other words :

expression1, expression2

will evaluate expression1 and then expression2, and will have the value of expression2.
0

LVL 46

Expert Comment

ID: 20005517
Hi Infinity08,

>       expression1, expression2
>
> will evaluate expression1 and then expression2, and will have the value of expression2.

That, I truly didn't know....

I learned something today.   :)

Kent
0

LVL 5

Author Comment

ID: 20028137
I mentioned this was going to be the first of two, but I sort of found how the second one worked; it's apparently a famous example of obfuscated code. It printed the 12 verses of the 12 days of christmas and I gather it was from some kind of embedded encryption. Very clever I thought, but even if someone could explain how the decryption worked, I probably wouldn't understand, so I shan't bother asking.

I will however have a look at some more obfuscations to see if there's anything I'd like to pose as a question!

Thanks :-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
###### Suggested Courses
Course of the Month19 days, 1 hour left to enroll