Author Topic: Factorials  (Read 13267 times)

0 Members and 1 Guest are viewing this topic.

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Factorials
« Reply #15 on: January 30, 2011, 09:02:49 am »
Maybe I should have rephrased a bit differently: The gamma function for positive integers follows the following property: gamma(x)=(x-1)!. Hence gamma(1)=0!=1, for instance.

However, what is gamma(0)? It would equal to (-1)!. But using the factorial recurrence formula would give us 0(-1)!=0!=1. In other words, (-1)! = 1/0. This is bad; hence, (-1)! is not defined, and so is gamma(0).

Most other numbers still have a gamma function attached to it, for example gamma(-1/2), gamma(3+4i), and gamma(1337.42i), but zero and negative numbers when plugged into gamma generate an undefined result, because their respective factorials are undefined as well.

EDIT: Aw darn, sniped
We prefer to say you got ninja'd, but yes. yes you did. ;-)

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Factorials
« Reply #16 on: January 30, 2011, 11:13:52 am »
Still, it's an interesting concept I hadn't thought  about!

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Factorials
« Reply #17 on: January 30, 2011, 11:15:13 am »
Code: [Select]
def getFactorial(x):
    if x == 0:
        return 1
    else:
        soma = 1
        for i in range(x,0,-1):
            soma=soma*i
        return soma

I just made a Python function that gets the factorial of a n number.

A simpler way of doing this is:

Code: [Select]
import math
print math.factorial(x)

But mine is much cooler.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Factorials
« Reply #18 on: January 30, 2011, 11:19:20 am »
Ah, but does it work like the gamma function?
(3.1)! is not the same as 3.1*2.1*1.1

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Factorials
« Reply #19 on: January 30, 2011, 03:55:23 pm »
two ways in java, out of boredom:

Code: [Select]
public int factorial(int x){
   if(x == 0)
      return 1;
   return x * factorial(x - 1);
}
Code: [Select]
public int factorial(int x){
   int sum = x == 0 ? 1 : x;
   for(int i = x; --i > 0;)
      sum *= i;
   return sum;
}


Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Factorials
« Reply #20 on: January 30, 2011, 04:15:48 pm »
Yet another cool way to compute factorials, this way is for factorials of very very large numbers without multi-precision containers.
No multiplication required ^-^

Code: (Pseudocode) [Select]
double sum=0;
for (int i=1; i<Max; i++) {
  sum += log(i);
}
int exponent = floor(sum);
double coeff = Pow(10,sum-exponent);

print("Answer is: ",coeff,"x10^",exponent);
« Last Edit: January 30, 2011, 04:16:11 pm by Quigibo »
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Factorials
« Reply #21 on: January 30, 2011, 04:17:25 pm »
Cool, is there a TI-BASIC representation of that?! That looks really cool!

Offline Quigibo

  • The Executioner
  • CoT Emeritus
  • LV11 Super Veteran (Next: 3000)
  • *
  • Posts: 2031
  • Rating: +1075/-24
  • I wish real life had a "Save" and "Load" button...
    • View Profile
Re: Factorials
« Reply #22 on: January 30, 2011, 04:20:56 pm »
Yeah, I actually use it in my program "MISCPRGM" if you want to see it.  It can do up to 999! I think as long as you have enough memory.
___Axe_Parser___
Today the calculator, tomorrow the world!

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Factorials
« Reply #23 on: January 30, 2011, 04:22:17 pm »
COOL! Awesome! Wow! (and a half)

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: Factorials
« Reply #24 on: January 30, 2011, 05:38:38 pm »
Code: [Select]
def getFactorial(x):
    if x == 0:
        return 1
    else:
        soma = 1
        for i in range(x,0,-1):
            soma=soma*i
        return soma

I just made a Python function that gets the factorial of a n number.

A simpler way of doing this is:

Code: [Select]
import math
print math.factorial(x)

But mine is much cooler.
I note some variables names in maybe portuguese... Depending on the project I also code with names and comments in portuguese. ^^ Although I try to not mix 2 languages.

Yet another cool way to compute factorials, this way is for factorials of very very large numbers without multi-precision containers.
No multiplication required ^-^

Code: (Pseudocode) [Select]
double sum=0;
for (int i=1; i<Max; i++) {
  sum += log(i);
}
int exponent = floor(sum);
double coeff = Pow(10,sum-exponent);

print("Answer is: ",coeff,"x10^",exponent);
Nice code, for some time I didn't see a trick like that. Where/How did you learn it?
Hobbing in calculator projects.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Factorials
« Reply #25 on: January 30, 2011, 05:42:12 pm »
Code: [Select]
def getFactorial(x):
    if x == 0:
        return 1
    else:
        sum = 1
        for i in range(x,0,-1):
            sum =sum *i
        return sum
English Version

Code: [Select]
def calcularFactorial(x):
    if x == 0:
        return 1
    else:
        soma = 1
        for i in range(x,0,-1):
            soma = soma *i
        return soma
Portuguese Version


/OFFTOPIC: Galandros, do you know portuguese? Where are you from?

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: Factorials
« Reply #26 on: January 30, 2011, 05:56:25 pm »
Oh, you didn't need to correct the variables names. It was just an future advice. Sometimes I also end mixing different conventions.
I speak Portuguese. See your personal messages. ;)
Hobbing in calculator projects.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Factorials
« Reply #27 on: January 31, 2011, 08:55:43 am »
Oh, you didn't need to correct the variables names. It was just an future advice. Sometimes I also end mixing different conventions.
I speak Portuguese. See your personal messages. ;)

I don't usually mix, it's all in english, this challenges, however, were made for a portuguese website.