|
| 1 | +use "collections" |
| 2 | +use "format" |
| 3 | +use "time" |
| 4 | + |
| 5 | +class PrimeSieve |
| 6 | + let _env: Env |
| 7 | + let _dictionary: Map[U64, U64] |
| 8 | + let _sieve_size: U64 |
| 9 | + let _bits: Array[Bool] |
| 10 | + |
| 11 | + new create(env': Env, dictionary': Map[U64, U64], sieve_size': U64) => |
| 12 | + _env = env' |
| 13 | + _dictionary = dictionary' |
| 14 | + |
| 15 | + _sieve_size = sieve_size' |
| 16 | + _bits = Array[Bool].init(false, _sieve_size.usize()) |
| 17 | + |
| 18 | + fun ref run_sieve() => |
| 19 | + var factor : U64 = U64(3) |
| 20 | + let q = _sieve_size.f64().sqrt().u64() |
| 21 | + |
| 22 | + while factor <= q do |
| 23 | + var num = factor |
| 24 | + while num < _sieve_size do |
| 25 | + try |
| 26 | + if not _bits(num.usize())? then |
| 27 | + factor = num |
| 28 | + break |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + num = num + 2 |
| 33 | + end |
| 34 | + |
| 35 | + var num2 = factor * factor |
| 36 | + while num2 < _sieve_size do |
| 37 | + try _bits.update(num2.usize(), true)? end |
| 38 | + num2 = num2 + (factor * 2) |
| 39 | + end |
| 40 | + |
| 41 | + factor = factor + 2 |
| 42 | + end |
| 43 | + |
| 44 | + fun print_results(show_results: Bool, duration: I64, passes: U64) => |
| 45 | + if show_results then |
| 46 | + _env.out.print("2") |
| 47 | + end |
| 48 | + |
| 49 | + var count: U64 = 1 |
| 50 | + for index in Range[U64](3, _sieve_size, 2) do |
| 51 | + try |
| 52 | + if not _bits(index.usize())? then |
| 53 | + if show_results then _env.out.print(Format.int[U64](index)) end |
| 54 | + count = count + 1 |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + if show_results then |
| 60 | + _env.out.print("") |
| 61 | + end |
| 62 | + |
| 63 | + _env.out.print( |
| 64 | + "Passes: " + Format.int[U64](passes) + " " + |
| 65 | + "Time: " + Format.int[I64](duration) + " " + |
| 66 | + "Limit: " + Format.int[U64](_sieve_size) + " " + |
| 67 | + "Count1: " + Format.int[U64](count) + " " + |
| 68 | + "Count2: " + Format.int[U64](count_primes()) + " " + |
| 69 | + "Valid: " + validate_results().string() |
| 70 | + ) |
| 71 | + |
| 72 | + _env.out.print( |
| 73 | + "marghidanu;" + |
| 74 | + Format.int[U64](passes) + ";" + |
| 75 | + Format.int[I64](duration) + ";" + |
| 76 | + "1;algorithm=base,faithful=yes,bits=1" |
| 77 | + ) |
| 78 | + |
| 79 | + fun count_primes() : U64 => |
| 80 | + var count : U64 = 1 |
| 81 | + for index in Range(3, _sieve_size.usize(), 2) do |
| 82 | + try |
| 83 | + if not _bits(index)? then |
| 84 | + count = count + 1 |
| 85 | + end |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + count |
| 90 | + |
| 91 | + fun validate_results() : Bool => |
| 92 | + _dictionary.get_or_else(_sieve_size, 0) == count_primes() |
| 93 | + |
| 94 | + |
| 95 | +actor Main |
| 96 | + new create(env: Env) => |
| 97 | + var passes = U64(0) |
| 98 | + let start_time = Time.now()._1 |
| 99 | + |
| 100 | + let dictionary = Map[U64, U64].create(10) |
| 101 | + dictionary.insert(10, 4) |
| 102 | + dictionary.insert(100, 25) |
| 103 | + dictionary.insert(1_000, 168) |
| 104 | + dictionary.insert(10_000, 1229) |
| 105 | + dictionary.insert(100_000, 9592) |
| 106 | + dictionary.insert(1_000_000, 78498) |
| 107 | + dictionary.insert(10_000_000, 664579) |
| 108 | + dictionary.insert(100_000_000, 5761455) |
| 109 | + dictionary.insert(1_000_000_000, 50847534) |
| 110 | + dictionary.insert(10_000_000_000, 45505251) |
| 111 | + |
| 112 | + while true do |
| 113 | + // NOTE: Pony doesn't allow global variables |
| 114 | + // therefor we need to pass all dependencies to the class instance. |
| 115 | + let sieve = PrimeSieve.create(env, dictionary, U64(1_000_000)) |
| 116 | + sieve.run_sieve() |
| 117 | + passes = passes + 1 |
| 118 | + |
| 119 | + // Unfortunately I can only get the seconds as an integer value. |
| 120 | + // Pony seems fast enough to make the time variance minimal. |
| 121 | + let duration = Time.now()._1 - start_time |
| 122 | + if duration >= 5 then |
| 123 | + sieve.print_results(false, duration, passes) |
| 124 | + break |
| 125 | + end |
| 126 | + end |
0 commit comments