954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

More Efficient Algorithm Possible?

Hi, a program requires one to enter a given number of strings, and for each string, the program has to output the number of permutations possible. We all know that if a string S of length l has a type of one letter, b type of another, c type of another, and so on, while the rest are present only once, then the answer will be (l!)/(a! * b! * c!). Line no. 8 calculates the denominator. Now the question is, can this be made more efficient in any possible way???

use bignum;
sub fact { $_[0] == 0 ? 1 : $_[0]*fact($_[0] - 1); }
chomp($count = <STDIN>);
for (1..$count) {
%hash = ();
$div = 1;
chomp($_ = <STDIN>);
$div *= 1+$hash{$_}++ for split //;
$init = fact(length($_))/$div;
print "$init\n"
}
Cupidvogel
Newbie Poster
22 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You