1

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>
user3027217
  • 17
  • 1
  • 1
  • 4

4 Answers4

0

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

john
  • 307
  • 2
  • 15
  • that wikibook page is bad advice, sorry. :) the code there is terrible, and terribly slow. it shows the timing of 5.6 seconds for primes under 1 mln, but [this SO answer](http://stackoverflow.com/a/2068548/849891) has it at 0.07 seconds for fastest plane Python code. that's right, 80 times faster is a lot, even if on different computers. – Will Ness Feb 15 '14 at 12:30
  • That means You think that some one should write a code for this simple task . To check whether a given no is prime or not . What is the running time of above code that You have marked as answer . – john Feb 15 '14 at 13:49
  • I'm not the OP, I couldn't possibly mark an answer as accepted. :) That's only for the OP to do. :) – Will Ness Feb 15 '14 at 15:55
0

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.

Neil T
  • 1,039
  • 7
  • 15
0

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.

Community
  • 1
  • 1
Pavel Štěrba
  • 2,481
  • 2
  • 21
  • 44
  • this is not a sieve of Atkin, but of Eratosthenes. :) the `j = i << 1` bit is a *"fast way of multiplying `i` by 2"* but it's a very slow way of starting your work from `i^2`. – Will Ness Feb 15 '14 at 12:22
0

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>
Satish Sharma
  • 9,217
  • 6
  • 24
  • 49