![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Next, suppose that you want to define factorial
, a method that answers
n!, where the receiver is to be an integer, n, and
n! =n * n-1 * ... * 1. As in the calculation
performed by powerOfTwo
, repeated multiplication is involved, but
this time the multipliers are not all the same.
You could define factorial
using timesRepeat
, albeit awkwardly:
Integer method definition instance factorial | result multiplier | result := 1. multiplier := self. self timesRepeat: [result := result * multiplier. multiplier := multiplier - 1]. ^ result Workspace Transcript show: 4 factorial printString Transcript 24