Test if number is prime or not, and find numbers divisors
Some integers can be divided evenly into several parts, but some are not. Number N that cannot be divided into any number of parts except for N itself, is called a prime number. Fundamental theorem of arithmetic states that every composite (non-prime) number N can be represented as a product of several lesser prime numbers pi, and the set of these prime numbers is uniquely linked with N. Script on this webpage searches any prime divisors for the given N (a process called factorization) and therefore can be used to determine whether some integer is prime or not.
edit
 
Test for primality

  • sourceShow/hide source
    
            function findDivisors(n, includeSelf){
                includeSelf = includeSelf || false;
                if (n == 1) {
                    return [];
                }
                var maxDiv = Math.ceil(Math.sqrt(n));
                var divisors = [];
                for (var p = 2; p <= maxDiv; p++) {
                    if (n % p == 0) {
                        q = n / p;
                        divisors.push(p);
                        divisors = divisors.concat(findDivisors(q, true));
                        if (divisors.reduce(function(a, b) { return a*b }) == n) {
                            return divisors;
                        }
                    }
                }
                if (includeSelf) {
                    divisors.push(n);
                }
                return divisors;
            }
    
            $(document).ready(function(){
    
               $(".btn-test").click(function(){
                   var num = parseInt($("#num").val(), 10);
                   var box = $("#result");
                   if (isNaN(num)) {
                       box.html("Invalid number");
                   } else if (num < 1) {
                       box.html("Only positive number can be checked!");
                   } else {
                       box.html("Checking in progress...");
                       var result = findDivisors(num, false);
                       if (result.length > 0) {
                           box.html(num + " <strong>is NOT prime</strong>! Prime divisors: <strong>" + result.join(" x ") + " = " + num + "</strong>");
                        } else {
                           box.html(num + " <strong>is prime</strong>! No divisors except 1 and " + num);
                        }
                   }
                   box.trigger("autoresize");
               });
    
             });