Question

I'm just trying to find prime numbers of an entered range of numbers. I have no clue how to calculate finding primes. I need to add them to an array and output the array after. I put a placeholder for the calculation... I just can't seem to figure out how find the primes.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">

<html>

    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />

        <title>LeapYears</title>

        <script type="text/javascript">
        /* <![CDATA[ */

        function calcPrimeNumber(){

                var beginNum = document.numbers.firstNum.value;
                var endNum = document.numbers.secondNum.value;
                var primeNumbs = new Array();


                var ctr = 0;
                while (beginNum <= endNum){ //throwaway
                    if ((beginNum % beginNum == 0) && (beginNum % 1 == 0)){
                        primeNumbs[ctr] = beginNum;
                        ++ctr;
                    }

                    ++beginNum;
                }

                if (primeNumbs == 0){
                    window.alert("There were no leap years within the range.");
                }

                else {
                    outputPrimeNums(primeNumbs);
                }

        }

        function outputPrimeNums(primes){
            document.write("<h2>Prime Numbers</h2>");
            for (i=0;i<primes.length;i++){
                    document.write(primes[i] + "<br/>");
                }

        }


        /* ]]> */
        </script>


    </head>


    <body>
        <form name="numbers">

            Beginning Number: <input type="text" name="firstNum" /> End Number: <input type="text" name="secondNum" /> 
            <input type="button" value="Find Prime Numbers" onclick="calcPrimeNumber()" />

        </form>

    </body>


</html>
Was it helpful?

Solution

try this full page of prime no example

<html>

    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />

        <title>LeapYears</title>

        <script type="text/javascript">
        /* <![CDATA[ */

        function calcPrimeNumber(){

                var beginNum = parseInt(document.numbers.firstNum.value);
                var endNum = parseInt(document.numbers.secondNum.value);
                var primeNumbs = new Array();


                var ctr = beginNum;
                while(ctr<=endNum)
                {
                    if(isPrime(ctr)==true)
                    {
                        primeNumbs[primeNumbs.length] = ctr;
                    }
                    ctr = ctr+1;

                }

                if (primeNumbs.length == 0){
                   document.getElementById('output_content').innerHTML = "There were no prime no within the range.";
                }

                else {
                    outputPrimeNums(primeNumbs);
                }

        }

        function isPrime(num)
        {
            var flag = true;
            for(var i=2; i<=Math.ceil(num/2); i++)
            {
                if((num%i)==0)
                {
                    flag = false;
                    break;
                }
            }
            return flag;    
        }

        function outputPrimeNums(primes){
            var html = "<h2>Prime Numbers</h2>";
            for (i=0;i<primes.length;i++){
                    html += primes[i] + "<br/>";
                }
            document.getElementById('output_content').innerHTML = html;
        }


        /* ]]> */
        </script>


    </head>


    <body>
        <form name="numbers">

            Beginning Number: <input type="text" name="firstNum" /> End Number: <input type="text" name="secondNum" /> 
            <input type="button" value="Find Prime Numbers" onclick="calcPrimeNumber()" />

        </form>
        <div id="output_content">
        </div>
    </body>


</html>

OTHER TIPS

You should use some algorithm to check whether a given no is prime no or not inside while loop . http://en.wikipedia.org/wiki/Prime_number

http://en.wikibooks.org/wiki/Efficient_Prime_Number_Generating_Algorithms

You need two loops here - the first to run between beginNum and endNum and the second to run from 1 to beginNum for each value of beginNum in the outer loop.

Try replacing the main section of your code with the following. (For clarity, I'm going to introduce a new variable - numberBeingTested.)

var ctr = 0;
var numberBeingTested = beginNum;

while (numberBeingTested <= endNum){ //throwaway

    var testDivisor = 2;
    var isPrime = true;

    while (testDivisor < numberBeingTested ){ //throwaway
        if (numberBeingTested % testDivisor == 0) {
            isPrime = false;
        }                    

    ++testDivisor;
    }

    if (isPrime){
        primeNumbs[ctr] = numberBeingTested;
        ++ctr;
    }

    ++numberBeingTested;
}

Note that there are many possible improvements here - for a start, this code as it stands will tell you that 1 is prime, and there are significant possible performance improvements (such as testing possible divisors up to the square root of the number being tested rather than the number itself) - but for your purposes it will probably suffice.

What I try was to edit Sieve of Atkin algorithm from linked question:

function getPrimes(min, max) {
    var sieve = [], i, j, primes = [];
    for (i = 2; i <= max; ++i) {
        if (!sieve[i]) {
            // i has not been marked -- it is prime
            if (i >= min) {
                primes.push(i);
            }
            for (j = i << 1; j <= max; j += i) {
                sieve[j] = true;
            }
        }
    }
    return primes;
}

console.log(getPrimes(10, 100));

This will give you array with prime numbers from min to max. It still has to go through all number from 2, so maybe there will be more effective way how to achieve this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top