Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to do Factorials in VFP with Recursion
Message
 
 
À
21/02/2000 22:53:23
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00335196
Message ID:
00335803
Vues:
21
DD,

Just to add more fuel to the fire. I find the Factorial function written like this to be much clearer:
function Factorial( pnFact )

if ( pnFact > 1 )
   return pnFact * Factorial( pnFact - 1 )
else
   return 1
endif

endfunc
And unless you are calculating thousands of them at a time, in which case even more processor time can be saved by precalculating a lookup array/table, the recursive form should have adequate performance. Actually I just tested this and the above function calculates 100,000 random factorials in 1.591 seconds and Vlad's non-recursive routine takes 2.407 seconds! The recursive method involves far fewer memory stores which probably accounts for this.

So the bottom line don't accept any "common wisdom" without your own proof tests. *g*
>LPARAMETERS nFactor
>
>local nRetVal
>nRetVal = 0
>
>DO CASE
>	CASE nFactor = 0
>		nRetVal = 1
>		return nRetVal
>
>	CASE nFactor > 0
>		nRetVal = nFactor * factorial(nFactor - 1)
>		RETURN nRetVal
>ENDCASE
>RETURN
df (was a 10 time MVP)

df FoxPro website
FoxPro Wiki site online, editable knowledgebase
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform