Facorial Calculator

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

Facorial Calculator

Post by Arrexel on Mon Feb 16, 2009 11:03 am
([msg=18020]see Facorial Calculator[/msg])

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
User avatar
Arrexel
New User
New User
 
Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Location: Ontario, Canada
Blog: View Blog (0)


Re: Facorial Calculator

Post by mutants_r_us_guild on Mon Feb 16, 2009 5:08 pm
([msg=18037]see Re: Facorial Calculator[/msg])

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>
Image
Image
Image
User avatar
mutants_r_us_guild
Poster
Poster
 
Posts: 246
Joined: Fri May 30, 2008 7:56 pm
Blog: View Blog (0)


Re: Facorial Calculator

Post by GuGaBoY on Mon Feb 16, 2009 5:47 pm
([msg=18042]see Re: Facorial Calculator[/msg])

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
User avatar
GuGaBoY
New User
New User
 
Posts: 15
Joined: Fri Apr 25, 2008 5:11 pm
Location: localhost
Blog: View Blog (0)


Re: Facorial Calculator

Post by Arrexel on Mon Feb 16, 2009 7:01 pm
([msg=18052]see Re: Facorial Calculator[/msg])

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

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
User avatar
Arrexel
New User
New User
 
Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Location: Ontario, Canada
Blog: View Blog (0)


Re: Facorial Calculator

Post by mischief on Fri Feb 20, 2009 6:33 pm
([msg=18320]see Re: Facorial Calculator[/msg])

C++ and GMP

Code: Select all
mpz_class factorial(mpz_class num)
{
   mpz_class tot(num--);
   while(num != 0)
   {
      tot *= num--;
   }
   return tot;
}
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
User avatar
mischief
Poster
Poster
 
Posts: 355
Joined: Wed Jan 07, 2009 4:16 pm
Blog: View Blog (0)


Re: Facorial Calculator

Post by mutants_r_us_guild on Fri Feb 20, 2009 6:46 pm
([msg=18321]see Re: Facorial Calculator[/msg])

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
}
Image
Image
Image
User avatar
mutants_r_us_guild
Poster
Poster
 
Posts: 246
Joined: Fri May 30, 2008 7:56 pm
Blog: View Blog (0)


Re: Facorial Calculator

Post by Arrexel on Fri Feb 20, 2009 7:18 pm
([msg=18323]see Re: Facorial Calculator[/msg])

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
User avatar
Arrexel
New User
New User
 
Posts: 31
Joined: Fri Feb 13, 2009 7:44 pm
Location: Ontario, Canada
Blog: View Blog (0)


Re: Facorial Calculator

Post by novalyphe on Tue Mar 24, 2009 12:45 pm
([msg=20452]see Re: Facorial Calculator[/msg])

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
Experienced User
 
Posts: 51
Joined: Mon Apr 14, 2008 11:15 am
Blog: View Blog (0)


Re: Facorial Calculator

Post by acskurucz on Thu Mar 26, 2009 7:13 pm
([msg=20640]see Re: Facorial Calculator[/msg])

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
New User
 
Posts: 29
Joined: Wed Jan 14, 2009 8:31 am
Blog: View Blog (0)


Re: Facorial Calculator

Post by mutants_r_us_guild on Fri Mar 27, 2009 4:42 pm
([msg=20716]see Re: Facorial Calculator[/msg])

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 :P
Image
Image
Image
User avatar
mutants_r_us_guild
Poster
Poster
 
Posts: 246
Joined: Fri May 30, 2008 7:56 pm
Blog: View Blog (0)


Next

Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests