

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the k-prime numbers with a specific distance in a range in JavaScript
<h2>K-Prime Numbers</h2><p>A natural number is called k-prime if it has exactly k prime factors, counted with multiplicity.</p><p>Which means even though the only prime factor of 4 is 2 it will be a 2-prime number because −</p><p>4 = 2 * 2 and both 2s will be counted separately taking the count to 2.</p><p>Similarly, 8 is 3-prime because 8 = 2 * 2 * 2 taking the count to 3.</p><h2>Problem</h2><p>We are required to write a JavaScript function that takes in a number k, a distance and a range.</p><p>Our function should return an array of arrays containing k-prime numbers within the range the distance between whom is exactly equal to the distance specified.</p><h2>Example</h2><p>Following is the code −</p><p><a class="demo" href="http://tpcg.io/FhQpyNRm" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">const k = 2; const step = 2; const range = [0, 50]; const kPrimeSteps = (k = 1, step = 1, [start, end]) => { const res = []; let i = start; const findLen = (n = 1) => { let count = 0, i = 2; while (i * i <= n) { while (n % i === 0) { count++; n /= i; } i++; } if (n > 1) count++; return count; } while (i <= end - step) { if ((findLen(i) == k && findLen(i+step) == k)) res.push([i, i+step]); i++; } return res; }; console.log(kPrimeSteps(k, step, range));</pre><h2>Output</h2><p>Following is the console output −</p><pre class="result notranslate">[ [ 4, 6 ], [ 33, 35 ] ]</pre>
- Related Questions & Answers
- Finding two prime numbers with a specific number gap in JavaScript
- Prime numbers in a range - JavaScript
- Prime numbers within a range in JavaScript
- Finding Armstrong numbers in a given range in JavaScript
- Sum of prime numbers between a range - JavaScript
- Finding sequential digit numbers within a range in JavaScript
- Listing all the prime numbers upto a specific number in JavaScript
- Finding sum of all numbers within a range in JavaScript
- Finding the least common multiple of a range of numbers in JavaScript?
- Finding hamming distance in a string in JavaScript
- Finding the count of total upside down numbers in a range using JavaScript
- Finding the count of numbers divisible by a number within a range using JavaScript
- How to generate random whole numbers in JavaScript in a specific range?
- Find numbers with K odd divisors in a given range in C++
- Finding the largest prime factor of a number in JavaScript
Advertisements