#
The Four Fours

*Update: November 2009* - I wrote this page back in 2005 when I had just set up my
website and was scratching around for something to put in it. Five years later
I have finally got around to analyzing my site's traffic and, to my surprise,
this page is by far the most popular on my site. But when I reviewed the code
I was horrified by how bad the style was (my C# was pretty green back then; it's
improved a bit since).

To celebrate its popularity I have given the code a facelift and I have added a
few new features. I recently discovered that this program inspired
a porting experiment a few years ago, and some of the participants complained
that the use of .4 and .4bar constitutes "cheating". I don't
necessarily disagree, so the new version allows you easily to disable the offending
terms, sacrificing solution completeness for intellectual integrity.

Anyway, if you're a fan of the four fours puzzle then I hope that you find what
you're looking for here.

This is a well known puzzle: express each integer from 1 to 100-ish using standard
mathematical operations and exactly four fours. For example, 16 = 4 + 4 +
4 + 4 (obviously in many cases there is more than one answer).

I have written a small C# program that will save you the trouble of finding the
solutions. The idea is simply to search every possible combination of operands
(4, 4!, .4, .4bar = .4444..., etc), binary operators (+, - , *, /, ^) and precedence
arrangements. It turns out that's all you need. I had the option of
increasing the speed by stopping as soon as I had found answers for all 100 numbers,
but since the thing runs so darn fast I keep it going to the bitter end in order
to find the 'nicest' answers (e.g. I prefer an answer that doesn't have to resort
to using the square root of .4bar).

The C# file:

Here is the program's output:

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 = ( (sqrt(4) + 4!) / sqrt(4)) + 4!

38 = (4 * 4) + (4! - sqrt(4))

39 = ( (4 + sqrt(4)) / .4) + 4!

40 = ( (4 * 4) + 4) * sqrt(4)

41 = ( (4! - 4) / .4bar) - 4

42 = (4 * 4) + (sqrt(4) + 4!)

43 = ( (4! - 4) / .4bar) - sqrt(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!) * sqrt(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 = ( (4 + 4!) + .4) / .4

72 = ( (4 * 4) + sqrt(4)) * 4

73 = ( (sqrt(4) * 4!) + sqrt(.4bar)) / sqrt(.4bar)

74 = (4 * 4!) - (4! - sqrt(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)

101 = (4 * 4!) + (sqrt(4) / .4)

102 = (4 + sqrt(4)) + (4 * 4!)

Done. (648000 loop iterations)

Num solved: 102

Elapsed time: 290 milliseconds