Ansichten: QuotePaste - CodePaste - NoPaste
Codesnippet eingetragen am 15.4.2013 um 14:10
Von: Michael
Sprache: Java
Beschreibung:
CodeSnippet:
  1.  
  2. import inout.Console;
  3.  
  4. /**
  5.  *
  6.  * @author Михайл Скиба <michael.skiba@rub.de>
  7.  */
  8. public class Crypto {
  9.  
  10. public static void main(String args[]) {
  11. while (true) {
  12. int a, p, remainder, counter = 0;
  13. System.out.println("Please enter a");
  14. a = Console.readInt();
  15. System.out.println("Please enter p (must be prime)");
  16. p = Console.readInt();
  17.  
  18. if (!isPrime(p)) {
  19. System.out.println(p + " is not a prime number!");
  20. System.exit(0);
  21. }
  22.  
  23. do {
  24. counter++;
  25. remainder = (int) (Math.pow(a, counter) % p);
  26. // Debug-Line: System.out.printf("%d^%d = %d mod %d\n", a, counter, remainder, p);
  27. } while (remainder != 1);
  28. System.out.printf("ord(%d)= %d", a, counter);
  29. if (counter == (p - 1)) {
  30. System.out.printf(" (%d is a primitiv element)", a);
  31. }
  32.  
  33. System.out.println("Continue y?");
  34. if(!Console.readString().equalsIgnoreCase("y")){
  35. System.exit(0);
  36. }
  37. }
  38. }
  39.  
  40. // Shamelessly stolen from the interwebs ...
  41. // http://stackoverflow.com/questions/2385909/what-would-be-the-fastest-method-to-test-for-primality-in-java
  42. public static boolean isPrime(int n) {
  43. return !new String(new char[n]).matches(".?|(..+?)\\1+");
  44. }
  45. }
  46.