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