Author Topic: Lua Routines  (Read 13392 times)

0 Members and 2 Guests are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Lua Routines
« on: June 15, 2011, 09:33:10 am »
It seems like there is no topic for us to group our TI-Nspire Lua routines. Feel free to post here your Lua routines, whether they are directly connected to Nspire or not. Don't forget to try and optimize other people's functions if you can.

To start, how about a function that checks if a number is prime or not? I tried to find one online but I couldn't, so I ended up making my own.

Check if a number is prime
Input:   One argument, an integer
Output: true or false, whether the argument is a prime or not

Code: [Select]
function isPrime(n)
    --[[Check if an integer n is a prime or not--]]
    if n==1 then return false end --1 is not a prime
    for i = 2, math.sqrt(n) do
    if n % i == 0 then
    return false
    end
    end
    return true
end

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Routines
« Reply #1 on: June 15, 2011, 10:12:13 am »
My version:
Code: [Select]
function isPrime(n)
    return math.eval("isprime("..n..")")
end

I don't know if its faster though.

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Routines
« Reply #2 on: June 15, 2011, 06:30:46 pm »
Code: [Select]
function isPrime(n)
    --[[Check if an integer n is a prime or not--]]
    if n==1 then return false end --1 is not a prime
    for i = 2, math.sqrt(n) do
    if n % i == 0 then
    return false
    end
    end
    return true
end
What exactly does the math.sqrt do?

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Lua Routines
« Reply #3 on: June 15, 2011, 06:37:43 pm »
It limits the number of times the loop will check, since one of the factors of any number n is at most sqrt(n).

That said, the routine given will take a loooong time for large primes.
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Routines
« Reply #4 on: June 15, 2011, 06:50:30 pm »
What happens if the square root is irrational?

Offline AngelFish

  • Is this my custom title?
  • Administrator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3242
  • Rating: +270/-27
  • I'm a Fishbot
    • View Profile
Re: Lua Routines
« Reply #5 on: June 15, 2011, 07:08:12 pm »
It doesn't compute the actual square root, only an approximation to it. That means that the interpreter will always see a rational number, whether or not the true square root actually is rational.
« Last Edit: June 15, 2011, 07:09:27 pm by Qwerty.55 »
∂²Ψ    -(2m(V(x)-E)Ψ
---  = -------------
∂x²        ℏ²Ψ

Offline pianoman

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 426
  • Rating: +24/-0
  • ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫ ♪♫
    • View Profile
Re: Lua Routines
« Reply #6 on: June 15, 2011, 07:08:40 pm »
Oh, got it. Thanks!

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Lua Routines
« Reply #7 on: June 20, 2011, 05:06:40 am »
Draw a triangle
Input:   Graphics Context, x, y, length of side
Result:  Draws a triangle at (x,y) in the screen with each side having length of l

Code: [Select]
function drawTriangle(gc,x,y,l)
    gc:drawLine(x,y,x+l,y)
    gc:drawLine(x, y, x+(l/2), y-(l*0.5))
    gc:drawLine(x+(l/2), y-(l*0.5), x+l, y)
end

All the sides of the triangle are of equal length and the (x,y) coordinates are of the left point of the triangle.

Now, I know this is not very hard math (simple trigonometry) but I think it might be useful for someone :) Any optimizations?

Here's how it works:


Ashbad

  • Guest
Re: Lua Routines
« Reply #8 on: June 21, 2011, 09:46:47 pm »
Pause for an exact amount of time, in milliseconds

Code: [Select]
WaitMS = function(milliseconds)
   while (stime = math.abs(timer.GetMilliSecCounter()+milliseconds))<
            math.abs(timer.getMilliSecCounter()) do end
   return true
end

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: Lua Routines
« Reply #9 on: June 21, 2011, 11:26:31 pm »
Draw a triangle
Input:   Graphics Context, x, y, length of side
Result:  Draws a triangle at (x,y) in the screen with each side having length of l

Code: [Select]
function drawTriangle(gc,x,y,l)
    gc:drawLine(x,y,x+l,y)
    gc:drawLine(x, y, x+(l/2), y-(l*0.5))
    gc:drawLine(x+(l/2), y-(l*0.5), x+l, y)
end

All the sides of the triangle are of equal length and the (x,y) coordinates are of the left point of the triangle.

Now, I know this is not very hard math (simple trigonometry) but I think it might be useful for someone :) Any optimizations?

Here's how it works:


If that's really an equilateral triangle, shouldn't the square root of 3 be involved somehow?
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Routines
« Reply #10 on: June 22, 2011, 02:59:33 am »
Pause for an exact amount of time, in milliseconds

Code: [Select]
WaitMS = function(milliseconds)
   while (stime = math.abs(timer.GetMilliSecCounter()+milliseconds))<
            math.abs(timer.getMilliSecCounter()) do end
   return true
end

Wouldn't that use extremely many resources?
I would suggest to run the code in a coroutine, and call a routine that pauses it and sets the timer to the amount of seconds you want. Then when the on.timer get called it resumes the coroutine. This is the way I do it in my oncalc Lua console, and it works good (Doesn't interrupt everything, and it will still catch user input).

Ashbad

  • Guest
Re: Lua Routines
« Reply #11 on: June 22, 2011, 07:45:05 am »
Well, it should't really use that many resources :P plus, my routine would be better if it got rid of the horrible on.event things that would happen at the same time of the loop.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Routines
« Reply #12 on: June 22, 2011, 08:13:10 am »
It probably does, as it doesn't react to anything while its looping (read: the calc freezes).

Ashbad

  • Guest
Re: Lua Routines
« Reply #13 on: June 22, 2011, 08:20:16 am »
Well, that's the point :P. It's supposed to stop all execution for that amount of time, like a pausing loop in z80 assembly that denies interrupt execution does.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Lua Routines
« Reply #14 on: June 22, 2011, 08:46:11 am »
Well, that is what you call a busy sleep. You can't have this kind of stuff on a computer, as it will takes lots of cpu power. And with nspire Lua you can have multiple script apps (basic multitasking), so this isn't the best solution.
Well, it actually depends on what you want to do :)