Created
March 5, 2022 14:22
-
-
Save Hermann-SW/a73321214fe2d446c476afe339c39f91 to your computer and use it in GitHub Desktop.
Test whether "num" is prime slowly (vie regexp match) and report runtime
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// GatewayScript script (IBM DataPower Gateways) that tests passed | |
// number "num" for being a prime slowly, via regexp match + reports runtime | |
// | |
var sm = require('service-metadata'); | |
session.input.readAsJSON(function (error, num) { throwIf(error); | |
var t0 = sm.timeElapsed; | |
var iP = isPrime(num); | |
var t1 = sm.timeElapsed; | |
session.output.write(iP+" ("+(t1-t0)/1000.0+"s)"); | |
}); | |
function isPrime(n) { | |
var re = /^.?$|^(..+?)\1+$/; | |
return !re.test('1'.repeat(n)); | |
} | |
function throwIf(error) { if (error) throw error; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Reports
true (2.564s)
for smallest 17bit prime number 65537, and reports
true (424.544s)
for 100,000th prime number 1299709 (both on IDG hardware appliance).