Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Help with Factorials?

edited June 2014 in Questions Posts: 29

I've been trying to get this simple Factorial program that takes a numeral and returns it's factorial...it keeps telling me (n) is a nil value, what am I doing wrong?

function setup ()
    print ("hai")
-- defines a factorial function
function fact(n)
    if n == 0 then
        return 1
        else
        return n * fact(n-1)
    end
end

print ("enter a number:")
a = io.read("*n")  --reads a number
print(fact(a))
    end

Comments

  • dave1707dave1707 Mod
    edited June 2014 Posts: 7,879

    @D4rk_Code Codea doesn't use io.read from the keyboard in that manner. Here's a different approach for input of a value. Enter a value into the "value" area and then press "factorial".


    function setup () parameter.text("value",0) parameter.action("factorial",xx) end function xx() output.clear() print(value.." factorial is ",fact(value)) end function fact(n) n=tonumber(n) if n==0 then return 1 else return(n*fact(n-1)) end end
  • Posts: 29

    Thanks, I'm working through book from lua.org "Programming in Lua" to grasp a better understanding of the language, this was one of it's tutorials, I was wondering why it never worked on my iPad :D

  • dave1707dave1707 Mod
    Posts: 7,879

    While the Lua book gives you a good understanding of how to program in Lua, Codea doesn't do everything in the book. You might also want to go thru the built in documentation of what Codea does so you have an idea of what you can and can't do. If there's something you don't understand, there are plenty of people here that will help.

  • Posts: 29

    okie, I was just a little confused because it worked on linux Lua compiler

  • I am an absolute beginner on coding and I too am following the book but I don't understand how I am going to learn when It comes to this stuff. Is there a book or something that I can start by that will teach me everything that I need to know when it comes to Codea as well? I don't get why I would have to learn Codea and then learn Lua as well. Is this one of the only differences? Is there a source that I can use to get all of the differences in the book to the program?

  • dave1707dave1707 Mod
    Posts: 7,879

    See the links at the top of the forum page. Select Wiki and look at those.

  • IgnatzIgnatz Mod
    Posts: 5,396

    I wrote some ebooks specifically for Codea and Lua, here

    https://www.dropbox.com/sh/mr2yzp07vffskxt/AACqVnmzpAKOkNDWENPmN4psa

  • Ignatz I recently started reading one of you're books. I love it so far! But the loops are not working and it is frustrating me lol. Thank you for your response.

  • dave1707dave1707 Mod
    Posts: 7,879

    @abstractartist07 Here's an example of 2 simple loops.

    function setup()
        for a=1,10 do   -- loop to do something 10 times
            print(a,a*a)   -- prints a number and that number times itself
        end
    
        t={2,4,6,8,10,12,14}
        for nbr,value in pairs(t) do  -- loop to print values in a table.
            print(nbr,value)  -- print a table position and the value there.
        end
    end
    
Sign In or Register to comment.