## Facorial Calculator

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

### Facorial Calculator

As stated in my previous post, I have been bored lately, writing occasionally useful code. This time I have written a very simple collection of lines that calculates the factorial of a number. This code is useful for calculating the total number of possibilities that a word list generator can produce.

Code: Select all
a = long(raw_input("Number: "))
b = a
factorial = a

while a > 1:
a -= 1
factorial = factorial * a

print "The factorial of",b,"is:",factorial

Factorial:
Mathematics. the product of a given positive integer multiplied by all lesser positive integers: The quantity four factorial (4!) = 4 · 3 · 2 · 1 = 24.
Symbol: n!, where n is the given integer.

-Reference.com
Last edited by Arrexel on Mon Feb 16, 2009 7:07 pm, edited 2 times in total.
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM

Arrexel
New User

Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Blog: View Blog (0)

### Re: Facorial Calculator

I was bored and read this post.. decided to do the same thing in JS.

Code: Select all
<script>
factorial=1;
num=prompt("Type number:", "");
for(a=num;a>1;a--){
factorial = factorial * a;
}
document.write("<h1><br><center>" + num + "!=" + factorial);
</script>

mutants_r_us_guild
Poster

Posts: 246
Joined: Fri May 30, 2008 7:56 pm
Blog: View Blog (0)

### Re: Facorial Calculator

nice coding
by the way, factorials are usually high numbers that won't fit on normal int variables.
This library helped me to deal with huge numbers in C/C++:
http://www.nongnu.org/hpalib/
it's well documented
"Imagination is more important than knowledge..." Albert Einstein

GuGaBoY
New User

Posts: 15
Joined: Fri Apr 25, 2008 5:11 pm
Location: localhost
Blog: View Blog (0)

### Re: Facorial Calculator

Ah, that makes sense. I'll update the code to fix this problem when I get a chance later tonight. And nice work, mutants

EDIT:
Well, that was simple enough.
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM

Arrexel
New User

Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Blog: View Blog (0)

### Re: Facorial Calculator

C++ and GMP

Code: Select all
mpz_class factorial(mpz_class num)
{
mpz_class tot(num--);
while(num != 0)
{
tot *= num--;
}
}
The whole secret of existence is to have no fear. Never fear what will become of you, depend on no one. Only the moment you reject all help are you freed.
--Buddha

mischief
Poster

Posts: 356
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)

### Re: Facorial Calculator

Here's my script turned into a function Enjoy:

Code: Select all
function factorial(num)
{
fact=1;
for(a=num;a>1;a--){
fact = fact * a;
}
return fact
}

mutants_r_us_guild
Poster

Posts: 246
Joined: Fri May 30, 2008 7:56 pm
Blog: View Blog (0)

### Re: Facorial Calculator

Lol, a lot of different scripts being posted
Nice work.
"I think there is a world market for maybe five computers."
Unverified Quote, 1945 - Thomas J. Watson, Founder of IBM

Arrexel
New User

Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Blog: View Blog (0)

### Re: Facorial Calculator

Very simple perl version.

Code: Select all
#!/usr/bin/perl

print "Enter an integer number: \n";

\$Number = <STDIN>;

for (\$n = (\$Number-1) ; \$n >1; \$n -=1) {
\$Number *= \$n; }

print "The factorial of that number is: \$Number\n";
novalyphe
Experienced User

Posts: 51
Joined: Mon Apr 14, 2008 11:15 am
Blog: View Blog (0)

### Re: Facorial Calculator

Code: Select all
<?php
\$factorial="1";
\$n=1000;
for(\$i=1; \$i<=\$n; ++\$i) \$factorial=bcmul(\$factorial, \$i);

echo \$factorial;
?>

It can handle very very very big numbers.
acskurucz
New User

Posts: 29
Joined: Wed Jan 14, 2009 8:31 am
Blog: View Blog (0)

### Re: Facorial Calculator

acskurucz wrote:
Code: Select all
<?php
\$factorial="1";
\$n=1000;
for(\$i=1; \$i<=\$n; ++\$i) \$factorial=bcmul(\$factorial, \$i);

echo \$factorial;
?>

It can handle very very very big numbers.

I turned your code into a function:

function factorial(\$x){
\$factorial="1";
for(\$i=1; \$i<=\$x; ++\$i) \$factorial=bcmul(\$factorial, \$i);
return \$factorial;
}

P.S. I incorporated that function into my bot

mutants_r_us_guild
Poster

Posts: 246
Joined: Fri May 30, 2008 7:56 pm
Blog: View Blog (0)

Next