I want to list all of the factors and prime factors of two set of numbers.
Here is my code, but my prime factors are not right and the comma at the end of the lines are not needed.
Any help to correct the prime factor? 4 should be 2,2,2 but it comes to be 2.
var l = parseInt(prompt("What is your lowest number"));
var k = parseInt(prompt("What is your highest number"));
var results = '';
//function counts (incremental) the numbers
// display the numbers in bold
// numbers are display on the left of
function genFactors(num) {
var result = "";
var result1 = "";
for (var i = 2; i <= num; ++i) {
if (num % i == 0) {
result += i + ",";
result1 += genPrimeFactors(i);
}
}
results += '<b>' + num + '</b>: ' + result1 + '<br>';
return result;
}
document.writeln("<h1>All Factors</h1>");
document.writeln("<p>The factors of " + l + " and " + k + " are: " + ":
</p>");
for (var num = 1; num <= k; ++num) {
document.writeln("<b>" + num + "</b>" + ":" + genFactors(num) + "<br />");
}
// determine the prime numbers
function isPrime(num) {
for (var i = 2; i < num; i++) {
if (num % i === 0) {
return 0;
} else {
return 1;
}
}
}
//determine the prime factors
function genPrimeFactors(num) {
for (var i = 2; i <= num; i++) {
if (num % i != 0) {
while ((num / 2 == parseInt(num / 2)) && (num > 2)) {
num = num / 2;
return "";
}
if (isPrime(num)) {
return num + ',';
} else {
}
}
}
if (num == 2) {
return 2 + ',';
}
}
/*function
function genPrimeFactors(num) {
var result = "";
for (var i = 2; i <= num; ++i) {
for (var k = 2; k <= i; ++k) {
if (i % k != 0) {
}
result += i + ",";
}
}
return result;
}
*/output
document.writeln("<h1>Prime Factors</h1>");
document.writeln("<p>The prime factors of " + l + " and " + k + " are: " + ":</p>");
document.writeln(results);