OPPGAVE 1: ---------- ================code===================== public class Permutation{ // use the entire string as the endingString // when called with one parameter public void permuteString(String s){ permuteString("", s); } public void assignment1(){ long t_0 = System.currentTimeMillis(); permuteString("123456789"); System.out.println("time used: "+(System.currentTimeMillis() - t_0)+" ms"); } // recursive declaration of method permuteString public void permuteString( String beginningString, String endingString ){ // base case: if string to permute is length less than or equal to // 1, just display this string concatenated with beginningString if ( endingString.length() <= 1 ){ if(isDevidable(beginningString + endingString)){ System.out.println( beginningString + endingString ); } }else{ // recursion step: permute endingString // for each character in endingString for ( int i = 0; i < endingString.length(); i++ ){ try{ // create new string to permute by eliminating the // character at index i String newEndString = endingString.substring( 0, i ) + endingString.substring( i + 1 ); String newBeginString = beginningString + endingString.charAt( i ); // recursive call with a new string to permute // and a beginning string to concatenate, which // includes the character at index i if( isDevidable( newBeginString)){ permuteString( newBeginString, newEndString ); } }catch ( StringIndexOutOfBoundsException exception ){ exception.printStackTrace(); } } } } public boolean isDevidable(String s){ // ignore test if string is not a number // also ignore for '1' length digits since // all numbers % 1 == 0 if(! s.matches("^\\d+$") || s.length() == 1) return true; int sAsInt = Integer.parseInt(s); return (sAsInt % s.length() == 0); } public static void main(String[] args){ Permutation p = new Permutation(); for(String s: args){ p.permuteString(s); System.out.println("----"); } p.assignment1(); } }