smalltalk: fibonacci classic program + smalltalk-load + 13 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
23
lib/smalltalk/tests/programs/fibonacci.st
Normal file
23
lib/smalltalk/tests/programs/fibonacci.st
Normal file
@@ -0,0 +1,23 @@
|
||||
"Fibonacci — recursive and array-memoised. Classic-corpus program for
|
||||
the Smalltalk-on-SX runtime."
|
||||
|
||||
Object subclass: #Fibonacci
|
||||
instanceVariableNames: 'memo'!
|
||||
|
||||
!Fibonacci methodsFor: 'init'!
|
||||
init memo := Array new: 100. ^ self! !
|
||||
|
||||
!Fibonacci methodsFor: 'compute'!
|
||||
fib: n
|
||||
n < 2 ifTrue: [^ n].
|
||||
^ (self fib: n - 1) + (self fib: n - 2)!
|
||||
|
||||
memoFib: n
|
||||
| cached |
|
||||
cached := memo at: n + 1.
|
||||
cached notNil ifTrue: [^ cached].
|
||||
cached := n < 2
|
||||
ifTrue: [n]
|
||||
ifFalse: [(self memoFib: n - 1) + (self memoFib: n - 2)].
|
||||
memo at: n + 1 put: cached.
|
||||
^ cached! !
|
||||
Reference in New Issue
Block a user