0

I'm trying to display prime numbers from 1-999 in a table and I just can't find a simple answer online or anywhere else.

Here's the question from the book:

A prime number is a number than can only be divided by itself or by one. Examples of prime numbers include 1, 3, 5, 13, and 17. Write a script that prints the prime numbers between 1 and 999 in a table that consists of 10 columns. You will need to use several looping and conditional statements to test all division possibilities. Use document.write() statements to create the table elements and a counter variable to create the table so that it consists of 10 columns. The counter variable should start with an initial value of 0 and be incremented by one each time your code identifies a prime number and prints it in a table cell. Once the counter variable reaches a value of 10 (meaning that 10 cells have been added to the current row), print </tr><tr> to start a new row and reset the variable to 0. Save the document as PrimeNumbers.html.

Any help is greatly appreciated! I am really stuck on this one.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Seems theory, try to give a precise explanation to your question with the code that you have tried so far. – Praveen Nov 08 '13 at 20:45
  • 1 is not a prime number. – Niet the Dark Absol Nov 08 '13 at 20:46
  • I'm not really sure where to start. This is only my 3rd week of doing JavaScript. – user2970463 Nov 08 '13 at 20:46
  • 2
    Also, any book that advises you to use `document.write` should be burned with sacred fire. – Niet the Dark Absol Nov 08 '13 at 20:46
  • Welcome to Stack Overflow! Please make sure you search for similar problems before posting - looks like the poster of this question (http://stackoverflow.com/questions/11966520/how-to-find-prime-numbers) has tried to do what your book tells you to do, and the answers show various sieving methods which will work much better! – Daniel Attfield Nov 08 '13 at 20:53

3 Answers3

3

I'm going to answer this, to show you how it should be done, because that book of yours is bad.

First of all, never use document.write. It is an outdated function and many far better alternatives exist.

First, let's define a function to test if a number is prime:

function isPrime(n) {
    if( n < 2) return false;
    // a number is prime if it is divisible only by 1 and itself.
    // so, let's check it
    var rt = Math.sqrt(n), i;
    for( i=2; i<=rt; i++) {
        if( n%i == 0) {
            // the number is divisible by something else.
            return false;
        }
    }
    return true;
}

Now, we build our main logic. First, we need a table:

var tbl = document.createElement('table'),
    tbd = tbl.appendChild(document.createElement('tbody')),
    tr, td, i, found = 0;

I've also defined the variables we'll need. Now we just loop on through, and see what we get:

for( i=2; i<1000; i++) { // we can start at 2, because as I said earlier 1 is not prime
    if( isPrime(i)) {
        // if the number of found numbers is a multiple of 10, start a new row
        // the first prime we find will be "number 0", which is divisible by 10.
        if( found % 10 == 0) tr = tbd.appendChild(document.createElement('tr'));
        td = tr.appendChild(document.createElement('td'));
        td.appendChild(document.createTextNode(i));
        found++;
    }
}

To be true to standards, the last row must have the full 10 cells. Here I'm "padding" it with a colspan'd cell

if( found % 10 != 0) {
    td = tr.appendChild(document.createElement('td'));
    td.colSpan = 10 - found % 10;
}

And finally, we add our table to the page:

document.body.appendChild(tbl);

Done! Here's a demo of it in action!

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
2

Here is simple example to get the prime numbers within 1 - 999

var i, 
    prime, 
    upper = 999,
    lower = 1,
    n;

for (n = lower + 1; n < upper; n++) {
    prime = 1;
    for (i = 2; i < n; i++)
    if (n % i == 0) {
        prime = 0;
        break;
    }
    if (prime) {
        console.log(n);
    }
}

Append these values to table.

Praveen
  • 55,303
  • 33
  • 133
  • 164
  • 1
    You should probably study some basic maths, for efficiency reasons :p When testing whether 11 is prime, for example, there is no need to see if you can divide it by 4, or 5, or 6... or indeed anything greater than its square root. – Niet the Dark Absol Nov 08 '13 at 20:55
1

Just to get prime numbers:

function is_prime(n)
{
    if ((n < 2) || ((n % 2) == 0)) {
        return (n == 2);
    }

    for (f = 3; (f * f) <= n; f += 2) {
        if ((n % f) == 0) {
            return false;
        }
    }

    return true;
}

var primes = [];

for (n = 1; n < 1000; n++) {
    if (is_prime(n)) {
        primes.push(n);
    }
}

console.log(primes);

Good luck!

Paulo Freitas
  • 13,194
  • 14
  • 74
  • 96