Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////////////
- // Written by M.Quartz of Second Life. Mon Oct 8, 2018: This script
- // calculates the factorial sum of a chosen integer using a recursion
- // method instead of a loop.
- //
- // https://en.wikipedia.org/wiki/Triangular_number
- //
- //////////////////////////////////////////////////////////////////////////
- // Recursive functions hog memory and can heap collide very quickly
- // especially with lsl scripts capped at 64k. So limit the input to
- // avoid memory problems.
- #define LIMIT 1600
- //////////////////////////////////////////////////////////////////////////
- #define int integer
- #define str string
- #define rtn return
- #define print(a) llSay(0, a)
- //////////////////////////////////////////////////////////////////////////
- #define RCT "[Recursion Test] "
- #define MES1 "Enter a number to calculate it's factorial sum: \n"
- #define MES2(a,b) "Triangular sum of "+a+" = "+b+"\n"
- #define ERR(a) "Triangular sum of "+a+" = E\n"
- //////////////////////////////////////////////////////////////////////////
- // Recursive Function: To simulate
- // this 𝚺 Formula: \sum_{x=1}^{n}x!
- int x(int n)
- {
- if (n <= 1)
- rtn 1;
- else
- rtn n+x(n-1); // Calls on itself
- }
- //////////////////////////////////////////////////////////////////////////
- // Begin Events:
- default
- {
- state_entry()
- {
- llListen(0, "", llGetOwner(), "");
- print(RCT+MES1);
- }
- listen(int c, str N, key i, str m)
- {
- int n = (int)m;
- if (n > 0 &&
- n < LIMIT) {
- str t = (str)x(n);
- print(RCT+MES2(m,t));
- }
- else
- print(RCT+ERR(m)); // Because: 32 bit
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement