all stats

SliceOfArdath's stats

guessed the most

namecorrect guessesgames togetherratio

were guessed the most by

namecorrect guessesgames togetherratio

entries

round #17

submitted at
4 likes

guesses
comments 0

post a comment


entry.rs ASCII text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
static mut RAIL: Vec<usize> = Vec::new();
static mut TOP: usize = 0;

fn raw_brute_prime(p: i32) -> bool {
    for i in 2..f64::sqrt(p as f64).round() as i32 + 1 {
        //TST[4] += 1;
        if p % i == 0 {
            return false;
        }
    }
    return true;
}
fn brute_prime(p: usize, lock: usize) -> bool {
    for i in 2..lock {
        if p % i == 0 {
            return false;
        }
    }
    return true;
}

unsafe fn primeband(n: usize, lock: usize) -> bool {
    let mut prime_count: usize = 0;
    let mut e: usize; let mut j: usize;
    let mut v: Vec<usize> = (2..n+1).collect();
    //Lock is TOP bound, sqrt n as usize + 1
    //Pass through the band
    for i in 0..lock { //Early exit with square root.
        //e not prime = there exits a and b whith axb = e, then a or b are <= sqrt(e).
        e = v[i];
        if e != 0 {
            prime_count += 1;
            j = e + i;
            while j < n - 1 { //bounce from value to value.
                v[j] = 0;
                j += e;
            }
        }
    }
    //railh.reserve(prime_count);
    RAIL.reserve(prime_count * 2); //By all means not the right amount.
    for i in 0..n-1 { //All != 0 are primes. 
        e = v[i]; 
        if e != 0 {
            RAIL.push(e);
        }
    }
    TOP = n;
    return RAIL.last().unwrap() == &n;
    //return railh.contains(&n);
}
unsafe fn extendedprimeband(n: usize, lock: usize) -> bool {
    //Primeband, but takes in account the already existing prime list.
    //let bottom;
    match RAIL.last() { //Nothing to process?
        Some(i) => {if i >= &n { return false; }}, // else { bottom = *i; }
        None => return primeband(n, lock),
    }
    let mut prime_count: usize = 0;
    let mut e: usize; let mut j: usize;
    let mut v: Vec<usize> = (TOP+1..n+1).collect(); //fill the band. values from TOP+1 to n incuded.
    //let lock = f64::sqrt(n as f64).round() as usize + 1; //TOP bound for calculations; cf primeband.
    //First pass: the original band.
    let mut g = 0; e = RAIL[0];
    while g < RAIL.len() - 1 && e <= lock {
        e = RAIL[g];
        j = e - (TOP + 1) % e; //sets staring point.
        //option 2 for setting starting point: (should by all means be slower)
        /*j = 0;
        while j < v.len() && (v[j] == 0 || v[j] % e != 0) {
            j += 1;
        }*/
        while j < v.len() {
            v[j] = 0;
            j += e;
        }
        g += 1;
    }
    //Second pass: extending the band using an adaptation of erathosthene's method
    //Not exacty sure of how that part works, not going to question it.
    //g = 0; while g < v.len() && v[g] <= lock //the other option, slower apparently. Again not sure why or how but hey.
    for i in 0..f64::sqrt(v.len() as f64).round() as usize + 1 {
        e = v[i];
        if e != 0 {
            prime_count += 1;
            j = e + i;
            while j < v.len() {
                v[j] = 0;
                j += e;
            }
        }
    }
    RAIL.reserve(prime_count * 2); //Not enough, but a start. 
    for i in 0..v.len() { //All != 0 are primes. 
        e = v[i]; 
        if e != 0 { 
            RAIL.push(e);
        }
    }
    TOP = n;
    return RAIL.last().unwrap() == &n;
}

unsafe fn resolve_band(p: usize, lock: usize) -> bool { 
    //where TOP < p <= TOP * TOP, RAIL not empty.
    let mut i: usize = 0; let mut e = RAIL[0];
    while i < RAIL.len() && e <= lock {
        e = RAIL[i];
        if p == RAIL[i] { return true; } //doubles as a fastcheck.
        if p % e == 0 {
            return false;
        }
        i += 1;
    }
    return true;
}
/* Not a good tradeoff.
unsafe fn fastcheck(p: usize) -> bool {
    for i in 0..RAIL.len() {
        if p == RAIL[i] { return true; }
        if p < RAIL[i] { return false; }
    }
    return false;
}*/


unsafe fn resolve(p: usize) -> bool { //Damn you and your I32 >:(
    //assumed that p > 1.
    let lock = f64::sqrt(p as f64) as usize;
    if TOP == 0 {
        return primeband(p, lock);
    } else if TOP >= lock { 
        return resolve_band(p, lock);
    } else if TOP <= p * p {
        return extendedprimeband(lock * 2, f64::sqrt(lock as f64) as usize);
    } else { //Outside of the scope of this project.
        return brute_prime(p, lock);
    }
}
fn entry(p: i32) -> bool {
    if p < 2 { return false; }
    unsafe {
        return resolve(p as usize);
    }
}
main_test.rs ASCII text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
use std::time;
extern crate rand;
use rand::Rng;
use std::env;

