Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.93 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3. import java.util.concurrent.ThreadLocalRandom;
  4.  
  5. public class Console {
  6.     public static void main(String[] args) {
  7.         System.out.println("Please choose one of available generators.");
  8.         System.out.println("1) Java Generator");
  9.         System.out.println("2) ANSI-C Generator");
  10.         System.out.println("3) GNU Generator");
  11.         System.out.println("4) Borland Generator");
  12.         System.out.println("5) SIMULA Generator");
  13.  
  14.         Scanner scanner = new Scanner(System.in);
  15.         int option = scanner.nextInt();
  16.  
  17.         long seed = System.currentTimeMillis() / 1000;
  18.  
  19.         RandomNumberGenerator randomNumberGenerator;
  20.         switch (option) {
  21.             case 2:
  22.                 randomNumberGenerator = new ANSIC(seed);
  23.                 break;
  24.             case 3:
  25.                 randomNumberGenerator = new GNU(seed);
  26.                 break;
  27.             case 4:
  28.                 randomNumberGenerator = new Borland(seed);
  29.                 break;
  30.             case 5:
  31.                 randomNumberGenerator = new SIMULA(seed);
  32.                 break;
  33.             default:
  34.                 randomNumberGenerator = new JavaRandomNumberGeneratorWrapper();
  35.         }
  36.  
  37.         MillerRabin millerRabin = new MillerRabin(randomNumberGenerator);
  38.  
  39.         System.out.println("Enter numbers (one by one): ");
  40.         while ((option = scanner.nextInt()) != 0) {
  41.             System.out.println(option);
  42.             boolean result = millerRabin.testIsPrime(option, 100);
  43.             System.out.println(result ? "Probably Prime" : "Complex");
  44.         }
  45.     }
  46. }
  47.  
  48. class MillerRabin {
  49.  
  50.     private static long powerModulo(long a, long b, long m) {
  51.         long n = 1;
  52.         long x = a % m;
  53.  
  54.         for (long i = 1; i <= b; i <<= 1) {
  55.             x %= m;
  56.             if ((b & i) != 0) {
  57.                 n *= x;
  58.                 n %= m;
  59.             }
  60.             x *= x;
  61.         }
  62.  
  63.         return n;
  64.     }
  65.  
  66.     private RandomNumberGenerator randomNumberGenerator;
  67.  
  68.     public MillerRabin(RandomNumberGenerator randomNumberGenerator) {
  69.         this.randomNumberGenerator = randomNumberGenerator;
  70.     }
  71.  
  72.     public boolean testIsPrime(long n, long k) {
  73.         if (n <= 1)
  74.             throw new IllegalArgumentException("n > 1");
  75.         if (n == 2)
  76.             return true;
  77.         if (n % 2 == 0)
  78.             return false;
  79.  
  80.         long d = n - 1;
  81.         int s = 0;
  82.         while ((d % 2) == 0) {
  83.             s++;
  84.             d = d / 2;
  85.         }
  86.  
  87.         outer:
  88.         for (long i = 0; i < k; i++) {
  89.             long a = randomNumberGenerator.nextRandom(2, n - 1);
  90.  
  91.             long x = powerModulo(a, d, n);
  92.             if (x == 1 || x == n - 1) {
  93.                 continue;
  94.             }
  95.  
  96.             for (int r = 1; r <= s - 1; r++) {
  97.                 x = powerModulo(x, 2, n);
  98.  
  99.                 if (x == 1) {
  100.                     return false;
  101.                 }
  102.                 if (x == n - 1) {
  103.                     continue outer;
  104.                 }
  105.             }
  106.  
  107.             return false;
  108.         }
  109.  
  110.         return true;
  111.     }
  112. }
  113.  
  114. interface RandomNumberGenerator {
  115.     long nextRandom(long l, long k);
  116. }
  117.  
  118. abstract class LCG implements RandomNumberGenerator {
  119.  
  120.     private long a;
  121.     private long xn;
  122.     private long c;
  123.     private long m;
  124.  
  125.     public LCG(long a, long xn, long c, long m) {
  126.         if (!(0 < a && a < m
  127.                 && 0 <= c && c < m
  128.                 && 0 <= xn && xn < m)) {
  129.             throw new IllegalArgumentException("Wrong arguments values");
  130.         }
  131.  
  132.         this.a = a;
  133.         this.xn = xn;
  134.         this.c = c;
  135.         this.m = m;
  136.     }
  137.  
  138.     public long nextRandom(long l, long k) {
  139.         if (l > k) {
  140.             throw new IllegalArgumentException("l should be lower than k");
  141.         }
  142.         if (k == l) {
  143.             return k;
  144.         }
  145.  
  146.         BigInteger a = BigInteger.valueOf(this.a);
  147.         BigInteger xn = BigInteger.valueOf(this.xn);
  148.         BigInteger c = BigInteger.valueOf(this.c);
  149.         BigInteger m = BigInteger.valueOf(this.m);
  150.  
  151.         // ((a * xn) + c) % m;
  152.         long base = a.multiply(xn).add(c).mod(m).longValue();
  153.         this.xn = base;
  154.  
  155.         return (base % (k - l)) + l;
  156.     }
  157. }
  158.  
  159. class ANSIC extends LCG {
  160.     public ANSIC(long xn) {
  161.         super(30517578125L, xn, 0, 34359738368L);
  162.     }
  163. }
  164.  
  165. class Borland extends LCG {
  166.     public Borland(long xn) {
  167.         super(22695477, xn, 1, 4294967296L);
  168.     }
  169. }
  170.  
  171. class GNU extends LCG {
  172.     public GNU(long xn) {
  173.         super(1103515245, xn, 12345, 4294967296L);
  174.     }
  175. }
  176.  
  177. class SIMULA extends LCG {
  178.     public SIMULA(long xn) {
  179.         super(30517578125L, xn, 0, 34359738368L);
  180.     }
  181. }
  182.  
  183. class JavaRandomNumberGeneratorWrapper implements RandomNumberGenerator {
  184.     @Override
  185.     public long nextRandom(long l, long k) {
  186.         return (ThreadLocalRandom.current().nextLong(l, k + 1));
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement