|
| 1 | +Red [ |
| 2 | + Author: "mmcdon20" |
| 3 | + Purpose: "A Red implementation of David Plummer's PrimeSieve application." |
| 4 | +] |
| 5 | + |
| 6 | +PrimeSieve: func [size [integer!] return: [object!]] [ |
| 7 | + make object! [ |
| 8 | + sieveSize: size |
| 9 | + bits: complement make bitset! ((size + 1) >> 1) |
| 10 | + |
| 11 | + resultsDictionary: #( |
| 12 | + 10 4 |
| 13 | + 100 25 |
| 14 | + 1000 168 |
| 15 | + 10000 1229 |
| 16 | + 100000 9592 |
| 17 | + 1000000 78498 |
| 18 | + 10000000 664579 |
| 19 | + 100000000 5761455 |
| 20 | + 1000000000 50847534 |
| 21 | + 10000000000 455052511 |
| 22 | + ) |
| 23 | + |
| 24 | + validateResults: func [return: [logic!]] [ |
| 25 | + resultsDictionary/:sieveSize = countPrimes |
| 26 | + ] |
| 27 | + |
| 28 | + getBit: func [index [integer!] return: [logic!]] [ |
| 29 | + bits/(:index >> 1) |
| 30 | + ] |
| 31 | + |
| 32 | + clearBit: func [index [integer!] return: [logic!]] [ |
| 33 | + bits/(:index >> 1): false |
| 34 | + ] |
| 35 | + |
| 36 | + runSieve: func [return: [none!]] [ |
| 37 | + factor: 3 |
| 38 | + q: round/down (sqrt sieveSize) |
| 39 | + |
| 40 | + while [factor <= q] [ |
| 41 | + num: factor |
| 42 | + while [num < sieveSize] [ |
| 43 | + if getBit num [ |
| 44 | + factor: num |
| 45 | + break |
| 46 | + ] |
| 47 | + num: num + 2 |
| 48 | + ] |
| 49 | + |
| 50 | + num: factor * factor |
| 51 | + while [num < sieveSize] [ |
| 52 | + clearBit num |
| 53 | + num: num + (factor * 2) |
| 54 | + ] |
| 55 | + |
| 56 | + factor: factor + 2 |
| 57 | + ] |
| 58 | + ] |
| 59 | + |
| 60 | + printResults: func [ |
| 61 | + showResults [logic!] |
| 62 | + duration [float!] |
| 63 | + passes [integer!] |
| 64 | + return: [none!] |
| 65 | + ] [ |
| 66 | + if showResults [ |
| 67 | + write-stdout "2, " |
| 68 | + ] |
| 69 | + |
| 70 | + count: if sieveSize >= 2 [ 1 ][ 0 ] |
| 71 | + |
| 72 | + num: 3 |
| 73 | + while [num < sieveSize] [ |
| 74 | + if getBit num [ |
| 75 | + if showResults [ |
| 76 | + write-stdout rejoin [num ", "] |
| 77 | + ] |
| 78 | + count: count + 1 |
| 79 | + ] |
| 80 | + num: num + 2 |
| 81 | + ] |
| 82 | + |
| 83 | + if showResults [ |
| 84 | + write-stdout "^/" |
| 85 | + write-stdout rejoin ["Passes: " passes ", Time: " duration ", "] |
| 86 | + write-stdout rejoin ["Avg: " (duration / passes) ", Limit: " sieveSize ", "] |
| 87 | + write-stdout rejoin ["Count1: " count ", Count2: " countPrimes ", "] |
| 88 | + write-stdout rejoin ["Valid: " validateResults "^/"] |
| 89 | + ] |
| 90 | + |
| 91 | + write-stdout rejoin ["mmcdon20_red;" passes ";" duration ";1;algorithm=base,faithful=yes,bits=1^/"] |
| 92 | + ] |
| 93 | + |
| 94 | + countPrimes: func [return: [integer!]] [ |
| 95 | + count: if sieveSize >= 2 [ 1 ][ 0 ] |
| 96 | + i: 3 |
| 97 | + while [i < sieveSize] [ |
| 98 | + if getBit i [ |
| 99 | + count: count + 1 |
| 100 | + ] |
| 101 | + i: i + 2 |
| 102 | + ] |
| 103 | + count |
| 104 | + ] |
| 105 | + ] |
| 106 | +] |
| 107 | + |
| 108 | +passes: 0 |
| 109 | +start: to float! now/precise/time |
| 110 | + |
| 111 | +forever [ |
| 112 | + sieve: PrimeSieve 1000000 |
| 113 | + do [sieve/runSieve] |
| 114 | + passes: passes + 1 |
| 115 | + stop: to float! now/precise/time |
| 116 | + |
| 117 | + if (stop - start) >= 5.0 [ |
| 118 | + do [sieve/printResults false (stop - start) passes] |
| 119 | + break |
| 120 | + ] |
| 121 | +] |
0 commit comments