sieve.carbon 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. // Compute and return the number of primes less than 1000.
  5. import Core library "prelude/operators/arithmetic";
  6. import Core library "prelude/operators/comparison";
  7. // TODO: Copied from core/prelude/types/i32.carbon.
  8. // Remove the following, once we do cross-file impl lookup.
  9. // import Core library "prelude/types/i32";
  10. impl i32 as Core.Add {
  11. fn Op[self: Self](other: Self) -> Self = "int.add";
  12. }
  13. impl i32 as Core.AddAssign {
  14. fn Op[addr self: Self*](other: Self) {
  15. *self = *self + other;
  16. }
  17. }
  18. impl i32 as Core.Inc {
  19. fn Op[addr self: Self*]() {
  20. *self += 1;
  21. }
  22. }
  23. impl i32 as Core.Div {
  24. fn Op[self: Self](other: Self) -> Self = "int.div";
  25. }
  26. impl i32 as Core.DivAssign {
  27. fn Op[addr self: Self*](other: Self) {
  28. *self = *self / other;
  29. }
  30. }
  31. impl i32 as Core.Eq {
  32. fn Equal[self: Self](other: Self) -> bool = "int.eq";
  33. fn NotEqual[self: Self](other: Self) -> bool = "int.neq";
  34. }
  35. impl i32 as Core.Mod {
  36. fn Op[self: Self](other: Self) -> Self = "int.mod";
  37. }
  38. impl i32 as Core.ModAssign {
  39. fn Op[addr self: Self*](other: Self) {
  40. *self = *self % other;
  41. }
  42. }
  43. impl i32 as Core.Mul {
  44. fn Op[self: Self](other: Self) -> Self = "int.mul";
  45. }
  46. impl i32 as Core.MulAssign {
  47. fn Op[addr self: Self*](other: Self) {
  48. *self = *self * other;
  49. }
  50. }
  51. impl i32 as Core.Negate {
  52. fn Op[self: Self]() -> Self = "int.negate";
  53. }
  54. impl i32 as Core.Ordered {
  55. // TODO: fn Compare
  56. fn Less[self: Self](other: Self) -> bool = "int.less";
  57. fn LessOrEquivalent[self: Self](other: Self) -> bool = "int.less_eq";
  58. fn Greater[self: Self](other: Self) -> bool = "int.greater";
  59. fn GreaterOrEquivalent[self: Self](other: Self) -> bool = "int.greater_eq";
  60. }
  61. impl i32 as Core.Sub {
  62. fn Op[self: Self](other: Self) -> Self = "int.sub";
  63. }
  64. impl i32 as Core.SubAssign {
  65. fn Op[addr self: Self*](other: Self) {
  66. *self = *self - other;
  67. }
  68. }
  69. impl i32 as Core.Dec {
  70. fn Op[addr self: Self*]() {
  71. *self -= 1;
  72. }
  73. }
  74. // ---
  75. fn Run() -> i32 {
  76. var is_prime: [bool; 1000];
  77. // TODO: `for` loop.
  78. var n: i32 = 0;
  79. while (n < 1000) {
  80. is_prime[n] = true;
  81. ++n;
  82. }
  83. var number_of_primes: i32 = 0;
  84. n = 2;
  85. while (n < 1000) {
  86. if (is_prime[n]) {
  87. ++number_of_primes;
  88. var k: i32 = 2 * n;
  89. while (k < 1000) {
  90. is_prime[k] = false;
  91. k += n;
  92. }
  93. }
  94. ++n;
  95. }
  96. return number_of_primes;
  97. }