This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,13 @@
Number extend [
my_factorial [
(self < 2) ifTrue: [ ^1 ]
ifFalse: [ |c|
c := OrderedCollection new.
2 to: self do: [ :i | c add: i ].
^ (c fold: [ :a :b | a * b ] )
]
]
].
7 factorial printNl.
7 my_factorial printNl.

View file

@ -0,0 +1,7 @@
Number extend [
my_factorial [
self < 0 ifTrue: [ self error: 'my_factorial is defined for natural numbers' ].
self isZero ifTrue: [ ^1 ].
^self * ((self - 1) my_factorial)
]
].

View file

@ -0,0 +1,10 @@
|fac|
fac := [:n |
n < 0 ifTrue: [ self error: 'fac is defined for natural numbers' ].
n <= 1
ifTrue: [ 1 ]
ifFalse: [ n * (fac value:(n - 1)) ]
].
fac value:1000.
].

View file

@ -0,0 +1,4 @@
| fac |
fac := [ :n | (1 to: n) inject: 1 into: [ :prod :next | prod * next ] ].
fac value: 10.
"3628800"