static mut RAIL: Vec<usize> = Vec::new();
static mut TOP: usize = 0;

fn raw_brute_prime(p: i32) -> bool {
    for i in 2..f64::sqrt(p as f64).round() as i32 + 1 {
        //TST[4] += 1;
        if p % i == 0 {
            return false;
        }
    }
    return true;
}
fn brute_prime(p: usize, lock: usize) -> bool {
    for i in 2..lock {
        if p % i == 0 {
            return false;
        }
    }
    return true;
}

unsafe fn primeband(n: usize, lock: usize) -> bool {
    let mut prime_count: usize = 0;
    let mut e: usize; let mut j: usize;
    let mut v: Vec<usize> = (2..n+1).collect();
    //Lock is TOP bound, sqrt n as usize + 1
    //Pass through the band
    for i in 0..lock { //Early exit with square root.
        //e not prime = there exits a and b whith axb = e, then a or b are <= sqrt(e).
        e = v[i];
        if e != 0 {
            prime_count += 1;
            j = e + i;
            while j < n - 1 { //bounce from value to value.
                v[j] = 0;
                j += e;
            }
        }
    }
    //railh.reserve(prime_count);
    RAIL.reserve(prime_count * 2); //By all means not the right amount.
    for i in 0..n-1 { //All != 0 are primes. 
        e = v[i]; 
        if e != 0 {
            RAIL.push(e);
        }
    }
    TOP = n;
    return RAIL.last().unwrap() == &n;
    //return railh.contains(&n);
}
unsafe fn extendedprimeband(n: usize, lock: usize) -> bool {
    //Primeband, but takes in account the already existing prime list.
    //let bottom;
    match RAIL.last() { //Nothing to process?
        Some(i) => {if i >= &n { return false; }}, // else { bottom = *i; }
        None => return primeband(n, lock),
    }
    let mut prime_count: usize = 0;
    let mut e: usize; let mut j: usize;
    let mut v: Vec<usize> = (TOP+1..n+1).collect(); //fill the band. values from TOP+1 to n incuded.
    //let lock = f64::sqrt(n as f64).round() as usize + 1; //TOP bound for calculations; cf primeband.
    //First pass: the original band.
    let mut g = 0; e = RAIL[0];
    while g < RAIL.len() - 1 && e <= lock {
        e = RAIL[g];
        j = e - (TOP + 1) % e; //sets staring point.
        //option 2 for setting starting point: (should by all means be slower)
        /*j = 0;
        while j < v.len() && (v[j] == 0 || v[j] % e != 0) {
            j += 1;
        }*/
        while j < v.len() {
            v[j] = 0;
            j += e;
        }
        g += 1;
    }
    //Second pass: extending the band using an adaptation of erathosthene's method
    //Not exacty sure of how that part works, not going to question it.
    //g = 0; while g < v.len() && v[g] <= lock //the other option, slower apparently. Again not sure why or how but hey.
    for i in 0..f64::sqrt(v.len() as f64).round() as usize + 1 {
        e = v[i];
        if e != 0 {
            prime_count += 1;
            j = e + i;
            while j < v.len() {
                v[j] = 0;
                j += e;
            }
        }
    }
    RAIL.reserve(prime_count * 2); //Not enough, but a start. 
    for i in 0..v.len() { //All != 0 are primes. 
        e = v[i]; 
        if e != 0 { 
            RAIL.push(e);
        }
    }
    TOP = n;
    return RAIL.last().unwrap() == &n;
}

unsafe fn resolve_band(p: usize, lock: usize) -> bool { 
    //where TOP < p <= TOP * TOP, RAIL not empty.
    let mut i: usize = 0; let mut e = RAIL[0];
    while i < RAIL.len() && e <= lock {
        e = RAIL[i];
        if p == RAIL[i] { return true; } //doubles as a fastcheck.
        if p % e == 0 {
            return false;
        }
        i += 1;
    }
    return true;
}
/* Not a good tradeoff.
unsafe fn fastcheck(p: usize) -> bool {
    for i in 0..RAIL.len() {
        if p == RAIL[i] { return true; }
        if p < RAIL[i] { return false; }
    }
    return false;
}*/


