#!/bin/sh
case $# in
  1) ;;
  *) echo "Usage: $0 <number>\n" 1>&2; exit 1 ;;
esac

exec induce --enter='
	precious "~/.fact2.gdbm" fact2(1);
	# Break recursion
	fact[0] = 1;
	# Write fact[n]=fact[u,n] where u is the greatest Fibonacci number
	# not exceeding n.
	fact[n] = fact[1,1,n] if (n >= 1);
	fact[u,v,n] = fact[u+v,u,n] if (n >= u+v);
	fact[u,v,n] = fact[u,n];
	# Now recurse down to fact[u,u]
	fact[u,u] = fact2[u];
	fact[u,v] = v.fact[u,v-1];
	# fact2[u] is exactly similar to fact[u], but it is memoized,
	# and its index is always a Fibonacci number.
	fact2[u] = u.fact[u-1];
' --quiet "fact[$1]"
