Calculer la factorielle jusqu'à 5409! avec jsp et l'afficher sur browser en un tableau

Contenu du snippet

Ce code fait le calcule de N! de N = 0 jusqu'à N = len //len est spécifier et que vous pouvez changer à n'importe quel moment et qui est dan le fichier index.jsp
Le code utilise BigInteger et quelques notions en JSP :D
attention si vous faite le len = 5409 faudrait attendre une 15aines de minutes et faudrait que vous ayez un espace d'une 50aines de Mo .
faudrait mettre le code JAVA dans un fichier Factorielle.java et dans un paquetage package ma.inpt.scupper;
et puis le code JSP dans une page index.jsp

Source / Exemple :


/*****JSP SRC ********/

<%-- 
    Document   : index
    Created on : Mar 23, 2011, 2:57:32 AM
    Author     : scupper
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>Factorielle</title>
    </head>
    <body>
        <%@page
            language="Java" import="java.io.*, java.util.*,java.math.*,java.lang.*,ma.inpt.scupper.Factorielle"
            info="Utilité de la page doit être écrite ici"
            contentType="text/html"
            session="false"
            buffer="32kb"
            autoFlush="true"
            isThreadSafe="true"
            errorPage="/JSPErrors/Error2.jsp"
            %>
            <%!
                long len=20;
            %>
            <table title="Factorielle" style="font-size: 15px;" border="3" width="2" cellspacing="1" cellpadding="1">
                <thead style=" font-size: 40px ">
                    <tr >
                        <th style="background: transparent;background-color:rgb(100,50,120); font : larger;text-decoration:  blink;">N</th>
                        <th style="background: transparent;background-color:rgb(0,50,100); font : larger;text-decoration:  blink;">N!</th>
                    </tr>
                </thead>
                <tbody>
                        <% for(long i=0;i<len+1;i++){
                                ma.inpt.scupper.Factorielle obj = new ma.inpt.scupper.Factorielle();
                                try{
                                    BigInteger Int = obj.Factorielle(i);
                                    out.print("<tr>");
                                    out.print("<td style=\"font-family:  serif; background-color:rgb(100,75,120);\">"+i+"</td>");
                                    out.print("<td style=\"font-family:  fantasy; background-color:rgb(0,70,100);\">"+Int + "</td>");
                                    out.print("</tr>");
                                }catch(java.lang.IllegalArgumentException e){
                                    out.print("<tr>");
                                    out.println("<td><b>Error: </td>");
                                    out.println("<td>"+e.toString()+"</td>");
                                    out.print("<tr>");
                                    out.print("<div><h1><b> Le ");
                                    int index =e.getLocalizedMessage().lastIndexOf('=');
                                    out.print(e.getMessage().substring(index+2));
                                    out.println(" que vous avez entré n'est pas dans les bornes calculable par l'algorithme ....</b></h1></div>");
                                }
                           }
                        %>
                </tbody>
            </table>

    </body>
</html>

/*****Java src********/

package ma.inpt.scupper;

import java.math.*;

/**
 *

  • @author scupper
  • @since 02:59 March 23rd 2011
  • /
public class Factorielle { private static BigInteger minus(BigInteger a,BigInteger b){ BigInteger c=a ; BigInteger d=b.negate(); c=c.add(d); return c; } public static BigInteger Factorielle(long n) throws IllegalArgumentException{ BigInteger fact=BigInteger.ONE; BigInteger N = BigInteger.valueOf(n); if(n<=0){ return BigInteger.ONE; }else{ if(n>=5410){ throw new IllegalArgumentException("Can't calculate Factorielle for this n= "+n); }else{ return Factorielle(n-1).multiply(N); } } } public static void main(String[] args){ try { System.out.println(Factorielle(5409)); } catch (Exception ex) { System.out.println("Can't calculate this long"); } } }

Conclusion :


lorsque vous deployerais la page JSP dans un serveur (glassfish,tomcat ou autre) le résultat c'est tableau avec N et N!

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.