Pi Day Special – Pi-Approximating Code

Retrospective (9/15/2020): 1) I have NO clue what’s happening with the thumbnail. Past me, WHAT were you thinking? I might redesign it purely because it bothers me so much. 2) So, the “Make a post every week” thing didn’t entirely work out. Still, I’m going to make a promise that I can’t keep, and say I’ll start trying to post here again! First, though, I wanna clean up the place a bit. Not sure what that’ll entail, but we’ll see soon enough! 3) While I’m here, shoutout to this bit of MARVELOUS obfuscated code I found a few days ago: https://youtu.be/QMYfkOtYYlg. I’m pretty sure most people already saw it last year when YouTube’s algorithms made it blow up, but I still find it fascinating. Okay, that’s all. See you next time, and enjoy the article!

If you subscribe to the email list (which you can do with the form at the bottom!), you’ll have (hopefully) received an email about a plan to make a post every week for as long as I can. Not sure if I mentioned that in these posts, but if I did, you might have noticed, subscribed or not, that I missed last week. I’ll try to make another post to make up for that, but in the meantime… Pi Day special!

In one of my computer science courses, the professor briefly mentioned obfuscated code contests. It piqued my interest, so I looked them up, and saw some pretty cool stuff. I was particularly inspired by one entry on this Wikipedia page (the link navigates to it). It was a 1988 submission that calculated Pi by looking at its own size. The code is below:

#define _ -F<00||--F-OO--;
int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()

I thought it was pretty cool, despite not being able to read it (since it obviously does its job). However, I saw potential for an alternative version, one that itself would form the circle to be estimated; the code itself would be the circle. Mostly, I just didn’t like that line at the top – it felt extraneous.

I’d been working in Python in recent weeks (courtesy of the course). However, I figured that with the whitespace requirements of the language, there’d be next to no way to pull off the circle shape while preserving functionality. So, it was back to Java, my specialty!

I needed to get working code approximately equivalent in length to the area of some circle with a whole-numbered radius. Then, I needed to get that code into the shape of a circle. Surprisingly, the first step went rather well – I found a Minecraft circle generator for assistance, and discovered that approx. 380 characters, corresponding to a circle 22 units wide, worked perfectly. However, from there, the development was painful. I spent way too long clinging to a poor decision to not count any whitespace at all when calculating the length, rather than only ignoring the leading stuff. I figured that somehow, despite this introducing 50-odd characters taking up space in the circle (never mind the extra ones I added to make it look right), I’d have enough room for all the code at the end. Unsurprisingly, it didn’t work out that way, and I was eventually forced to do it the smart way.

Finalizing it took a lot of tweaking. It was a rather unusual debugging experience – it’s weird constantly having to keep in mind that inserting a character changes the length, and therefore the behavior, of the program. I ended up making another program to read the length externally when fine-tuning the circle, and temporarily hijacking the circle’s understanding of itself to acquire a perfect length-reading while fixing the calculations. But eventually, it was done!

