Skip to content

Instantly share code, notes, and snippets.

@mykeels
Created April 4, 2018 09:02
Show Gist options
  • Save mykeels/8396f87d42a809697d08f6b6c9cd8db0 to your computer and use it in GitHub Desktop.
Save mykeels/8396f87d42a809697d08f6b6c9cd8db0 to your computer and use it in GitHub Desktop.
const express = require('express')
const isPrime = require('./is-prime')
const app = express()
app.get('/', (req, res) => {
const primes = []
const max = Number(req.query.max) || 1000
for (let i = 1; i <= max; i++) {
if (isPrime(i)) primes.push(i)
}
res.json(primes)
})
app.listen(process.env.PORT || 3030)
console.log('app is running!')
module.exports = app
module.exports = (num) => {
if ([2, 3].indexOf(num) >= 0) return true;
else if ([2, 3].some(n => num % n == 0)) return false;
else {
let i = 5, w = 2;
while ((i * i) <= num) {
if (num % i == 0) return false
i += w
w = 6 - w
}
}
return true
}
{
"name": "prime-cluster",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index"
},
"author": "",
"license": "MIT",
"dependencies": {
"express": "^4.16.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment