sieve.carbon 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // TODO: Copied from core/prelude/types/i32.carbon.
  6. // Because we don't deduplicate interfaces, the implementations in that file are
  7. // treated as implementing a different interface from the one we import above.
  8. // Remove the following, once we deduplicate interfaces.
  9. impl i32 as Core.Add {
  10. fn Op[self: Self](other: Self) -> Self = "int.sadd";
  11. }
  12. impl i32 as Core.AddAssign {
  13. fn Op[addr self: Self*](other: Self) {
  14. *self = *self + other;
  15. }
  16. }
  17. impl i32 as Core.Inc {
  18. fn Op[addr self: Self*]() {
  19. *self += 1;
  20. }
  21. }
  22. impl i32 as Core.Div {
  23. fn Op[self: Self](other: Self) -> Self = "int.sdiv";
  24. }
  25. impl i32 as Core.DivAssign {
  26. fn Op[addr self: Self*](other: Self) {
  27. *self = *self / other;
  28. }
  29. }
  30. impl i32 as Core.Eq {
  31. fn Equal[self: Self](other: Self) -> bool = "int.eq";
  32. fn NotEqual[self: Self](other: Self) -> bool = "int.neq";
  33. }
  34. impl i32 as Core.Mod {
  35. fn Op[self: Self](other: Self) -> Self = "int.smod";
  36. }
  37. impl i32 as Core.ModAssign {
  38. fn Op[addr self: Self*](other: Self) {
  39. *self = *self % other;
  40. }
  41. }
  42. impl i32 as Core.Mul {
  43. fn Op[self: Self](other: Self) -> Self = "int.smul";
  44. }
  45. impl i32 as Core.MulAssign {
  46. fn Op[addr self: Self*](other: Self) {
  47. *self = *self * other;
  48. }
  49. }
  50. impl i32 as Core.Negate {
  51. fn Op[self: Self]() -> Self = "int.snegate";
  52. }
  53. impl i32 as Core.Ordered {
  54. // TODO: fn Compare
  55. fn Less[self: Self](other: Self) -> bool = "int.less";
  56. fn LessOrEquivalent[self: Self](other: Self) -> bool = "int.less_eq";
  57. fn Greater[self: Self](other: Self) -> bool = "int.greater";
  58. fn GreaterOrEquivalent[self: Self](other: Self) -> bool = "int.greater_eq";
  59. }
  60. impl i32 as Core.Sub {
  61. fn Op[self: Self](other: Self) -> Self = "int.ssub";
  62. }
  63. impl i32 as Core.SubAssign {
  64. fn Op[addr self: Self*](other: Self) {
  65. *self = *self - other;
  66. }
  67. }
  68. impl i32 as Core.Dec {
  69. fn Op[addr self: Self*]() {
  70. *self -= 1;
  71. }
  72. }
  73. // ---
  74. class Sieve {
  75. fn Make() -> Sieve {
  76. returned var s: Sieve;
  77. // TODO: `for` loop.
  78. var n: i32 = 0;
  79. while (n < 1000) {
  80. s.is_prime[n] = true;
  81. ++n;
  82. }
  83. return var;
  84. }
  85. fn MarkMultiplesNotPrime[addr self: Self*](p: i32) {
  86. var n: i32 = 2 * p;
  87. while (n < 1000) {
  88. self->is_prime[n] = false;
  89. n += p;
  90. }
  91. }
  92. var is_prime: [bool; 1000];
  93. }
  94. fn Run() -> i32 {
  95. var s: Sieve = Sieve.Make();
  96. var number_of_primes: i32 = 0;
  97. var n: i32 = 2;
  98. while (n < 1000) {
  99. if (s.is_prime[n]) {
  100. ++number_of_primes;
  101. s.MarkMultiplesNotPrime(n);
  102. }
  103. ++n;
  104. }
  105. return number_of_primes;
  106. }