
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Smallest prime number just greater than the specified number in JavaScript
We are required to write a JavaScript function that takes in a positive integer as the first and the only argument.
The function should find one such smallest prime number which is just greater than the number specified as argument.
For example −
If the input is −
const num = 18;
Then the output should be:
const output = 19;
Example
Following is the code:
const num = 18; const justGreaterPrime = (num) => { for (let i = num + 1;; i++) { let isPrime = true; for (let d = 2; d * d <= i; d++) { if (i % d === 0) { isPrime = false; break; }; }; if (isPrime) { return i; }; }; }; console.log(justGreaterPrime(num));
Output
Following is the console output −
19
- Related Articles
- Kth prime number greater than N in C++
- How to find the smallest number greater than x in Python?
- How to get the smallest integer greater than or equal to a number in JavaScript?
- JavaScript - Find the smallest n digit number or greater
- Finding nearest prime to a specified number in JavaScript
- Find Smallest Letter Greater Than Target in JavaScript
- What is the HCF of smallest prime number and the smallest composite number?
- Find the difference between the smallest 3 digit prime number and largest 1digit prime number
- JavaScript: Finding nearest prime number greater or equal to sum of digits - JavaScript
- Is the reversed number a prime number in JavaScript
- Getting equal or greater than number from the list of numbers in JavaScript
- Smallest triangular number larger than p
- Returning just greater array in JavaScript
- Finding the smallest fitting number in JavaScript
- JavaScript Recursion finding the smallest number?

Advertisements