# Quick N! (Factorial) calculation using Double!

Explain a metodology for a quick computation of
N! ( factorial) suggestion and optimization are wellcomed!

Original Author: michele berardi

### Code

`Dim N As Double, b As Double, c As Double, p As Double'' Fast Way To Calculate N! ( N Factorial)'' A is N' using visual basic my original algorithm' is adapted to the vb limits..' you can use long or int instead of double' for small calculation..' some tips require the use of asr (aritmetic' shift right)' instead of division by 2!' and code optimization instead - 1 you can....' a good exercize of optimization... enjoy!' (also assembly form of this code boost the' performances!)'' N.B.' using double I extend the range of N!' that i can represent!' PASS TO VARIABLE N ' THE VALUE FOR WITCH' YOU MUST CALCULATE' FACTORIAL ( N! )' c = N - 1p = 1While c > 0p = 0b = cWhile b > 0If b And 1 Thenp = p + NEnd Ifb = int (b / 2) ' YOU MUST USE THE INTEGER PART NOT THE REST! asr more efficient fo division!N = N + NWendN = pc = c - 1WendMsgBox p ' the result of N!`

Posted: 2003-06-01
By: ArchiveBot
Viewed: 108 times

Categories

Visual Basic 6

Attachments

No attachments for this post