package com.emansoft.shuffling; /** * @author Emanuel M. Kadziela * Date: Jul 25, 2005 */ public class Shuffles { static long shuffles(int nCards, int iCut) { long shuffles = 0; //initialize the deck int[] initialDeck = new int[nCards]; int[] markings = new int[nCards]; for (int i=0; i 0 && iBotIndex > 0) { iTopIndex--; result[iTopIndex+iBotIndex] = top[iTopIndex];//bottom card from the top deck goes down iBotIndex--; result[iTopIndex+iBotIndex] = bot[iBotIndex];//bottom card from the bottom deck goes down } while (iTopIndex > 0) { iTopIndex--; result[iTopIndex] = top[iTopIndex]; } while (iBotIndex > 0) { iBotIndex--; result[iBotIndex] = bot[iBotIndex]; } return result; } static void printDeck(int[] deck, long shuffles) { System.out.print("shuffle number: "+shuffles+" : ["); for (int i = 0; i result) result = mark; } return result; } static long reduceAndMultiply(long result, long mark) { int iterations = 0; for (int i=2;i<111;i++) { while (result%i==0 && mark%i==0) { result = result/i; mark = mark/i; iterations++; } if (iterations>0) result = result*iterations*i; iterations=0; } result = result*mark; if (mark>110) { for (int j=2; j<100; j++) if (mark%j==0) return result; System.out.println("Warning, the number of shuffles may be excessive as I check for common prime factors" + "only up to 111 and seem to have found one exceeding 111"); } return result; } public static void main (String args[]) { int iDeck = new Integer(args[0]).intValue(); int iCut = new Integer(args[1]).intValue(); System.out.println("It takes "+shuffles(iDeck, iCut)+" shuffles to do a deck of " +iDeck+" cards with a cut of "+iCut+" cards."); } }