Monday, August 10, 2015

How to find out prime number in javaScript

Html part:
 <!doctype html>  
 <html lang="en">  
 <head>  
   <meta charset="UTF-8">  
   <title>Document</title>  
 </head>  
 <body>  
 <script src="script.js"></script>  
 </body>  
 </html>  

 JavaScript Part: save as script.js
function is_prime ( number ) {  
   var check = true;  
   if(number > 1 ) {  
     if ( number > 2 ) {  
       var range = parseInt(number / 2) + 2;  
       for ( var i = 2; i < range; i++){  
        if( (number % i) == 0 ){  
          check = false;  
          break;  
         }  
       }  
     }  
   } else {  
     check = false;  
   }  
   return check;  
 }  
 number = 100;  
 for (var i = 1; i <= number; i++ ) {  
   if(is_prime(i)) {  
     console.log(i);  
   }  
 }  

No comments:

Post a Comment

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }