Function to find prime numbers

serkan sendur 0 Tallied Votes 158 Views Share

Accepts number as input, writes the prime numbers up to that number to the screen.

function DisplayPrimeNumbers(number)
    {
        var this_number,divisor,not_prime;
        this_number = 3;
        while(this_number < number)
        {
            divisor = parseInt( this_number / 2);
            not_prime = 0;
            while(divisor > 1)
            {
                if(this_number % divisor == 0)
                {
                   not_prime = 1;
                   divisor = 0;
                }
                else
                {
                    divisor = divisor - 1;
                }
            }
            if(not_prime == 0)
            {
                document.writeln(this_number + " is a prime number </br>");
            }
            this_number = this_number + 1;
        }
       
    }