<%@ page import="java.util.*,Prime" %>
<%

// process page

String action = request.getParameter("action"); String nString = request.getParameter("n");

int n = -1;

boolean hasError = false;
String message = null;

if (action == null || action.equals("Clear")) {

nString = null;
}
else if (nString == null || nString.equals("")) {

        hasError = true;
        message = "Enter an integer for <i>n</i>!";

}
else
{

        try
        {
            n = Integer.valueOf(nString).intValue();
        }
        catch (NumberFormatException nfe)
        {
            hasError = true;
            message = "Enter an integer for <i>n</i>!";
        }

        if (!hasError)
        {
            if (action.equals("n is prime?"))
            {
                if (n < 2)
                {
                    hasError = true;
                    message = "Enter an integer >= 2 for <i>n</i>!";
                }
                else if (Prime.isPrime(n))
                {
                    message = "Yes, "+n+" is a prime #.";
                }
                else
                {
                    message = "No, "+n+" is a composite #.";
                }
            }
            else if (action.equals("nth prime"))
            {
                if (n <= 0)
                {
                    hasError = true;
                    message = "Enter an integer >= 1 for <i>n</i>!";
                }
                else
                {
                    message = "The "+n+"th prime is "+Prime.getPrime(n)+".";
                }
            }
            else if (action.equals("pi(n) & its estimate"))
            {
                if (n < 2)
                {
                    hasError = true;
                    message = "Enter an integer >= 2 for <i>n</i>!";
                }
            }
            else if (action.equals("All primes <= n"))
            {
                if (n < 2)
                {
                    hasError = true;
                    message = "Enter an integer >= 2 for <i>n</i>!";
                }
            }
        }

}

// print page
%>
<html>
<head><title>Prime Numbers</title></head> <body bgcolor="white">
<font size=3>
Enter an integer <i>n</i> below to see if <i>n</i> is a prime #, the <i>n</i>th prime, the # of primes <= <i>n</i> (<i>pi(n) & its estimate</i>), or a list of primes <= <i>n</i>.
<p>
The algorithm used is based on the well-known method for finding primes called <i>Sieve of Eratosthenes</i>. In estimating <i>pi(n)</i>, we use the <i>Prime Number Theorem</i> which states that the ratio of <i>pi(n)</i> to <i>n/ln(n)</i> -> 1 as <i>n</i> -> infinity.
<p>
<form method=POST action=Prime.jsp>
<%

if (nString == null || nString.equals("")) {
%>
<i>n</i> <input type=text name=n size=8 maxlength=8> <%

}
else
{
%>
<i>n</i> <input type=text name=n value=<%= nString %> size=8 maxlength=8> <%

}
%>
<p>
<i>
<input type=submit name=action value="n is prime?"> <input type=submit name=action value="nth prime"> <input type=submit name=action value="pi(n) & its estimate"> <input type=submit name=action value="All primes <= n"> <input type=submit name=action value="Clear"> </i>
</form>
<p>
<%

if (action == null || action.equals("Clear")) {
}
else if (hasError || action.equals("n is prime?") || action.equals("nth prime")) {
%>
<%= message %>
<%

}
else if (action.equals("pi(n) & its estimate")) {
%>
<table border=1>
<tr>
<th><i>n</i>
<th><i>pi(n)</i>
<th><i>pi(n)</i> estimate <i>n/ln(n)</i> <th>Error in %</i>
<%

        int pi = Prime.getPrimeCount(n);
        double estimate = n / Math.log(n);
        double error = Math.abs(pi - estimate)/pi * 100;

%>
<tr>
<td><%= n %>
<td><%= pi %>
<td><%= estimate %>
<td><%= error %>
</table>
<%

}
else if (action.equals("All primes <= n")) {

List list = Prime.getPrimes(n); %>
<table border=1>
<tr><th><i>i</i><th><i>i</i>th Prime
<%

        Iterator iterator = list.iterator();
        int i = 1;
        for (; iterator.hasNext(); i++)
        {
            int prime = ((Integer)iterator.next()).intValue();

%>
<tr><td><%= i %><td><%= prime %>
<%

}
%>
</table>
<%

}
%>
</font>
<hr>
<a href="http://www.computing-wisdom.com"><i>Computing Wisdom Inc. Home Page</i></a> </body>
</html>