home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / programming / misc_programming / batnmore.z!p / PRIME.BTM < prev    next >
Text File  |  1992-11-15  |  3KB  |  78 lines

  1. *REM This batch file is Freeware, free to use and redistribute unmodified *
  2. *REM Jouni Miettunen * jon@stekt.oulu.fi * Oulu * Finland * Europe * 1992 *
  3.  
  4. goto alku
  5. :W
  6. set peek=2^set try=%@eval[%try+2+(%try+1)%%3+%@int[%@eval[1/((%try+2+(%try+1)%%3)%%5+1)]]*((%try+1)%%5)]^if %try gt %limit goto end
  7. :L
  8. set peek=%@eval[%peek+1]^if %@eval[%[try]%%%[%peek]]==0 goto W^if %@eval[(%[%peek]+2)*(%[%peek]+2)] le %try goto L^iff %@eval[%_column+%@len[%try]+2] le 80 then^if %_column != 0 echos ` `^else^echo.^endiff^screen %_row %_column %try,^goto W
  9.  
  10. REM Calculate Prime Numbers using modified "Sieve of Eratosthenes"
  11. REM Timestamp 15-Nov-1992
  12.  
  13. REM why I'm doing all this jumping? Well, it's called speed optimization
  14. REM Basically ":W" above is same as ":work" below and equally ":L" and ":loop"
  15. REM They are placed in the beginning to be located as fast as possible (?)
  16.  
  17. :alku
  18. break on^setlocal^unalias *^unset *^if %# gt 0 goto error
  19.  
  20. REM set up the working environment and defaults..
  21. set limit=11111111^set index=4^set 1=2^set 2=3^set 3=5^set 4=7^set try=7
  22.  
  23. REM Here are 3 separate outputs: comma-separated, space separated and columns.
  24. REM To change output change 3 places: below the startup, in :loop and in :L.
  25. REM Note: one (1) is not a prime number.
  26.  
  27. echos 2, 3, 5, 7,
  28. REM echos 2 3 5 7
  29. REM echos        2       3       5       7
  30.  
  31. :work
  32. REM save time and check only some numbers..
  33. REM 2 is a prime number, so I don't have to check numbers dividable by 2
  34. REM 3 is a prime number, so I don't have to check numbers dividable by 3
  35. REM 5 is a prime number, so I don't have to check numbers dividable by 5
  36. set peek=3^set try=%@eval[%try+2+(%try+1)%%3+%@int[%@eval[1/((%try+2+(%try+1)%%3)%%5+1)]]*((%try+1)%%5)]
  37.  
  38. :loop
  39. set peek=%@eval[%peek+1]^if %@eval[%[try]%%%[%peek]]==0 goto work
  40.  
  41. REM don't have to check against every prime number already found..
  42. if %@eval[(%[%peek]+2)*(%[%peek]+2)] le %try goto loop
  43.  
  44. REM "1229" took 9 bytes Environmental Memory, 15 enough for at least 6digits
  45. iff %_env gt 15 then^set index=%@eval[%index+1]^set %index=%try
  46. else^set limit=%@eval[%try*%try]^endiff
  47.  
  48. REM current place + space + number + comma equal max 80 characters on line.
  49. REM Note: "n" is NEWLINE _except_ with "echos n" it's LINEFEED, watch out.
  50. REM IMHO a character should have only one definition, not two. Weird.
  51. REM 4DOS 4.01 rev D
  52.  
  53. iff %@eval[%_column+%@len[%try]+2] le 80 then^if %_column != 0 echos ` `
  54. else^echo.^endiff^screen %_row %_column %try,
  55.  
  56. REM Listing without commas; to use this comment 2 previous lines etc.
  57. REM iff %@eval[%_column+%@len[%try]+1] le 80 then^if %_column != 0 echos ` `
  58. REM else^echos rn^endiff^screen %_row %_column %try
  59.  
  60. REM If you prefer columns.. you need to fix echoing of 2,3,5,7
  61. REM also fix the output of the later part of this batch file (:L)
  62. REM if %@eval[%_column+8] gt 80 echos nr
  63. REM screen %_row %@eval[%_column+8-%@len[%try]] %try
  64.  
  65. if %limit==11111111 goto work
  66.  
  67. REM All Env Memory is used up, now go and calculate, what u can w/primes u got
  68. goto W
  69.  
  70. :error
  71. echo Usage: %@lower[%@name[%0]]^echo.
  72.  
  73. :end
  74. break off^quit
  75.  
  76. REM Now this one was fun to write! BTW: did u notice I used full 80 columns
  77. REM not just the usual 79 ... B-)
  78.