1// prime.js 2 3// Pure Ecmascript version of low level helper 4function primeCheckEcmascript(val, limit) { 5 for (var i = 2; i <= limit; i++) { 6 if ((val % i) == 0) { return false; } 7 } 8 return true; 9} 10 11// Select available helper at load time 12var primeCheckHelper = (this.primeCheckNative || primeCheckEcmascript); 13 14// Check 'val' for primality 15function primeCheck(val) { 16 if (val == 1 || val == 2) { return true; } 17 var limit = Math.ceil(Math.sqrt(val)); 18 while (limit * limit < val) { limit += 1; } 19 return primeCheckHelper(val, limit); 20} 21 22function primeTest() { 23 var res = []; 24 25 print('Have native helper: ' + (primeCheckHelper !== primeCheckEcmascript) + '\n'); 26 for (var i = 2; i <= 1000; i++) { 27 if (primeCheck(i)) { res.push(i); } 28 } 29 print(res.join(' ')); 30} 31 32print = this.send || conn.write 33 34print('HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n'); 35 36primeTest(); 37