|
| 1 | +from Standard.Base import all |
| 2 | + |
| 3 | +import project.Sieve.Sieve_Without_Types |
| 4 | + |
| 5 | +foreign js natural_in_js = """ |
| 6 | + class Gen { |
| 7 | + constructor() { |
| 8 | + this.n = 2 |
| 9 | + } |
| 10 | + |
| 11 | + n() { |
| 12 | + return this.n |
| 13 | + } |
| 14 | + |
| 15 | + next() { |
| 16 | + this.n++ |
| 17 | + return this |
| 18 | + } |
| 19 | + } |
| 20 | + return new Gen() |
| 21 | + |
| 22 | +compute_nth_prime_natural_in_js = Sieve_Without_Types.compute_nth_prime gen=natural_in_js |
| 23 | + |
| 24 | +foreign js filter_in_js = """ |
| 25 | + function Filter(number) { |
| 26 | + this.number = number; |
| 27 | + this.next = null; |
| 28 | + this.last = this; |
| 29 | + } |
| 30 | + Filter.prototype.acceptAndAdd = function(n) { |
| 31 | + var filter = this; |
| 32 | + var sqrt = Math.sqrt(n); |
| 33 | + while (filter != null) { |
| 34 | + if (filter.number) { |
| 35 | + if (n % filter.number === 0) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + if (filter.number > sqrt) { |
| 39 | + break; |
| 40 | + } |
| 41 | + } |
| 42 | + filter = filter.next; |
| 43 | + } |
| 44 | + var newFilter = new Filter(n); |
| 45 | + this.last.next = newFilter; |
| 46 | + this.last = newFilter; |
| 47 | + return this; |
| 48 | + }; |
| 49 | + return new Filter(null); |
| 50 | + |
| 51 | +compute_nth_prime_filter_in_js = Sieve_Without_Types.compute_nth_prime fil=filter_in_js |
| 52 | + |
| 53 | +compute_nth_prime_from_js_file n = |
| 54 | + script = enso_project.data/"sieve.js" |
| 55 | + bench_external_file script.to_text n |
| 56 | + |
| 57 | +foreign js bench_external_file script n = """ |
| 58 | + if (!globalThis.globalPrimes) { |
| 59 | + Polyglot.evalFile("js", script.toString()); |
| 60 | + } |
| 61 | + return globalPrimes(n); |
| 62 | + |
| 63 | +type Primes |
| 64 | + Alg generator filter |
| 65 | + |
| 66 | +Primes.next self = case self of |
| 67 | + Primes.Alg g f -> |
| 68 | + filter = f.acceptAndAdd g.n |
| 69 | + new_primes = Primes.Alg g.next filter |
| 70 | + if Meta.is_same_object filter f then @Tail_Call new_primes.next else new_primes |
| 71 | + |
| 72 | +Primes.last_prime self = case self of |
| 73 | + Primes.Alg g _ -> g.n - 1 |
| 74 | + |
| 75 | +foreign js all_in_java_script n = """ |
| 76 | + function Natural() { |
| 77 | + this.x = 2; |
| 78 | + }; |
| 79 | + Natural.prototype.next = function() { |
| 80 | + return this.x++; |
| 81 | + }; |
| 82 | + |
| 83 | + function Filter(number) { |
| 84 | + this.number = number; |
| 85 | + this.next = null; |
| 86 | + this.last = this; |
| 87 | + } |
| 88 | + Filter.prototype.acceptAndAdd = function(n) { |
| 89 | + var filter = this; |
| 90 | + var sqrt = Math.sqrt(n); |
| 91 | + for (;;) { |
| 92 | + if (n % filter.number === 0) { |
| 93 | + return false; |
| 94 | + } |
| 95 | + if (filter.number > sqrt) { |
| 96 | + break; |
| 97 | + } |
| 98 | + filter = filter.next; |
| 99 | + } |
| 100 | + var newFilter = new Filter(n); |
| 101 | + this.last.next = newFilter; |
| 102 | + this.last = newFilter; |
| 103 | + return true; |
| 104 | + }; |
| 105 | + |
| 106 | + function Primes(natural) { |
| 107 | + this.natural = natural; |
| 108 | + this.filter = null; |
| 109 | + } |
| 110 | + Primes.prototype.next = function() { |
| 111 | + for (;;) { |
| 112 | + var n = this.natural.next(); |
| 113 | + if (this.filter === null) { |
| 114 | + this.filter = new Filter(n); |
| 115 | + return n; |
| 116 | + } |
| 117 | + if (this.filter.acceptAndAdd(n)) { |
| 118 | + return n; |
| 119 | + } |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + var res = -1; |
| 124 | + let primes = new Primes(new Natural()); |
| 125 | + for (let i = 0; i < n; i++) { |
| 126 | + res = primes.next(); |
| 127 | + } |
| 128 | + return res; |
| 129 | + |
0 commit comments