How to calculate the factorial of n using stacks

recursive function for factorial using stack
algorithm to find factorial of a number using recursion
fibonacci using stack
factorial function
factorial using recursion
recursion using stack
relation between stack and recursion
recursion and stack in c

I need to calculate the factorial of n using stacks and the code I made doesn't return any results. I also don't know what pop stack really does ( What it's second parameter is for ) so I just used a random value there. I used int **x; because I didn't know what to put here pop(&mystack,*x);.

#include <iostream>
using namespace std;
int n;
int aux;
int aux1;
int aux2;
int **x;
typedef struct {
    int content[100];
    int top;
} stack;
stack mystack;

int push(stack *somestack,int somevalue)
{
    if (somestack->top+1>=100)
        return 1;
    (*somestack).top++;
    (*somestack).content[(*somestack).top]=somevalue;
    return 0;
}

int pop(stack *somestack, int *oldvalue)
{
    if((*somestack).top==0)
    {
        return 1;
    }
    *oldvalue=(*somestack).content[(*somestack).top];
    return 0;
}

int main()
{
    cout<<"n=";
    cin>>n;
    push(&mystack,n);
    int direction=1;
    while(mystack.top>=1)
    {
        if((direction==1)&&(mystack.content[mystack.top]>1))
        {
            aux=mystack.content[mystack.top];
            push(&mystack,aux-1);
        }
        else
        {
            if(mystack.content[mystack.top]==1)
            {
                direction=0;
            }
            else
            {
                if(aux1<n)
                {
                    aux1=mystack.content[mystack.top];
                    aux2=aux1*(aux1+1);
                    pop(&mystack,*x);
                    mystack.content[mystack.top]=aux2;
                }
            }
        }
    }
    cout<<endl<<mystack.content[0];
    return 0;
}

Stacks have push and pop operations. Push adds a new item to the top of the stack and pop removes the item from the top of the stack and returns it. Some pseudocode for factorial:

int factorial(int n) {
    Stack<int> stack;
    stack.push(1);

    for(int i=1; i<=n; ++i) {
       stack.push(stack.pop()*i);
    }
    return stack.pop();
}

Example: the Factorial Function � Data Structures and Algorithms, (read n factorial) where n is an integer. give me the factorial of any number I can use it to find the factorial of that number-1 Thus, we can now pop the stack. For example for n=5 the following stacks will have to be maintained . f (5) -> f (4) -> f (3) -> f (2) -> f (1) ->f (0) As we can see that 5 stacks will have to be maintained until a call to f(0) is reached whose value is known and is returned. Therefore for n factorial, n stacks will have to be maintained. Thus space complexity is O(n).

Use the interpreter pattern, and its nothing more than putting the numbers from n..2 in the stack (exclude 1 as it is the base case)

package main

import "github.com/ayalaio/utils/stack"

type MathFact struct {
    Content int
}

func (self *MathFact) FactorialFunc() func(int) int {
    return func(j int) int {
        return self.Content * j
    }
}

func main() {
    s := stack.New()

    factorialOf := 6

    curr := factorialOf

    for curr >= 2 {
        s.Add(&MathFact{curr})
        curr--
    }

    // 1 -> base case
    r := 1
    for s.Len() > 0 {
        e := s.Front()
        ff := e.Value.(*MathFact).FactorialFunc()
        r = ff(r)
        s.Remove()
    }
    println(r)
}

Program for factorial of a number, Recursive Solution: Factorial can be calculated using following recursive formula. n! = n * (n-1)! n! = 1 if n = 0 or� Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720. Recursive Solution: Factorial can be calculated using following recursive formula. n! = n * (n-1)! n! = 1 if n = 0 or n = 1

This is what I cam up with to factorialize a number without recursion (using a stack).

#include <iostream>
#include <stack>

using namespace std;

void main()
{
    stack<int> myStack;
    int userFactor, tempFactor, stackLoop;
    int runTotal = 0;

    cout << "Enter number to be factorialized: ";
    cin >> userFactor;
    tempFactor = userFactor;

    if (userFactor == 1 || userFactor == 0) //a "base case" of sorts
        cout << endl << userFactor << " factorialized is 1.\n";

    else
    {
        while (tempFactor > 1) //load the stack
        {
            myStack.push(tempFactor);
            tempFactor--;
        }

        runTotal = myStack.top(); //start unloading the stack
        myStack.pop();
        stackLoop = (int)myStack.size();

        for (int x = 0; x < stackLoop; x++) //multiply each 
        {
            runTotal *= myStack.top();
            myStack.pop();
        }
        cout << endl << userFactor << " factorialized is: " << runTotal << endl;
    }
}

Algorithms 13: Using Stack – recursion, A simpler problem is to compute the factorial value of a number. The values of n stored in each stack frame can be seen using PyCharm's� int n; int i; // loop variable int ans = 1; // stroes the final answer int TOP = -1; // stack variables that mainntain stack's top int s[MAX]; // the stack printf(" Enter number: "); scanf("%d",&n); // here we can also make sure that the use is not entering a number // that can not be accomodated in the stack // if the user enters a number

Factorial.java, <P> <br>Morgan McGuire morgan@cs.williams.edu */ public class Factorial { /** Standard recursive definition */ static public int factorialRecursive(int n) { if (n it iterative by using an explicit stack variable to act like the program's own stack. This makes calculating the products of a long list of number pairs relatively easy. For n!, where n=10, we know that 1*10=10, we also know that the next pairing will result in 10+8, or 18 the next paring will result in a product of 18+6=24, followed by 24+4=28, and finally by 28+2=30.

Using a Stack to Implement Recursion, Using a Stack to Implement Recursion. (define (factorial n) (if (= n 1) 1 (* ( factorial (- n 1)) n))) As we see from the similarly had to compute another GCD. Calculator Use. Instead of calculating a factorial one digit at a time, use this calculator to calculate the factorial n! of a number n. Enter an integer, up to 4 digits long. You will get the long integer answer and also the scientific notation for large factorials.

factorial using stack, then we can not find factorial if(n<=0) { printf("\nThe number can not be less than 0"); } else { // push the numbers n,n-1 .1 in the stack Factorial of a non-negative integer, is the multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720. We have discussed simple program for factorial. How to compute factorial of 100 using a C/C++ program? Factorial of 100 has 158 digits.

Comments
  • It looks as if you have copy/pasted some code and you are looking for advice as to how to debug it. I recommend that you write your own code to accomplish your task, using this code a bit of a guideline. Then, when you are actually getting result, you will likely find the solution on your own. If you get stuck after that, we're here.
  • It make sense to move push/pop free functions into member functions of stack.