# [Pharo-project] Integer>>#factorial

Hernan Wilkinson hernan.wilkinson at 10pines.com
Thu Jun 16 11:03:43 EDT 2011

```I agree with Alex, but I don't see why, alex, you say it is a good example
for teaching OO (the smalltalk factorial implementation...)
For me is a good example of a recursive "function"
I think it would be a good OO example it it was using "object recursion" or
the one that could be implemented in a prototyped language... why do you
think it is a good oo example the smalltalk factorial implementation?

On Thu, Jun 16, 2011 at 9:52 AM, Alexandre Bergel
<alexandre.bergel at me.com>wrote:

> Hi Sven,
>
> I am not sure it should replace the standard implementation.
> Integer>>factorial is essentially there for teaching purpose in my opinion.
> I am not sure how often factorial is used in practice. The most important
> for me is not it to be fast, but to be an excellent example of OOP.
>
> Your implementation could be next to the standard one though.
>
> Cheers,
> Alexandre
>
>
> On 16 Jun 2011, at 06:38, Sven Van Caekenberghe wrote:
>
> > Hi,
> >
> http://www.parcplace.net/list/vwnc-archive/1106/msg00080.html the goal
> being to optimize the current, naive Integer>>#factorial implementation.
> >
> > I had some old Lisp code hanging around, implementing an algorithm that I
> once found somewhere, and I ported it to Smalltalk. I am sure there are
> faster solutions, but this one is twice as fast as the original while still
> being easy to understand and it has a useful, safe helper method:
> >
> > Integer>>#svcFactorial
> >
> >       self = 0 ifTrue: [ ^ 1 ].
> >       self > 0 ifTrue: [ ^ 1 productUpTo: self ].
> >       self error: 'Not valid for negative integers'
> >
> > Integer>>#productUpTo: anInteger
> (non-inclusive) up to anInteger (inclusive)."
> >
> >       | difference split |
> >       self assert: (self between: 0 and: anInteger).
> >       "the idea is to multiply LargePositiveIntegers of approximately the
> same size"
> >       difference := anInteger - self.
> >       difference = 0 ifTrue: [ ^ 1 ].
> >       difference = 1 ifTrue: [ ^ anInteger ].
> >       difference = 2 ifTrue: [ ^ (anInteger - 1) * anInteger ].
> >       difference = 3 ifTrue: [ ^ (anInteger - 2) * (anInteger - 1) *
> anInteger ].
> >       difference = 4 ifTrue: [ ^ (anInteger - 3) * (anInteger - 2) *
> (anInteger - 1) * anInteger ].
> >       split := (self + anInteger) bitShift: -1.
> >       ^ (self productUpTo: split) * (split productUpTo: anInteger)
> >
> > Would it be a good idea to replace the current implementation with this
> one ?
> >
> > Sven
> >
> >
>
> --
> _,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
> Alexandre Bergel  http://www.bergel.eu
> ^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.
>
>
>
>
>
>
>

--
*Hernán Wilkinson
Agile Software Development, Teaching & Coaching
Mobile: +54 - 911 - 4470 - 7207
email: hernan.wilkinson at 10Pines.com
site: http://www.10Pines.com <http://www.10pines.com/>*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20110616/d0a27b38/attachment-0001.html>
```