Advertisement
Guest User

Prime Detector

a guest
Nov 28th, 2018
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ////////////////////////////////////////////////////////////
  2. // M. Quartz: Check an input number to determine whether
  3. // its a prime or a composite.
  4.  
  5. ////////////////////////////////////////////////////////////
  6.  
  7. #define int         integer
  8. #define str         string
  9. #define lst         list
  10.  
  11. #define print(a)    llOwnerSay(a)
  12.  
  13. ////////////////////////////////////////////////////////////
  14. // Keeping the time, size short and modest:
  15. #define MCR_LIMIT    1500
  16. //
  17. ////////////////////////////////////////////////////////////
  18. default
  19. {
  20.     state_entry()
  21.     {
  22.         llListen(22, "", llGetOwner(), "");
  23.     }
  24.     listen(int c, str n, key i, str m)
  25.     {
  26.         if ((int)m < MCR_LIMIT) // capped
  27.         {
  28.             print("Scanning integer: This may take awhile..");
  29.  
  30.             lst t; // fishing net
  31.             int z = (int)m; // typecast
  32.             /////////////////////////////////////////////////////////////////////////////////
  33.             int x; // rows up
  34.             while (++x <z)
  35.             {
  36.                 int y; // columns across
  37.                 while (++y <=x)
  38.                    
  39.                     if (x*y == z) // capture
  40.                         t += ["("+(str)x+" * "+(str)y+")"]; // collect
  41.             }
  42.             /////////////////////////////////////////////////////////////////////////////////
  43.             if (t != []) // caught anything?
  44.             {
  45.                 int n  = llGetListLength(t); // how many
  46.                 print("Factors found: "+(str)n+"\n"+llDumpList2String(t, "\n")+ // list
  47.                         "\nTherefore integer \""+m+"\" is a composite."); t = []; // clear
  48.             } else
  49.                 print("No factors found: Therefore integer \""+m+"\" is a prime."); // prime
  50.             m = ""; // tidy
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement