stack overflow error while implementing a Recursive Function(factorial)

iamgr007

I tried to implement a factorial function like this:

function factorial(n)
    if (n == 0) then
        return 1
    else
        return n * factorial(n - 1)
    end
end

io.write("number?")
n =io.read()
fac = factorial(n)
print("factorial of",n,"=",fac)

It works fine until I give 0 as input. It returns

lua: factorial.lua:5: stack overflow
stack traceback:
                factorial.lua:5: in function 'factorial'

What am I doing wrong?

Also, It gives normal output only till 16. when I give n=17, output is 3.55687428096e+014

How to get it right?

iamgr007

To get "0" to work, tell read to read a number: n = io.read("*n")

To get the normal notation instead of scientific notation,use

print("factorial of",n,"=",string.format("%0f",fac))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

stack overflow error while implementing a Recursive Function(factorial)

From Dev

Stack overflow error in a recursive method

From Dev

Stack Overflow error while looping

From Dev

Stack Overflow error from recursive directory traversal

From Dev

Recursive Java method giving stack overflow error

From Dev

Stack Overflow error from recursive directory traversal

From Dev

Stack overflow while processing large array in recursive function

From Dev

C++ Stack Overflow while using recursive function

From Dev

Stack Overflow Error Implementing Quicksort (with duplicates) in C#

From Dev

Evaluating if a stack overflow error is possible in a tree traversal recursive algorithm (Java)

From Dev

Prevent infinite recursion/stack overflow error in a recursive R function

From Dev

How to solve recursive call that causes stack overflow error in Android?

From Dev

Causes of stack overflow in recursive functions

From Dev

recursive validation function stack overflow

From Dev

recursive stack overflow in fibonacci function

From Dev

Stack Overflow error while performing Search in Binary search tree

From Dev

Error in implementing stack in c

From Dev

Error in implementing stack in c

From Dev

stack overflow error in delphi

From Dev

Stack overflow error with recursion

From Dev

Implementing Default on nested structures causes a stack overflow

From Dev

Implementing Default on nested structures causes a stack overflow

From Dev

Lua stack overflow in recursive function in corona sdk

From Dev

Stack overflow in mutually recursive scala parser

From Dev

How to avoid stack overflow in this code, (Recursive function)

From Dev

Lua stack overflow in recursive function in corona sdk

From Dev

F# recursive function arguments and stack overflow

From Dev

How to get around recursive stack overflow?

From Dev

Stack overflow in recursive function (language C)

Related Related

  1. 1

    stack overflow error while implementing a Recursive Function(factorial)

  2. 2

    Stack overflow error in a recursive method

  3. 3

    Stack Overflow error while looping

  4. 4

    Stack Overflow error from recursive directory traversal

  5. 5

    Recursive Java method giving stack overflow error

  6. 6

    Stack Overflow error from recursive directory traversal

  7. 7

    Stack overflow while processing large array in recursive function

  8. 8

    C++ Stack Overflow while using recursive function

  9. 9

    Stack Overflow Error Implementing Quicksort (with duplicates) in C#

  10. 10

    Evaluating if a stack overflow error is possible in a tree traversal recursive algorithm (Java)

  11. 11

    Prevent infinite recursion/stack overflow error in a recursive R function

  12. 12

    How to solve recursive call that causes stack overflow error in Android?

  13. 13

    Causes of stack overflow in recursive functions

  14. 14

    recursive validation function stack overflow

  15. 15

    recursive stack overflow in fibonacci function

  16. 16

    Stack Overflow error while performing Search in Binary search tree

  17. 17

    Error in implementing stack in c

  18. 18

    Error in implementing stack in c

  19. 19

    stack overflow error in delphi

  20. 20

    Stack overflow error with recursion

  21. 21

    Implementing Default on nested structures causes a stack overflow

  22. 22

    Implementing Default on nested structures causes a stack overflow

  23. 23

    Lua stack overflow in recursive function in corona sdk

  24. 24

    Stack overflow in mutually recursive scala parser

  25. 25

    How to avoid stack overflow in this code, (Recursive function)

  26. 26

    Lua stack overflow in recursive function in corona sdk

  27. 27

    F# recursive function arguments and stack overflow

  28. 28

    How to get around recursive stack overflow?

  29. 29

    Stack overflow in recursive function (language C)

HotTag

Archive