1 36 37 40 41 42 43 import java.util.Random ; 44 45 53 class Permuter { 54 57 private int modulus; 58 59 62 private int multiplier; 63 64 67 private int addend = 22; 68 69 public Permuter(int n) { 70 if (n<0) { 71 throw new IllegalArgumentException (); 72 } 73 modulus = n; 74 if (n==1) { 75 return; 76 } 77 78 multiplier = (int) Math.sqrt(n); 80 while (gcd(multiplier, n) != 1) { 81 if (++multiplier == n) { 82 multiplier = 1; 83 } 84 } 85 } 86 87 92 public int map(int i) { 93 return (multiplier * i + addend) % modulus; 94 } 95 96 99 private static int gcd(int a, int b) { 100 while(b != 0) { 101 int tmp = a % b; 102 a = b; 103 b = tmp; 104 } 105 return a; 106 } 107 108 111 public static void main(String [] args) { 112 int modulus = Integer.parseInt(args[0]); 113 Permuter p = new Permuter(modulus); 114 for (int i=0; i<modulus; i++) { 115 System.out.print(p.map(i)+" "); 116 } 117 System.out.println(); 118 } 119 } 120 121 | Popular Tags |