Sadly, I had to abandon the Minecraft circle template. Their version (which I’ve chosen to trust as mathematically optimal, on the flimsy grounds that it was generated here, in Desmos) had an area of 384, while the area of a circle of diameter 22 rounds to 380. I was/am rather curious why the two don’t sync up, but the point is that my code’s circle needed to differ from the generator’s circle by 4 characters in order to properly approximate pi. The code is below, so a challenge: try to find the flaw in the circle!

    import java.io
   .*; public class
  Circ{public static
  void main(String[]
 arg){String prog="";
 double dia=0.0;try {
File file = new File (
"Circ.java" ); Scanner
sc=new Scanner (file);
while ( sc.hasNextLine
()){ prog=prog.concat(
 Exception e){}System
  )(prog.length()) /
     PI w/ code*/

I posted the code on that CS class’s discussion board. Someone else replied with some obfuscated code they made as well – a quine (that threw into doubt my understanding of Python’s whitespace requirements) below:

u=lambda a,b,c:a.                insert(b,(a.pop(                b)+1)*-~-(not c)
);d=0;a=[d];d=~d                 <<5;b=lambda i=0                :("+"and"".join(
map(lambda c:chr                 (c+65),a)),u(a,i                ,b),u(a,i,0)or(a
.append(0)if i==                 ~-len(a)else b(i                +1))if a[i]>d+d-
d else b)[0];d^=                 ~6;m=lambda n:[*
[(lambda o:(globals().update(o)))({b():n})],n][1]                ;j=map(m,[next,\
lambda j,*v:j(*v),map,list,m,chr,input,ord,type((                lambda W:W(W,))(
repr)),lambda s,t:"".join(map(lambda q:s[q],t)),(                print)]);next(j\
)(j)(list,j).pop()(J(m(open(__file__ if 1else 0).                read().replace(\
"\r","\n")),[532,178,46,H("?"),3,37,36,17,504,46,                0,733]));K(E(G()
));B(K,B(J,L,[73                 *10,250,168,121,                387,421,258])+M+
"!");K(J(L,[730,                 36,37,36,8,33,35                ,8,38,178,36,8,-
-35,46,0,37,13,1                 +35,8,13,46,6,6*                6,8,46,257,8,38,
178,36,8,45,37,9                 *5+1,390,37,3,4,                14]));G("Press"+
" enter to cont"                 +"inue...");B(B,                K,I(L));C(D,[L])

However, that response was anonymous, so it came to me as a great surprise when I discovered that individual was: 1) A friend of mine, 2) A computer science guy (I’d met him the previous semester in Creative Writing), 3) In the same class, albeit a different section, and 4) Incredibly, as it turned out, experienced with obfuscated code – he’d been doing it for years. His name was (is?) Conor O’Brien, whose GitHub you can find here. Following the conversation where I discovered this, we spent a lovely few hours together, which began with a discussion of our code. From him, I learned that my code wasn’t really “obfuscated,” since programs in that category are meant to be difficult to read. I’d figured this – but then, I hadn’t really intended to settle fully into that niche, anyway. I also learned in that conversation with Conor that my code’s most impressive trait is being shaped, and yet written in Java. The competitions all use C, and his code used Python, with the reasoning being that shaping code is difficult in languages requiring long keywords, like Java. That made my difficulty with shaping the circle more understandable (especially since I didn’t opt for one-word variable names), and that made the fact I’d pulled it off all the more satisfying.

Here’s the result of running the code in jGRASP (it’s what I learned first, don’t judge me):

----jGRASP exec: java Circ
 ----jGRASP: operation complete.

As you can see, it’s a decent approximation. It’ll go toe-to-toe with some of Matt Parker’s Pi Day approximations. You should check him out, by the way – his new Pi Day video (and a subsequent binge of his older ones) inspired me to make this post.

To wind this article up, here’s some photos of the code I made for fun, mostly included because I sank too much time into making them – thus the thumbnail’s inclusion (and existence, to be honest). One of them is also included because it’s a generated vector graphic, and 1) I like it, 2) I’m curious if I can upload it in its vector format, and 3) I’ve heard those are good for web design.

Huh. That didn’t come out looking as good as I expected. I was sure the “gallery” option would have one of those fancy sliders, but this just looks… ugh. Well, what can you do?

I don’t think there’s really any download available for this – all of it is right there in front of you! Well, to close things up (again), here’s some challenges for you:

  1. Figure out where the “flaw” in the circle is, as described above. You can do it by eye, if you look close enough!
  2. Try to figure out what the hell Conor’s code does, and how it works. To be honest, I still don’t really get it.
  3. While you’re at it, check out Conor’s GitHub! There’s some good stuff there.
  4. And as long as you’re graciously giving people some love, please subscribe to the email list! It’s got a whole 5 people on it at the moment, of whom at least 3 aren’t spambots. Do it in the form below!
  5. Try to figure out what my code does! I’ve neglected giving a full explanation for this purpose, after all. It shouldn’t be too hard – it’s supposed to be interesting to read, not difficult.
  6. Finally, leave a comment! It won’t help me at all – in fact, I’m pretty sure the hours I’ve spent culling spambot comments are all thanks to the one single real comment I do have – but it’s nice to know I’m not simply shouting into the void, and that you’re a real person.

Shoot, it isn’t Pi Day anymore, at least in my time zone. Well, anyway, thank you very much for reading!

Leave a Reply

Your email address will not be published. Required fields are marked *