View Single Post
Old 12-27-2005, 08:44 AM   #1 (permalink)
destin
Java Junkie
 
destin's Avatar
 
Join Date: Mar 2005
Posts: 40
destin is on a distinguished road
...

I was kind of bored today.. so i was searching for a problem to do. I found this: http://www.devx.com/DevX/HTML/17955

So i did it.. everything works.. just i have a huge series of nested for loops (it has 4 nested for loops because the problem only calls for an array with a length of 5, but i want to clean this up so i can have any length). anyone know what i can do?

it might be really obvious but i can't think of anything.

Code:
/**
 *  FewestFactors.java
 *  FewestFactors
 *
 *  Created by destin on 12/27/05.
 */

import java.util.*;

public class FewestFactors {
    public static void main(String args[]) {
        int[] i = { 1, 2, 3, 4 };
        System.out.println(number(i));
    }
	
    static public int number(int[] digits) {
        /** 
         * used to store possible ways (Note: this will always have a size of 
         * (digits.length)!) 
         */
        ArrayList<Integer> ways = new ArrayList<Integer>();
        /** 
         * used to remember which index in the ArrayList 
         * has the least amount of factors
         */
        int leastFactorsNum = 0;
        /** 
         * start with a number higher than any possible factor 
         * (if digit.length is 5, this will be 50000) 
         */
        int leastFactors = (int) (Math.pow(10, digits.length) / 2);

        /** 
         * extremely long (and would be ongoing) nested for loops to 
         * determine amount of possibilities
         *
         * there has to be a better way to do this....
         */
        for (int a = 0; a < digits.length; a++) {
            if (digits.length == 1) {
                ways.add(digits[a]);
            } else {
                for (int b = 0; b < digits.length; b++) {
                    if (digits.length == 2) {
                        if (a != b) {
                            ways.add((10 * digits[a]) + digits[b]);
                        }
                    } else {
                        for (int c = 0; c < digits.length; c++) {
                            if (digits.length == 3) {
                                if ((a != b) && (b != c) && (a != c)) {
                                    ways.add((100 * digits[a]) + (10 * digits[b]) + digits[c]);
                                }
                            } else {
                                for (int d = 0; d < digits.length; d++ ) {
                                    if (digits.length == 4) {
                                        if ((a != b) && (a != c) && (a != d) &&
                                                (b != c) && (b != d) && (c != d)) {
                                            ways.add((1000 * digits[a]) + (100 * digits[b]) + (10 * digits[c]) + digits[d]);
                                        }
                                    } else {
                                        for (int e = 0; e < digits.length; e++) {
                                            if (digits.length == 5) {
                                                if ((a != b) && (a != c) && (a != d) && (a != e) &&
                                                        (b != c) && (b != d) && (b != e) && 
                                                        (c != d) && (c != e) && (d != e)) {
                                                    ways.add((10000 * digits[a]) + (1000 * digits[b]) + (100 * digits[c]) +
                                                                  (10 * digits[d]) + digits[e]);											
                                                }
                                            } else {
                                                return 0;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
		
        /** print out all possible ways */
        for (int i = 0; i < ways.size(); i++) {
            System.out.println("ways[" + i + "]: " + ways.get(i));
        }
		
        /** find all factors of every possibility */
        for (int i = 0; i < ways.size(); i++) {
            int count = 0;
            for (int j = 2; j <= ways.get(i) / 2; j++) {
                if (ways.get( i ) % j == 0) {
                    count++;
                }
            }
			
            /** 
             * if the this number's amount of factors is less than leastFactors 
             * or if they are equal but we found a smaller number
             */
            if ((count < leastFactors) || 
                    ((ways.get(i) < ways.get(leastFactorsNum)) && (count == leastFactors))) {
                leastFactors = count;
                leastFactorsNum = i;
            }
        }
        return (int) ways.get( leastFactorsNum );
    }
}

Last edited by destin; 12-27-2005 at 04:40 PM.
destin is offline   Reply With Quote