Time complexity: O(sqrt(n))
Space complexity: O(1)
const isPrime = num => {
for(let i = 2, s = Math.sqrt(num); i <= s; i++)
if(num % i === 0) return false;
return num > 1;
}
More Related Contents:
- html in a javascript variable gives null error
- what’s wrong with this javascript script.?
- How to find distance between items on array – javascript [closed]
- Get the name of an object’s type
- How can I access a JavaScript object which has spaces in the object’s key?
- jQuery: Get height of hidden element in jQuery
- Is it possible to focus on a using JavaScript focus() function?
- Node.js global variables
- Javascript domready?
- Why can’t I add properties to a string object in javascript?
- New es6 syntax for importing commonjs / amd modules i.e. `import foo = require(‘foo’)`
- How do I normalize the CSS3 transition end events across browsers?
- How do I make a div element editable (like a textarea when I click it)?
- Why not always use the index as the key in a vue.js for loop?
- How to know if a function is async?
- Can’t get value of input type=”file”?
- JQGrid Custom Sorting
- How to globally replace a forward slash in a JavaScript string?
- JavaScript function similar to Python range()
- js. splice returns removed item?
- Difference between Array.length = 0 and Array =[]?
- Saving and Restoring caret position for contentEditable div
- angular 4+ assign @Input for ngComponentOutlet dynamically created component
- React code throwing “TypeError: this.props.data.map is not a function”
- iOS WKWebView not showing javascript alert() dialog
- How to get the file-path of the currently executing javascript code
- What must be wrapped in then() statements in CasperJS? How to determine execution order of sync/async functions?
- How can I use HTTPS in AngularJS?
- Display an arrow head in the middle of D3 force layout link
- How to debug identical strings that do not equal in google app script?