Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created March 5, 2022 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hermann-SW/a73321214fe2d446c476afe339c39f91 to your computer and use it in GitHub Desktop.
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
// 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; }
@Hermann-SW
Copy link
Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment