### Four Fours in Python

 I couldn't leave it alone. I just had to see what the C# code looked like in Python, so I could understand how it worked, and to see how much slower it was in a scripting language.

I've made no attempts to optimize the code. If I had more time to spend on this, I think I would have designed the data structures differently for the Python version.

To my unpracticed eye, it looks more readable as Python code, but I don't want to start a my-language-is-better-than-yours slanging match.

Here's the results. Hopefully identicals to Carl's.

% python fourfours.py
1 = (4 + 4) / (4 + 4)
2 = (4 \* 4) / (4 + 4)
3 = ( (4 + 4) + 4) / 4
4 = ( (4 - 4) \* 4) + 4
5 = ( (4 \* 4) + 4) / 4
6 = ( (4 + 4) / 4) + 4
7 = (4 + 4) - (4 / 4)
8 = (4 + 4) + (4 - 4)
9 = (4 + 4) + (4 / 4)
10 = (4 + 4) + (4 - sqrt(4))
11 = ( (4 + 4!) / 4) + 4
12 = 4 - (4 / 4) \* 4
13 = (4 / 4) + (4! / sqrt(4))
14 = (4 + 4) + (4 + sqrt(4))
15 = (4 \* 4) - (4 / 4)
16 = (4 + 4) + (4 + 4)
17 = (4 \* 4) + (4 / 4)
18 = (4 \* 4) + (4 - sqrt(4))
19 = (4! - 4) - (4 / 4)
20 = ( (4 / 4) + 4) \* 4
21 = (4 / 4) - (4 - 4!)
22 = (4 \* 4) + (4 + sqrt(4))
23 = ( (4 \* 4!) - 4) / 4
24 = (4 + 4) + (4 \* 4)
25 = ( (4 / 4) + 4) \^ sqrt(4)
26 = ( (4 + 4) / 4) + 4!
27 = (4 + 4!) - (4 / 4)
28 = ( (4 + 4) \* 4) - 4
29 = (4 / 4) + (4 + 4!)
30 = ( (4 + 4) \* 4) - sqrt(4)
31 = ( (4 + 4!) / 4) + 4!
32 = (4 \* 4) + (4 \* 4)
33 = (4 + 4!) + (sqrt(4) / .4)
34 = ( (4 + 4) \* 4) + sqrt(4)
35 = ( (4 \* 4) - sqrt(4)) / .4
36 = ( (4 + 4) \* 4) + 4
37 = (4 + 4!) + (4 / .4bar)
38 = (4 \* 4) - (sqrt(4) - 4!)
39 = ( (4 \* 4) - .4) / .4
40 = ( (4 \* 4) + 4) \* sqrt(4)
41 = ( (4! - 4) / .4bar) - 4
42 = (4 \* 4) + (sqrt(4) + 4!)
43 = (sqrt(4) \* 4!) - (sqrt(4) / .4)
44 = (4 \* 4) + (4 + 4!)
45 = ( (4 \* 4) + 4) / .4bar
46 = ( (4 \* 4!) - 4) / sqrt(4)
47 = (sqrt(4) \* 4!) - (4 / 4)
48 = ( (4 + 4) + 4) \* 4
49 = (4 / 4) + (sqrt(4) \* 4!)
50 = ( (4 \* 4) + 4) / .4
51 = ( (4! - sqrt(4)) / .4) - 4
52 = ( (4 - sqrt(4)) \* 4!) + 4
53 = (4! / .4bar) - (4 / 4)
54 = ( (4 + sqrt(4)) \^ 4) / 4!
55 = (4 / 4) + (4! / .4bar)
56 = ( (4 \* 4) - sqrt(4)) \* 4
57 = (4 / .4bar) + (sqrt(4) \* 4!)
58 = ( (4 \^ 4) - 4!) / 4
59 = (4! / .4) - (4 / 4)
60 = ( (4 \* 4) \* 4) - 4
61 = (4 / 4) + (4! / .4)
62 = ( (4 \* 4) \* 4) - sqrt(4)
63 = ( (4 \^ 4) - 4) / 4
64 = (4 + 4) \* (4 + 4)
65 = ( (4 \^ 4) + 4) / 4
66 = ( (4 \* 4) \* 4) + sqrt(4)
67 = ( (4 + 4!) / .4bar) + 4
68 = ( (4 \* 4) \* 4) + 4
69 = ( (sqrt(4) + 4!) / .4) + 4
70 = ( (4 \^ 4) + 4!) / 4
71 = ( (sqrt(4) \* 4!) - sqrt(.4bar)) / sqrt(.4bar)
72 = ( (4 \* 4) + sqrt(4)) \* 4
73 = ( (sqrt(4) \* 4!) + sqrt(.4bar)) / sqrt(.4bar)
74 = (4 \* 4!) + (sqrt(4) - 4!)
75 = ( (4 + sqrt(4)) + 4!) / .4
76 = ( (4! - 4) \* 4) - 4
77 = ( (4 / .4bar) \^ sqrt(4)) - 4
78 = ( (4! - 4) \* 4) - sqrt(4)
79 = ( (4 / .4bar) \^ sqrt(4)) - sqrt(4)
80 = ( (4 \* 4) + 4) \* 4
81 = ( (4 / 4) - 4) \^ 4
82 = ( (4! - 4) \* 4) + sqrt(4)
83 = ( (4 / .4bar) \^ sqrt(4)) + sqrt(4)
84 = ( (4! - 4) \* 4) + 4
85 = ( (4 / .4bar) \^ sqrt(4)) + 4
86 = ( (4! - sqrt(4)) \* 4) - sqrt(4)
87 = (4 \* 4!) - (4 / .4bar)
88 = (4 \* 4!) - (4 + 4)
89 = ( (sqrt(4) + 4!) / .4) + 4!
90 = (4 \* 4!) - (4 + sqrt(4))
91 = (4 \* 4!) - (sqrt(4) / .4)
92 = 4! - (4 / 4) \* 4
93 = (4 \* 4!) - (sqrt(4) / sqrt(.4bar))
94 = (4 \* 4!) - (4 - sqrt(4))
95 = (4 \* 4!) - (4 / 4)
96 = (4 \* 4) \* (4 + sqrt(4))
97 = (4 / 4) + (4 \* 4!)
98 = (4 - sqrt(4)) + (4 \* 4!)
99 = ( (sqrt(4) \* 4!) - 4) / .4bar
100 = ( (4 + 4) + sqrt(4)) \^ sqrt(4)
Done. (648000 loop iterations)
Elapsed time:  8.08476805687  seconds
%

I ran this on an Acer Ferrari 3400 laptop (which has a Mobile AMD Athlon 64 processor), under Ubuntu Edgy (with Python 2.4.4c1). I don't know what Carl ran his code on.

Assuming they are roughly comparable, the Python version was over 20 times slower. Not bad. Unlike Homer Simpson and his microwaved food, I think I can wait out the 8 seconds with patience.

If somebody has a setup to run both and compare apples with apples that would be great.

I think the itch is scratched now. On to other things.

Update:

I received an email notification of a comment from "Name", but that comment didn't automatically appear with this blog entry. It was marked as spam. I've cleared that. Name said:

"Some of the values are wrong - I noticed 92, but there are probably others. I've created a new version of this, also written in Python. It's very, very slow (30 seconds to run), but is correct. (lets hope posting it works)"

Thanks! I've put the new version here.

Update:

I've also received a Java version of Carl's original C# code from Jeffrey Olson (thanks!), which you can find here

Comments are closed for this entry.