sieve.csp 646 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. define MAX 1000
  2. define NS 10
  3. SIEVE ==
  4. [
  5. i = 0 ->
  6. console!2;
  7. n : integer := 3;
  8. *[
  9. n < MAX -> sieve[1]!n; n := n + 2
  10. ]
  11. []
  12. i = NS + 1 ->
  13. n : integer;
  14. *[
  15. sieve[NS]?n -> console!n
  16. ]
  17. []
  18. (i/=0) & (i/=NS + 1) ->
  19. p, mp : integer;
  20. sieve [i-1]?p;
  21. console!p;
  22. mp := p;
  23. m : integer;
  24. *[
  25. sieve[i-1]?m ->
  26. * [ m > mp -> mp := mp + p];
  27. [
  28. m = mp -> skip
  29. []
  30. m < mp -> sieve[i+1]!m
  31. ]
  32. ]
  33. ]
  34. MAIN ==
  35. [
  36. sieve [i:0..NS + 1] :: SIEVE
  37. ||
  38. console ::
  39. n : integer;
  40. *[
  41. (i:0..NS + 1) sieve[i]?n -> print (n + "\n")
  42. ]
  43. ]