CodeSnippet:
package Crypto; import java.util.Scanner; /** * * @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"); Scanner scanner = new Scanner (System. in); a = scanner.nextInt(); System. out. println("Please enter p (must be prime)"); scanner = new Scanner (System. in); p = scanner.nextInt(); if (!isPrime(p)) { System. out. println(p + " is not a prime number!"); } do { counter++; 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)", a ); } System. out. printf("\nContinue y?\n"); scanner = new Scanner (System. in); if (!scanner.nextLine().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+"); } }
|