-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathprimeFactors.js
35 lines (30 loc) · 1.13 KB
/
primeFactors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* Given a positive integer, return an array consisting of all its prime
* factors sorted from smallest to largest. Each prime appears in the
* returned array as many times as it divides the given integer.
*
* @example
* primeFactors(2); // => [2]
* primeFactors(2 * 2); // => [2, 2]
* primeFactors(2 * 2 * 3); // => [2, 2, 3]
* primeFactors(2 * 2 * 3 * 17 * 197); // => [2, 2, 3, 17, 197]
* primeFactors(917329); // => [7, 7, 97, 193]
*
* @param {number} num - A positive integer
* @returns {number[]} An array of all the prime factors of the given integer
*/
function primeFactors(num) {
/*
Your code goes here.
Work out one version that works and don't worry about performance.
If you're having trouble working it out in code, step out of JS-land
and use pen/paper, index cards, etc. — anything that helps you think
about it without getting stuck in JavaScript syntax.
*/
}
if (require.main === module) {
console.log('Running sanity checks for primeFactors:');
// Add your own sanity checks here.
// How else will you be sure your code does what you think it does?
}
module.exports = primeFactors;