unsafe fn resolve(p: usize) -> bool { //Damn you and your I32 >:(
    //assumed that p > 1.
    let lock = f64::sqrt(p as f64) as usize;
    if TOP == 0 {
        return primeband(p, lock);
    } else if TOP >= lock { 
        return resolve_band(p, lock);
    } else if TOP <= p * p {
        return extendedprimeband(lock * 2, f64::sqrt(lock as f64) as usize);
    } else { //Outside of the scope of this project.
        return brute_prime(p, lock);
    }
}
fn entry(p: i32) -> bool {
    if p < 2 { return false; }
    unsafe {
        return resolve(p as usize);
    }
}

fn test_compare(r: i32) {
    let mut cha: Vec<i32> = Vec::new();
    let mut chb: Vec<i32> = Vec::new();
    /*for i in 2..r {
        //assert_eq!(entry(i), raw_brute_prime(i));
        if entry(i) != raw_brute_prime(i) {
            println!("ono {}", i);
        }
    }*/
    let mut t = time::Instant::now();
    for i in 2..r {
        if entry(i) {
            cha.push(i);
        }
    }
    let da = time::Instant::now() - t;
    t = time::Instant::now();
    for i in 2..r {
        if raw_brute_prime(i) {
            chb.push(i);
        }
    }
    let db = time::Instant::now() - t;
    for i in 0..cha.len() {
        if cha[i] != chb[i] {
            println!("{} {}", cha[i], chb[i]);
        }
    }
    println!("Brute time: {:?} Optimised time: {:?}", db, da);
    if db<da {
        println!("Optimised is slower than Brute.");
        //println!("Optimised is {:?} times slower than Brute.", da / db);
    }
}
fn test_self(r: i32) {
    let t = time::Instant::now();
    for i in 2..r {
        entry(i);
    }
    let da = time::Instant::now() - t;
    println!("Optimised time: {:?}", da);
}
fn test_actual_compare(r: i32, mx: i32) {
    let mut cha: Vec<i32> = Vec::new();
    let mut chb: Vec<i32> = Vec::new();
    let mut rcx: Vec<i32> = Vec::with_capacity(r as usize);
    let mut rng = rand::thread_rng();
    for _ in 2..r { //That way the set is the same
        rcx.push(rng.gen_range(2, mx));
    }
    let mut t = time::Instant::now();
    for i in &rcx {
        if entry(*i) {
            cha.push(*i);
        }
    }
    let da = time::Instant::now() - t;
    t = time::Instant::now();
    for i in &rcx {
        if raw_brute_prime(*i) {
            chb.push(*i);
        }
    }
    let db = time::Instant::now() - t;
    println!("Brute time: {:?} Optimised time: {:?}", db, da);
    if db<da {
        println!("Optimised is slower than Brute.");
    }
}
fn test_actual_self(r: i32, mx: i32) {
    let mut rng = rand::thread_rng();
    let mut rcx: Vec<i32> = Vec::with_capacity(r as usize);
    for _ in 2..r { //Generate before timing.
        rcx.push(rng.gen_range(2, mx));
    }
    let t = time::Instant::now();
    for i in rcx {
        entry(i);
    }
    let da = time::Instant::now() - t;
    println!("Optimised time: {:?}", da);
}
fn test_compare_r(r: i32) {
    let mut cha: Vec<i32> = Vec::new();
    let mut chb: Vec<i32> = Vec::new();
    let mut t = time::Instant::now();
    for i in 2..r {
        if entry(r-i+2) {
            cha.push(r-i+2);
        }
    }
    let da = time::Instant::now() - t;
    t = time::Instant::now();
    for i in 2..r {
        if raw_brute_prime(r-i+2) {
            chb.push(r-i+2);
        }
    }
    let db = time::Instant::now() - t;
    for i in 0..cha.len() {
        if cha[i] != chb[i] {
            println!("{} {}", cha[i], chb[i]);
        }
    }
    println!("Brute time: {:?} Optimised time: {:?}", db, da);
}
fn test_self_r(r: i32) {
    //let mut cha: Vec<i32> = Vec::new();
    let t = time::Instant::now();
    for i in 2..r {
        entry(r-i+2);
    }
    let da = time::Instant::now() - t;
    println!("Optimised time: {:?}", da);
}

fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() < 3 {
        println!("This program needs test_mode and range arguments. (int)");
        return;
    }
    let mode: i32 = args[1].parse().unwrap();
    let r: i32 = args[2].parse().unwrap();
    if mode == 0 {
        test_compare(r); 
    } else if mode == 1 {
        test_self(r); //Should run in under 500 seconds.
    } else if mode == 2 {
        if args.len() < 4 {
            println!("This program needs a max_value argument. (int)");
            return;
        }
        test_actual_compare(r, args[3].parse().unwrap());
    } else if mode == 3 {
        if args.len() < 4 {
            println!("This program needs a max_value argument. (int)");
            return;
        }
        test_actual_self(r, args[3].parse().unwrap());
    } else if mode == 4 {
        test_compare_r(r); 
    } else if mode == 5 {
        test_self_r(r); 
    } 
}