Lab 4 - JavaScript Prime Calculator

Due Feb 25

Goal

  • To build a simple web application to perform calculations with prime numbers.
  • To learn common JavaScript methods to process input fields and create tables.

Step 1 - Check if a number is prime

Create a page with a form element, which contains a text input field, an output text box, and a button called "Check Prime". When a number n is entered in the text input field, and the button is pressed, your JavaScript program should determine whether the number is prime and write a message in the output text box, either

  • "The number n is a prime number", OR
  • "The number n is not a prime number, it is divisable by x."

where n and x are the actual numbers determined by the user input. If n is divisable by more than one other integer, you may report any of them. Use appropriate CSS to make the page look attractive and well organized.

Note: A number n is divisable by x if:

n % x = 0

where % is the mod operator. The mod operation of x % y returns the remainder of dividing x by y.

Step 2 - build a factor table

Add a second button to the page from step 1, called "Show Factors". When this button is pressed build a table showing a list of ALL factors of the number that was entered, including 1 and the number itself. Your JavaScript program should produce an HTML table of the following general form:

Number: 448
# Factor
1 1
2 2
3 4
4 7
5 8
6 14
7 16
8 28
9 32
10 56
11 64
12 112
13 224
14 448

The table should appear at the bottom of the page under the input form. If another number is entered, and the Show Factors button is clicked, then replace the table with a new one.

Hint: You can find the factors of n simply by doing n % x on every x between 1 and n. This will require a loop ( for or while).

Step 3 - prime factorization

This step is:
  • Required for all students who have taken Data Structures and Algorithm Design.
  • Optional for others (+10% extra credit)
Add a third button called "Show prime factorization". When clicked, your program should show the prime factorization of the number (i.e., it will show the set of prime numbers, which when multipled together, equal the given number.

Example:

Number: 448
Prime factorization: 2 * 2 * 2 * 2 * 2 * 2 * 7

Alternately, you could display the factorization as a polynomial:

Number: 448
Prime factorization: 26 x 71

Below is some Java code (not JavaScript !) to do a prime factorization (you will need to write a Javascript version). You won't need to use lists, but create two arrays, one with the factors (like above) and another to keep count a of the number of times that factor was used. Thus you will end up with arrays something like this: (these arrays are NOT displayed by your program, other than for debugging purposes).

Index Factor Count
2 2 6
3 4 0
4 7 1
5 8 0
6 14 0
7 16 0
8 28 0
9 32 0
10 56 0
11 64 0
12 112 0
13 224 0


<br />// Java Code to find the Prime Factorization of a number.
import java.util.ArrayList;
import java.util.List;

public class PrimeFactors {
public static List<Integer> primeFactors(int number) {
int n = number;
List<Integer> factors = new ArrayList<Integer>();
for (int i = 2; i <= n; i++) {
while (n % i == 0) {
factors.add(i);
n /= i;
}
}
return factors;
}

-- RobertKasper - 2015-02-18

Topic revision: r1 - 2015-02-18 - RobertKasper
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback