Fibonacci numbers in Smalltalk
Example for versions
Squeak 3.10
This code is a method of an object, and adds n Fibonacci numbers to an OrderedCollection called numbers
.
Variable declarations are enclosed in vertical bars and don’t specify their type. Code blocks are enclosed in square brackets. The “^” carrot at the end returns the result from this method.
fibonacci: n
| numbers a b |
numbers := OrderedCollection new.
a := 1.
b := 1.
[ numbers size < n ] whileTrue: [
| temp |
numbers add: a.
temp := b.
b := a + b.
a := temp.
].
^ numbers.
Comments
]]>blog comments powered by Disqus
]]>