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

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

- "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*.

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:

# | 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**).

- Required for all students who have taken Data Structures and Algorithm Design.
- Optional for others (+10% extra credit)

Example:

Prime factorization: 2 * 2 * 2 * 2 * 2 * 2 * 7

Alternately, you could display the factorization as a polynomial:

Prime factorization: 2

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

Edit | Attach | Print version | History: r1 | Backlinks | Raw View | Raw edit | More topic actions

Topic revision: r1 - 2015-02-18 - RobertKasper

Copyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

Ideas, requests, problems regarding TWiki? Send feedback

Ideas, requests, problems regarding TWiki? Send feedback