CodeSnippet:
import inout.Console; /** * * @author Михайл Скиба <michael.skiba@rub.de> */ public class Crypto { public static void main (String args []) { while (true) { int a, p, remainder=0, counter = 0; System. out. println("Please enter a"); a = Console.readInt(); System. out. println("Please enter p (must be prime)"); p = Console.readInt(); if (!isPrime(p)) { System. out. println(p + " is not a prime number!"); } do { counter++; // remainder = (int) (Math.pow(a, counter) %p); if(counter == 1){ remainder = a; }else{ remainder = (remainder * a) %p; } /* Debug-Line: */System. out. printf("%d^%d = %d mod %d\n", a, counter, remainder, p ); } while (remainder != 1); System. out. printf("ord(%d)= %d", a, counter ); if (counter == (p - 1)) { System. out. printf(" (%d is a primitiv element)\n", a ); } System. out. println("Continue y?"); if(!Console.readString().equalsIgnoreCase("y")){ } } } // Shamelessly stolen from the interwebs ... // http://stackoverflow.com/questions/2385909/what-would-be-the-fastest-method-to-test-for-primality-in-java public static boolean isPrime(int n) { return !new String(new char[n ]). matches(".?|(..+?)\\1+"); } }
|