<html> <body> <script> var lower= prompt (\"Enter the lowbound number\"); var hi
ID: 3592736 • Letter: #
Question
<html>
<body>
<script>
var lower= prompt ("Enter the lowbound number");
var higher= prompt ("Enter the highbound number");
test(lower, higher)
function test(lower, higher)
{
for(var i=low;i<=high;i++)
{
var list=new Array();
list=factors(i);
document.write(i+ list+"<br>");
}
}
function factors(n)
{
var num_factors = new Array();
for (var i = 2; i <=n; i++){
while (n % i == 0)
n = n / i;
{
num_factors .push(i);
}
} return num_factors ;
}
</script>
</body>
</html>
trying to get this javascript to push out prime factors. it should show
2:2
3:3
4:4,2
5:5
6:2,3
7:7
8:2,2,2
9:3,3
10: 2,5
Explanation / Answer
var lower= prompt ("Enter the lowbound number");
var higher= prompt ("Enter the highbound number");
test(lower, higher)
function test(lower, higher)
{
for(var i=lower;i<=higher;i++)
{
var list=new Array();
list=factor(i);
document.write(i+": "+ list+ "<br>");
}
}
function factor(num) {
num = Math.floor(num);
var root, factors = [], x, sqrt = Math.sqrt, doLoop = 1 < num;
while( doLoop ){
root = sqrt(num);
x = 2;
if (num % x) {
x = 3;
while ((num % x) && ((x += 2) < root));
}
x = (x > root) ? num : x;
factors.push(x);
doLoop = ( x != num );
num /= x;
}
return factors;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.