C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Fonction Factorielle

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French)
View previous topic :: View next topic  
Author Message
dperetti
Guest





PostPosted: Thu Jan 22, 2004 2:08 pm    Post subject: Fonction Factorielle Reply with quote



existe il une fonction factorielle en c++ toute faite
style.... long fact (long )
merci


Back to top
Rémy
Guest





PostPosted: Thu Jan 22, 2004 2:29 pm    Post subject: Re: Fonction Factorielle Reply with quote




"dperetti" <dperetti (AT) wanadoo (DOT) fr> a écrit dans le message de
news:buolf5$98g$1 (AT) news-reader3 (DOT) wanadoo.fr...
Quote:
existe il une fonction factorielle en c++ toute faite
style.... long fact (long )
merci



long fact(long n)
{
long result=1;
for (,n>0,n--)
{
result = result * n;
}
return result;
}

Évidemment, dès que n est un peu grand, ça va déborder...



Back to top
Benoit Dejean
Guest





PostPosted: Fri Jan 23, 2004 12:01 am    Post subject: Re: Fonction Factorielle Reply with quote



Le Thu, 22 Jan 2004 15:29:16 +0100, Rémy a écrit :

Quote:

"dperetti" <dperetti (AT) wanadoo (DOT) fr> a écrit dans le message de
news:buolf5$98g$1 (AT) news-reader3 (DOT) wanadoo.fr...
existe il une fonction factorielle en c++ toute faite
style.... long fact (long )
merci



long fact(long n)
{
long result=1;
for (,n>0,n--)

n>1 suffit

avec des ; c'est mieux

Quote:
{
result = result * n;

soyons avares

result *=n;

Quote:
}
return result;
}



Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Fri Jan 23, 2004 8:57 am    Post subject: Re: Fonction Factorielle Reply with quote

"dperetti" <dperetti (AT) wanadoo (DOT) fr> wrote


Quote:
existe il une fonction factorielle en c++ toute faite style.... long
fact (long )

En C++ standard, non, mais Posix a une fonction lgamma qui peut servir :

double
fact( int n )
{
return exp( lgamma( n + 1.0 ) ) ;
}

Sinon, il y a toujours la classique :

double
fact( int n )
{
return n > 1 ? n * fact( n - 1 ) : 1.0 ;
}

Avec un type de retour de long, on est limité a n <= 20 ; l'utilisation
d'un double nous permet jusqu'à n <= 170. Sur ma machine, au moins. Et a
priori (mais je ne suis pas spécialisé dans les calculs numériques),
j'imagine que la première version est plus précise. (Les résultats
diffèrent dès le douzième chiffre chez moi.)

--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16

Back to top
dperetti
Guest





PostPosted: Mon Jan 26, 2004 10:17 pm    Post subject: Re: Fonction Factorielle Reply with quote


"dperetti" <dperetti (AT) wanadoo (DOT) fr> a écrit dans le message news:
buolf5$98g$1 (AT) news-reader3 (DOT) wanadoo.fr...
Quote:
existe il une fonction factorielle en c++ toute faite
style.... long fact (long )
merci

merci pour vos reponse a tous (je cherchais pour n <70)



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (French) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.