Factorial in J

Example for versions j602

This example implements three methods of calculating factorial. fact represents a factorial function which generates the first n integers as an array and multiplies them together. factr represents the recursive definition. ! is the built-in factorial function.

load 'printf'

fact=: [: */ [: >: i.
factr=: 1:`(]*$:@<:)@.*

'!%d = %d' printf (,"0 fact) i.17x
'!%d = %d' printf (,"0 factr) i.17x
'!%d = %d' printf (,"0 !) i.17x