import java.util.Scanner; public class Shuffle2 { public static int [] shuffle( int[] x , int a ) { int[] out = new int[53]; int j; for ( int i = 1; i <= 52; i++ ) { a = a%53; // Prevent overflow j = (i*a)%53; // a, 2a, 3a out[j] = x[i]; // Put card i into j-th position (permute) } return out; } public static void print( int[] x ) { int k = 1; for ( int i = 0; i < 2; i++ ) { for ( int j = 0; j < 26; j++ ) System.out.print(x[k++] + " "); System.out.println(); } System.out.println(); } public static void main( String[] args ) { int i, a; int[] x = new int[53]; int[] y; Scanner in = new Scanner(System.in); for ( i = 1; i <= 52; i++ ) x[i] = i; while ( true ) { System.out.print("Enter a = "); a = in.nextInt(); if ( a == 1 || a%53 == 0 ) { System.out.println("Error : " + a + " is 1 or divisble by 52"); System.out.println("Try again....\n"); continue; } i = 1; while ( true ) { y = shuffle(x, i*a); System.out.println("Shuffle i = " + i); print(y); if ( y[1] == 1 ) break; i++; } } } }