send
Grupo de Telegram
play_arrow
Este sitio utiliza cookies propias y de terceros. Si continúa navegando consideramos que acepta el uso de cookies. OK Más Información.

Applet de factorizacion de enteros gausianos

  • 0 Respuestas
  • 1988 Vistas

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado ProcessKill

  • *
  • Underc0der
  • Mensajes: 154
  • Actividad:
    0%
  • Reputación 0
  • Arriba los hackers de Argentina!
    • Ver Perfil
    • Email
« en: Febrero 24, 2010, 04:24:03 pm »
Applet de factorizacion de enteros gausianos

Código: Java
  1. // <XMP>
  2. // Gaussian Integer factorization applet
  3. //
  4. // Written by Dario Alejandro Alpern (Buenos Aires - Argentina)
  5. // Last updated May 31st, 2002, See http://www.alpertron.com.ar/GAUSIANO.HTM
  6. //
  7. // No part of this code can be used for commercial purposes without
  8. // the written consent from the author. Otherwise it can be used freely
  9. // except that you have to write somewhere in the code this header.
  10. //
  11. import java.applet.*;
  12. import java.util.*;
  13. import java.awt.*;
  14. import java.math.*;
  15.  
  16. public final class gausiano extends You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login implements You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login {
  17.  
  18. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login Primes[];
  19. private int Exponents[];
  20. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login Solution1[];
  21. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login Solution2[];
  22. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login Increment[];
  23. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login Aux[];
  24. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login ValA, ValB;
  25. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login info;
  26. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login txt;
  27. private int SolNbr;
  28. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login factorWindow;
  29. private ecmc factorPanel;
  30. private volatile You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login calcThread;
  31. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login LastModulus = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.valueOf(0);
  32. private int Ind;
  33. private You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login textError;
  34.  
  35. void w(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login texto) {
  36.   info=info.append(texto);
  37.   }
  38.  
  39. void Solution(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login value) {
  40.   SolNbr++;
  41.   w(SolNbr+") x = "+value.toString()+"<BR>");
  42.   }
  43.  
  44. public void init() {
  45.   Primes=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[400];
  46.   Exponents=new int[400];
  47.   Solution1=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[400];
  48.   Solution2=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[400];
  49.   Increment=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[400];
  50.   Aux=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[400];
  51.   }
  52.  
  53. /* type = 0: factor expression, type = 1: just compute expression */
  54. public You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login startCalc(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login expr, int type) {
  55.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login InputField;
  56.   if (calcThread != null) {
  57. //    TerminateThread = true;
  58.     try {
  59.       calcThread.join();        /* Wait until the solving thread dies */
  60.       } catch (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login ie) {};
  61.     }
  62.   calcThread = new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login(this);  /* Start solving thread */
  63.   try {
  64.     int ExpressionRC;
  65.     You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login [] ExpressionResult = new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[2];
  66.     InputField = expr.trim();
  67.     if (InputField.equals("")) {
  68.       ExpressionRC = -6;
  69.       }
  70.     try {
  71.       ExpressionRC = GaussExpression.ComputeExpression(InputField, ExpressionResult);
  72.       } catch (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {ExpressionRC = -1;}
  73.     switch (ExpressionRC) {
  74.       case -1:
  75.         textError = "No hay suficiente memoria.";
  76.         break;
  77.       case -2:
  78.         textError = "Número muy grande (más de 500 dígitos).";
  79.         break;
  80.       case -3:
  81.         textError = "Expresión intermedia con más de 2000 dígitos.";
  82.         break;
  83.       case -4:
  84.         textError = "División no entera.";
  85.         break;
  86.       case -5:
  87.         textError = "Error de paréntesis.";
  88.         break;
  89.       case -6:
  90.         textError = "Error de sintaxis.";
  91.         break;
  92.       case -7:
  93.         textError = "Demasiados paréntesis.";
  94.         break;
  95.       case -8:
  96.         textError = "Parámetro inválido.";
  97.         break;
  98.       default:
  99.         if (ExpressionResult[0].compareTo(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.valueOf(10L).pow(500))>=0) {
  100.           textError = "Número muy grande (más de 500 dígitos).";
  101.           }
  102.         else {
  103.           textError = "";
  104.           }
  105.       }
  106.     if (textError.length() > 0) {return textError;}
  107.     ValA = ExpressionResult[0];
  108.     ValB = ExpressionResult[1];
  109.     } catch (You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login e) {return "Se entraron datos inválidos";}
  110.   if (type == 1) {     /* Only computing expression */
  111.     info=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login();
  112.     w("<HTML><HEAD><TITLE>Calculadora de enteros gausianos</TITLE></HEAD><BODY>");
  113.     w("<CENTER><H3><FONT COLOR=Red>Calculadora de enteros gausianos</FONT></H3></CENTER>");
  114.     w("<P><I>por Dario Alejandro Alpern</I><P><DL><DT>Expresión de entrada:<DD>");
  115.     w(InputField);
  116.     w("<P><DT>Valor:<DD>");
  117.     w(ValA.toString()+(ValB.signum()>=0?" + ":" - ")+ValB.abs().toString()+" i</DL>");
  118.     w("<P>Si encontró algún error, por favor envíeme un <A HREF=\"mailto:alpertron@hotmail.com?subject=Factorizacion de enteros de Gauss\">e-mail</A><P>");
  119.     w("<A HREF=\"GBOOK.HTM\">Apriete aquí</A> para ver o firmar mi libro de invitados.</BODY></HTML>");
  120.     return info.toString();
  121.     }
  122.   calcThread.start();
  123.   return "";
  124.   }
  125.  
  126. public You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login resultCalc() {
  127.   if (calcThread == null) {
  128.     return info.toString();
  129.     }
  130.   return "";
  131.   }
  132.  
  133. public void run() {
  134.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login parms1[];
  135.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login parms2[];
  136.   parms1=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[0];
  137.   parms2=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login[1];
  138.   info=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login();
  139.   w("<TITLE>Applet de factorizacion de enteros de Gauss</TITLE><CENTER><FONT COLOR=Red><B>");
  140.   w("Factores primos de "+ValA.toString()+(ValB.signum()>=0?" + ":" - ")+ValB.abs().toString()+" i</B></FONT></CENTER><P><I>por Dario Alejandro Alpern</I><P>");
  141.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login OldDate=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login();
  142.   long Old=OldDate.getTime();
  143.   SolNbr = 0;
  144.   GaussianFactorization();
  145.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login NewDate=new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login();
  146.   long New=NewDate.getTime();
  147.   w("<P>Tiempo de cálculo: ");
  148.   int t=(int)(((New-Old)/1000+86400)%86400);
  149.   w(t/3600+"h "+((t%3600)/60)+"m "+(t%60)+"s");
  150.   w("<P>Si encontró algún error, por favor envíeme un <A HREF=\"mailto:alpertron@hotmail.com?subject=Factorizacion de enteros de Gauss\">e-mail</A><P>");
  151.   w("<A HREF=\"GBOOK.HTM\">Apriete aquí</A> para ver o firmar mi libro de invitados.</BODY></HTML>");
  152.   calcThread = null;
  153.   }
  154.  
  155. void GaussianFactorization() {
  156.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login BigInt0, BigInt1, BigInt2;
  157.   BigInt0 = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.valueOf(0L);
  158.   BigInt1 = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.valueOf(1L);
  159.   BigInt2 = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.valueOf(2L);
  160.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login K, Mult1, Mult2, p, q, M1, M2, Tmp;
  161.   int index, index2;
  162.  
  163.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login norm = ValA.multiply(ValA).add(ValB.multiply(ValB));
  164.   Ind = 0;
  165.   if (norm.signum() == 0) {
  166.     w("Cualquier primo gausiano divide este número");
  167.     return;
  168.     }
  169.   w("<UL>");
  170.   if (norm.compareTo(BigInt1) > 0) {
  171.     if (norm.bitLength() < 32) {
  172.       long modulus = norm.longValue();
  173.       int Exp = 0;
  174.       Ind = 0;
  175.       while (modulus % 2 == 0) {
  176.         Exp++;
  177.         modulus /= 2;
  178.         }
  179.       if (Exp > 0) {
  180.         Primes[0] = BigInt2;
  181.         Exponents[0] = Exp;
  182.         Ind++;
  183.         }
  184.       long Div = 3;
  185.       while (Div*Div <= modulus) {
  186.         Exp = 0;
  187.         while (modulus % Div == 0) {
  188.           Exp++;
  189.           modulus /= Div;
  190.           }
  191.         if (Exp > 0) {
  192.           Primes[Ind] = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.valueOf(Div);
  193.           Exponents[Ind] = Exp;
  194.           Ind++;
  195.           }
  196.         Div += 2;
  197.         }
  198.       if (modulus > 1) {
  199.         Primes[Ind] = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login.valueOf(modulus);
  200.         Exponents[Ind] = 1;
  201.         Ind++;
  202.         }
  203.       }
  204.     else {     /* Factor norm > 2^32 */
  205.       factorPanel = new ecmc();
  206.       factorWindow = new You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login("Factorizacion de la norma");
  207.       factorWindow.setResizable(false);
  208.       factorWindow.add(factorPanel);
  209.       factorPanel.setSize(600, 390);
  210.       You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login in = factorWindow.getInsets();
  211.       factorWindow.setSize(600+in.right+in.left, 390+in.top+in.bottom);
  212.       factorWindow.show();
  213.       Ind = factorPanel.getFactors(norm, Primes, Exponents);
  214.       factorWindow.remove(factorPanel);
  215.       factorWindow.dispose();
  216.       }
  217.     for (index = 0; index < Ind; index++) {
  218.       p = Primes[index];
  219.       if (p.equals(BigInt2)) {
  220.         for (index2 = 0; index2 < Exponents[index]; index2++) {
  221.           DivideGaussian(BigInt1, BigInt1);           /* Divide by 1+i */
  222.           DivideGaussian(BigInt1, BigInt1.negate());  /* Divide by 1-i */
  223.           }
  224.         }
  225.       if (p.testBit(1) == false) {    /* if p = 1 (mod 4) */
  226.         q = p.subtract(BigInt1);      /* q = p-1 */
  227.         K = BigInt1;
  228.         do {                 // Compute Mult1 = sqrt(-1) mod p
  229.           K = K.add(BigInt1);
  230.           Mult1 = K.modPow(q.shiftRight(2),p);
  231.           } while (Mult1.equals(BigInt1) || Mult1.equals(q));
  232.         Mult2 = BigInt1;
  233.         while (true) {
  234.           K = Mult1.multiply(Mult1).add(Mult2.multiply(Mult2)).divide(p);
  235.           if (K.equals(BigInt1)) {
  236.             break;
  237.             }
  238.           M1 = Mult1.mod(K);
  239.           M2 = Mult2.mod(K);
  240.           if (M1.compareTo(K.shiftRight(1)) > 0) {M1 = M1.subtract(K);}
  241.           if (M2.compareTo(K.shiftRight(1)) > 0) {M2 = M2.subtract(K);}
  242.           Tmp = Mult1.multiply(M1).add(Mult2.multiply(M2)).divide(K);
  243.           Mult2 = Mult1.multiply(M2).subtract(Mult2.multiply(M1)).divide(K);
  244.           Mult1 = Tmp;
  245.           }            /* end while */
  246.         if (Mult1.abs().compareTo(Mult2.abs()) < 0) {
  247.           Tmp = Mult1;
  248.           Mult1 = Mult2;
  249.           Mult2 = Tmp;
  250.           }
  251.         for (index2 = 0; index2 < Exponents[index]; index2++) {
  252.           DivideGaussian(Mult1,Mult2);
  253.           DivideGaussian(Mult1,Mult2.negate());
  254.           }
  255.         }              /* end p = 1 (mod 4) */
  256.       else {           /* if p = 3 (mod 4) */
  257.         for (index2 = 0; index2 < Exponents[index]; index2++) {
  258.           DivideGaussian(Primes[index],BigInt0);
  259.           }            /* end p = 3 (mod 4) */
  260.         }
  261.       }
  262.     }
  263.   if (ValA.equals(BigInt1)) {
  264.     if (Ind == 0) {
  265.       w("Ningún primo gausiano divide este número");
  266.       }
  267.     }
  268.   else if (ValA.add(BigInt1).signum() == 0) {
  269.     w("<LI>-1");
  270.     }
  271.   else if (ValB.equals(BigInt1)) {
  272.     w("<LI>i");
  273.     }
  274.   else {
  275.     w("<LI>-i");
  276.     }
  277.   w("</UL>");
  278.   }
  279.  
  280. private void DivideGaussian(You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login real, You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login imag) {
  281.   real = real.abs();
  282.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login norm = real.multiply(real).add(imag.multiply(imag));
  283.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login realNum = ValA.multiply(real).add(ValB.multiply(imag));
  284.   You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login imagNum = ValB.multiply(real).subtract(ValA.multiply(imag));
  285.   if (realNum.mod(norm).signum() == 0 &&
  286.       imagNum.mod(norm).signum() == 0) {
  287.     ValA = realNum.divide(norm);
  288.     ValB = imagNum.divide(norm);
  289.     w("<LI>");
  290.     w(real.toString()+(imag.signum()>=0?" + ":" - ")+imag.abs().toString()+" i");
  291.     }
  292.   }
  293. }

Espero que les sirva el code  ;D Saludos
« Última modificación: Abril 18, 2013, 12:44:52 pm por Expermicid »

 

¿Te gustó el post? COMPARTILO!