using html 9.19 An integer is said to be prime if it’s greater than 1 and divisi
ID: 3606134 • Letter: U
Question
using html
9.19 An integer is said to be prime if it’s greater than 1 and divisible only by 1 and itself. For
example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not.
a) Write a function that determines whether a number is prime.
b) Use this function in a script that determines and prints all the prime numbers between
1 and 10,000. How many of these 10,000 numbers do you really have to test before
being sure that you have found all the primes? Display the results in a <textarea>.
c) Initially, you might think that n/2 is the upper limit for which you must test to see
whether a number is prime, but you need go only as high as the square root of n. Why?
Rewrite the program using the Math.sqrt method to calculate the square root, and run
it both ways. Estimate the performance improvement.
Explanation / Answer
solution of a and b part--
<!DOCTYPE html>
<html>
<head>
<title>JavaScript to find prime number or not!</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
function findPrime(){
//get the input value
var num = document.getElementById('num').value;
var c = 0;
//loop till i equals to $num
for (i = 1; i <= num; i++) {
//check if the $num is divisible by itself and 1
// % modules will give the reminder value, so if the reminder is 0 then it is divisible
if (num % i == 0) {
//increment the value of c
c = c + 1;
}
}
//if the value of c is 2 then it is a prime number
//because a prime number should be exactly divisible by 2 times only (itself and 1)
if (c == 2) {
document.getElementById('result').innerHTML = num + ' is a Prime number';
}else{
document.getElementById('result').innerHTML = num + ' is NOT a Prime number';
}
}
function printPrime() {
var x = document.createElement("TEXTAREA");
for (i=2; i<=10000; i++) {
if (i%10000==0){document.write("<BR>")};
prime=true;
for (j=2; j<i; j++) {
if (i%j==0){prime=false;break}
};
if (prime)
var t = document.createTextNode(i);
x.appendChild(t);
document.body.appendChild(x);
}
}
</script>
</head>
<body>
<h2>JavaScript to find Prime number or not!</h2>
<form method="post">
Enter a number: <input type="number" id="num" name="num" min="0" /><input type="button" value="Find Prime Number" name="find" />
<input type="button" value="Prime Number Range" />
<div id="result"></div>
</form>
</body>
</html>
Solution of C part--
<!DOCTYPE html>
<html>
<title>Check Prime Number</title>
<head>
<script type="text/javascript">
function isNumber(evt){
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
function isPrime(){
var resultMsg=document.getElementById("resultMsg");
var numElement=document.getElementById("num");
var num=0;
if(numElement!=null){
if(isNaN(numElement.value)){
resultMsg.innerHTML="Please enter integer";
}else{
num=parseInt(numElement.value);
var isPrime=chkPrime(num);
if(isPrime){
resultMsg.innerHTML="Number "+num+" is Prime number.";
}else{
resultMsg.innerHTML="Number "+num+" is not Prime number.";
}
}
}
}
function chkPrime(number){
var isPrime=true;
if(number<=1){
isPrime=false;
}else if(number<=3){
isPrime=true;
}else{
var num_sqrt =Math.sqrt(number);
for (var i=2; i <= num_sqrt; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
return isPrime;
}
</script>
</head>
<body>
<h3>Javascript Program to check whether number is Prime number or not</h3>
<span>Enter Number:</span>
<input type="text" name="num" id="num"/>
<button>Check Prime</button>
<div id="resultMsg"></div>
</body>
</html>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.