PRIMES.PR 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #noedit
  2. -- A frivolous example project file, which calculates the first 30 primes
  3. -- using the Sieve of Erasthingy algorithm.
  4. -- The calculation is performed using only the constructs built in to the
  5. -- TopSpeed Project language. Macro expansion is used to provide looping
  6. -- construct and simple (base 1) arithmetic.
  7. -- unary to decimal conversion macros
  8. #set writeit = '
  9. #if "%%reset" #then
  10. #set wrcount = ""
  11. #set d1 = 0
  12. #set d11 = ""
  13. #set d111 = ""
  14. #endif
  15. %%wr2
  16. #set number = %%d111%%%%d11%%%%d1
  17. '
  18. #set wr2 = '
  19. #if #not ("%%wrcount%%" = "%%it%%") #then
  20. #set digit = 1
  21. %%increment
  22. #set wrcount = %%wrcount%%1
  23. %%wr2
  24. #endif
  25. '
  26. #set increment = '
  27. #set val = d%%digit %%getval
  28. #if "%%result" = "" #then #set %%val = 1
  29. #elsif %%result = 0 #then #set %%val = 1
  30. #elsif %%result = 1 #then #set %%val = 2
  31. #elsif %%result = 2 #then #set %%val = 3
  32. #elsif %%result = 3 #then #set %%val = 4
  33. #elsif %%result = 4 #then #set %%val = 5
  34. #elsif %%result = 5 #then #set %%val = 6
  35. #elsif %%result = 6 #then #set %%val = 7
  36. #elsif %%result = 7 #then #set %%val = 8
  37. #elsif %%result = 8 #then #set %%val = 9
  38. #elsif %%result = 9 #then
  39. #set %%val = 0
  40. #set digit = %%digit%%1
  41. %%increment
  42. #endif
  43. '
  44. -- macro to perform a for-loop
  45. #set for_loop = '
  46. #if #not ("%%i" = "%%end") #then
  47. %%body
  48. #set i = %%i%%1
  49. %%for_loop
  50. #endif
  51. '
  52. #set getval = "#set result = %%%%%%val"
  53. #set SIZE = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"
  54. -- SIZE = 120
  55. #set i=1 #set end = %SIZE -- for(i = 0; i<=SIZE;i++)
  56. #set body="#set %%i%%=on" -- flags[i]=TRUE;
  57. %for_loop
  58. #set i=%SIZE%1 #set end = %SIZE%%SIZE%1 -- for(i = SIZE+1; i<=2*SIZE;i++)
  59. #set body="#set %%i%%=toobig" -- flags[i]=TOOBIG;
  60. %for_loop
  61. -- count=0;
  62. #set count = ""
  63. #set i=11 #set end = %SIZE -- for(i=2;i <= SIZE;i++){
  64. #set body = "
  65. #set val = %%i%% %%getval
  66. #if %%result #then -- if(flags[i]){
  67. #set it = %%i%%
  68. %%writeit
  69. #message '%%number%% is prime'
  70. #set k=%%i%%%%i%% -- k=i+i;
  71. %%while_loop -- (see below)
  72. #set count=%%count%%1 -- count = count+1;
  73. #endif -- }
  74. " -- }
  75. #set while_loop = ' -- while(k<=SIZE){
  76. #set val = %%k%%
  77. %%getval
  78. #if #not ("%%result%%" == toobig) #then
  79. #set %%k%%=off -- flags[k] = FALSE;
  80. #set k = %%k%%%%i%% -- k += i;
  81. %%while_loop
  82. #endif
  83. '
  84. %for_loop
  85. #set it = %count
  86. #set reset = on
  87. %writeit
  88. #message "%number primes found"
  89.