[C++] Simple factorial algorithm

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C++] Simple factorial algorithm

Post by ayu »

This is a "fun code" that we wrote during a not so fun lecture. It's not very long, most of the fun was done on paper.

Mathematical term is x!

Example: 3! = 3*2*1 = 6

Code: Select all

long long int Fakultet(int n)
{
       long long int sum = 1;

       for(; n>0; --n)
              sum*=n;

       return sum;
}
Last edited by ayu on 05 Dec 2009, 18:24, edited 1 time in total.
"The best place to hide a tree, is in a forest"

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

The English term is "Factorial". We use the same word as "Faculty" in Dutch too (faculteit) :)

Recursive version:

Code: Select all

long long int fact(int n)
{
  if (n == 0) return 1;
  return n * fact(n-1);
} 
Tail recursive version:

Code: Select all

long long int fact_i(int n, int product) {
  if(n == 1) return product;
  else return fact_i(n - 1, product*n);
}

long long int fact(int n) {
    if(n == 0) return 1;
    fact_i(n, 1);
}

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

heh yeah thanks for pointing that out leetnigga ^^
"The best place to hide a tree, is in a forest"

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

Nice to see people who knows about Tail recursivity. I don't see why would it be good to apply it here though ;)

the problem with recursivity without being tail recursive is the stack overflow. Using the tail recursivity simply mean that when you found the answer, you jump directly back to the upper environement.
Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

User avatar
Lundis
Distorter of Reality
Distorter of Reality
Posts: 543
Joined: 22 Aug 2008, 16:00
15
Location: Deadlock of Awesome
Contact:

Post by Lundis »

I didn't know C++ supported tail-recursion! :o

I googled it and found this... thought it would be useful:
http://danzig.jct.ac.il/cpp/recursion.html

User avatar
leetnigga
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 447
Joined: 28 Jul 2009, 16:00
14

Post by leetnigga »

Nerdz wrote:Nice to see people who knows about Tail recursivity. I don't see why would it be good to apply it here though ;)
Because this is a classic example where the stack grows linearly. I forgot to give product the long long int type, by the way:

Code: Select all

long long int fact_i(int n, long long int product) {
  if(n == 1) return product;
  else return fact_i(n - 1, product*n);
}

long long int fact(int n) {
    if(n == 0) return 1;
    fact_i(n, 1);
} 
It's true that G++ is able to optimize this by itself.
Nerdz wrote:the problem with recursivity without being tail recursive is the stack overflow. Using the tail recursivity simply mean that when you found the answer, you jump directly back to the upper environement.
Well, a regular recursive function winds the stack, executes the function, calls itself (which winds the stack, executes the function, calls itself, etc, etc) and unwinds (unwinds, unwinds, unwinds, etc, etc) when the answer is found. This will cause a stack overflow when the function calls itself too often.

A tail recursive function winds the stack once, executes the function, and when it needs to call itself (which is always the last operation in a tail-recursive function, because that's what makes a function tail-recursive) it simply jumps back to the start of the function (though after the winding of the stack). This way the overhead of pushing and popping stack frames all the time is removed.

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Post by IceDane »

leetnigga wrote:
Nerdz wrote:Nice to see people who knows about Tail recursivity. I don't see why would it be good to apply it here though ;)
Because this is a classic example where the stack grows linearly. I forgot to give product the long long int type, by the way:

Code: Select all

long long int fact_i(int n, long long int product) {
  if(n == 1) return product;
  else return fact_i(n - 1, product*n);
}

long long int fact(int n) {
    if(n == 0) return 1;
    fact_i(n, 1);
} 
It's true that G++ is able to optimize this by itself.
Nerdz wrote:the problem with recursivity without being tail recursive is the stack overflow. Using the tail recursivity simply mean that when you found the answer, you jump directly back to the upper environement.
Well, a regular recursive function winds the stack, executes the function, calls itself (which winds the stack, executes the function, calls itself, etc, etc) and unwinds (unwinds, unwinds, unwinds, etc, etc) when the answer is found. This will cause a stack overflow when the function calls itself too often.

A tail recursive function winds the stack once, executes the function, and when it needs to call itself (which is always the last operation in a tail-recursive function, because that's what makes a function tail-recursive) it simply jumps back to the start of the function (though after the winding of the stack). This way the overhead of pushing and popping stack frames all the time is removed.
Hmm, that is interesting. I didn't know that it g++ would optimize that out either. Any idea if other compilers do this automatically as well?

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Post by l0ngb1t »

leetnigga wrote: Recursive version:

Code: Select all

long long int fact(int n)
{
  if (n == 0) return 1;
  return n * fact(n-1);
} 



i think u r missing an else
hope that i'm right

Code: Select all

 if (n == 0) return 1; else 

without it, it will jump to the next next line of code even if n==0
am i right ????
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
IceDane
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 197
Joined: 12 Aug 2009, 16:00
14

Post by IceDane »

l0ngb1t wrote:
leetnigga wrote: Recursive version:

Code: Select all

long long int fact(int n)
{
  if (n == 0) return 1;
  return n * fact(n-1);
} 



i think u r missing an else
hope that i'm right

Code: Select all

 if (n == 0) return 1; else 

without it, it will jump to the next next line of code even if n==0
am i right ????
Normally, yes. But "return 1;" terminates the function. So the else would be redundant in this case.

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Post by l0ngb1t »

mmm yeah
apparently i missed the "return 1"
:oops: :oops: :oops:
i should read carefully next time
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

Post Reply