public class Primzahlen {

/**************************************************
* File:           Primzahlen.java
* First Created:  30.04.2006
* Last Modified:  30.04.2006
* Author and (C): Dr. Michael Hufschmidt
* *************************************************
* Content / Description / Requirements / Usage:
*
* Test-Applikation fuer Java, hat 2 Methoden: testPrim und main.
* Das Programm rechnet mit 8-Byte Zahlen von Typ "long", damit koennen
* Zahlen im Bereich von +- 9.223.372.036.854.775.807 verarbeitet werden.
*
* *************************************************
*/

  public static long testPrim(long zahl) {
    // Diese Methode gibt den kleinsten Teiler einer Zahl "zahl"
    // zureuck, bzw. 0 wenn die Zahl eine Primzahl ist.
    long test, testBis;
    testBis = (long) Math.sqrt(zahl) + 1;
    // Bis zu diesem Teiler muss ich testen, ob die Zahl prim ist.
    for ( test = 2; test <= testBis ; test++ ) {
      if (zahl % test == 0) { // % ist die Modulo-Funktion
        return test;          // Teiler gefunden
      }
    }
    return 0; // Kein Teiler gefunden
  }
  
  public static void main(String[] args) {
    long zahl, teiler;
    if (args.length != 1) {
      System.out.println("Dieses Programm muss mit einem Kommadozeilenparameter aufgerufen werden!");
      System.out.println("Also z.B. mit \"Java Primzahlen 17\"");
    } else {
      // Umwandlung des Strings in eine numerische Variable
      zahl = Long.parseLong(args[0]);
      // Testen auf Primzahl
      teiler = testPrim(zahl);
      if (teiler ==0 ) {
        System.out.println(zahl + " ist Primzahl.");
      } else {
        System.out.println(zahl + " ist durch " + teiler + " teilbar.");
      }
    }
  }
}


