Factorial in Smalltalk

Example for versions Squeak 3.10

This example is actually cut and paste from the Squeak core source code in the Integer class. From any Squeak image:

10000 factorial. “and press alt-p to print the result below this line”

284625968091705451890641321211986889014805140170279923079417999427441134000376444377299078675778477581588406214231752883004233994015351873905242116138271617481982419982759241828925978789812425312059465996259867065601615720360323979263287367170557419759620994797203461536981…etc… edited for brevity.

This takes about 200 milliseconds on a modern PC to compute the factorial, and about 6 seconds to convert the resulting number into a String for displaying.

The resulting number has this 35660 digits:

10000 factorial asString size


	"Answer the factorial of the receiver."

	self = 0 ifTrue: [^ 1].
	self > 0 ifTrue: [^ self * (self - 1) factorial].
	self error: 'Not valid for negative integers'