--- /dev/null
+#!/usr/bin/awk -f
+
+# f(n | n is even) = n/2
+# f(n | n is odd) = 3n + 1
+# Which integer under 1,000,000 requires the largest number of applications of
+# f(n) to reach 1?
+
+# This is basically a transliteration of the Pez version of this program. It
+# runs in 5.6s under gawk. It segfaults mawk on my machine. :/
+
+BEGIN {
+ cmap[1] = 1
+ rmap[1] = 1
+
+ final = max_steps(1000000)
+ print(final " (" cmap[final] " steps.)")
+}
+
+function collatz(n) {
+ if(n % 2) return n * 3 + 1
+ return n / 2
+}
+
+function csteps(n, steps) {
+ if(n in cmap) return cmap[n]
+ steps = csteps(collatz(n)) + 1
+ cmap[n] = steps
+ rmap[steps] = n
+ return steps
+}
+
+function find_max(lim, max, c) {
+ max = 0
+ for(i = 1; i < lim; i++) {
+ c = csteps(i)
+ if(c > max) max = c
+ }
+ return max
+}
+
+function max_steps(lim) {
+ return rmap[find_max(lim)]
+}
--- /dev/null
+#!/usr/bin/awk -f
+
+# Given lines with a base/exponent pair on each line, determine which line
+# number has the greatest numerical value.
+
+# Basic property of logarithms, courtesy of Wikipedia:
+# a^b = e^(b * log(a))
+# So, b * log(a) can be used to compare, and is quick and easy besides.
+
+# If you guessed that I am, at this point, just going through the problems that
+# have CSV files and using awk to process them, then you have guessed correctly
+# because that is how I decided to do this one. After checking the forum page
+# for this, though, I found that I'm not the only one to have done an awk
+# solution, and saw this clever one-liner by a guy called 'mpersano':
+# t = log($1)*$2 > m { m = log($1)*$2; l = NR } END { print l }
+# Sure beats mine.
+
+# 0m0.004s
+
+BEGIN {
+ lmax = 0
+ max = 0
+}
+
+{
+ r = $2 * log($1)
+ if(r > max) {
+ lmax = NR
+ max = r
+ }
+}
+
+END {
+ print lmax, max
+}
+