Advertisement
Guest User

Prime Number Checker v2

a guest
Sep 15th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////
  2. // M. Quartz (Second Life - Fri Sep 15, 2018)
  3. // Updated to act more robust.
  4. //
  5. // Since I'm too lazy to manually check if a number is
  6. // a prime or not, why not let lsl check for me:
  7.  
  8. // Create a prim in-world and drop this script in.
  9. // Use channel 22 and type a number in public
  10. // chat such as "/22 100" without quotes.
  11.  
  12. // Okay. If your number happens to be a prime
  13. // This script will say so. If your number happens
  14. // to NOT be a prime, the script will display a list
  15. // of all existing factors of that number in question.
  16.  
  17. // NOTE: The bigger the number, the longer
  18. // this takes, so go easy and keep it
  19. // under 4 digits lest wait!
  20. //
  21. ////////////////////////////////////////////////////////////
  22. #define int         integer
  23. #define str         string
  24. #define lst         list
  25. #define print0(a)   llOwnerSay(a)
  26.  
  27. default
  28. {
  29.     state_entry()
  30.     {
  31.         llListen(22,"",NULL_KEY,"");
  32.     }
  33.     listen(int c, str n, key i, str m)
  34.     {
  35.         if (llGetOwnerKey(i) == llGetOwner())
  36.         {
  37.             if ((int)m)
  38.             {
  39.                 print0("Please wait. This may take awhile..");
  40.                
  41.                 lst t;
  42.                 int x; int e;
  43.                 while ((++x)<(int)m)
  44.                 {
  45.                     int y; ++e;
  46.                     while ((++y)<=x)
  47.                    
  48.                         if ((e*y) == (int)m)
  49.                             t += ["("+(str)e+" * "+(str)y+")"];
  50.                 }
  51.                 if (t != [])
  52.                 {
  53.                     int n  = llGetListLength(t);
  54.                     print0("Multiplication factors found: "+(str)n+"\n"+llDumpList2String(t, "\n")
  55.                                 +"\nTherefore "+"\""+m+"\" is NOT a prime number."); t = [];
  56.                 }
  57.                 else
  58.                     print0("No factors found: "+"\""+m+"\" is definitely a prime number.");
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement