<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Prime Number in another way</title>
</head>
<body>
<input type="text" name="" id="prime_number">
<button onclick="prime_number()">Click for prime number</button>
<div id="result"></div>
<script>
function is_prime(number){
check = true;
if (number > 1){
var range = parseInt(Math.sqrt(number));
for(var i = 2; i <= range; i++){
if(number % i == 0){
check = false;
break;
}
}
} else{
check = false;
}
return check;
}
function show_prime(given_range){
var prime;
var text;
var count = 1;
if(given_range < 2){
text = "Range should be 2 and afterwords"
} else {
for(var i = 2; i <= given_range; i++){
if(is_prime(i)){
if(prime === undefined){
prime = i;
}else {
prime = prime +", "+ i
count++
}
}
}
text = "Prime Number: <br/>"+ prime + "<br> Number of prime number is <br/>" + count;
}
return text;
}
function prime_number(){
var given_number = parseInt(document.getElementById('prime_number').value);
result = show_prime(given_number);
document.getElementById('result').innerHTML = result;
}
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Prime Number in another way</title>
</head>
<body>
<input type="text" name="" id="prime_number">
<button onclick="prime_number()">Click for prime number</button>
<div id="result"></div>
<script>
function is_prime(number){
check = true;
if (number > 1){
var range = parseInt(Math.sqrt(number));
for(var i = 2; i <= range; i++){
if(number % i == 0){
check = false;
break;
}
}
} else{
check = false;
}
return check;
}
function show_prime(given_range){
var prime;
var text;
var count = 1;
if(given_range < 2){
text = "Range should be 2 and afterwords"
} else {
for(var i = 2; i <= given_range; i++){
if(is_prime(i)){
if(prime === undefined){
prime = i;
}else {
prime = prime +", "+ i
count++
}
}
}
text = "Prime Number: <br/>"+ prime + "<br> Number of prime number is <br/>" + count;
}
return text;
}
function prime_number(){
var given_number = parseInt(document.getElementById('prime_number').value);
result = show_prime(given_number);
document.getElementById('result').innerHTML = result;
}
</script>
</body>
</html>
No comments:
Post a Comment