Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Combinations?!

Name: Anonymous 2010-08-06 16:42

How does one compute them?

Seriously as a newb it's my first time dealing with this problem.

I would appreciate some help.

The problem I was to solve is Euler's Problem 15.

This is my code.

It works like a charm of 2x2 grid.

It gives an out of memory error (even with 3GB of memory dedicated to it) with a 20x20 grid. How I suspect it's more the fault of the String's immutability.


class prob15{
   
        public static String generateCondition(String t, byte x, byte y){
   
    String w = new String("");

    byte k = 0;
    for(byte i = 0; i < t.length(); i++)
        if (t.charAt(i)=='_')
        {
            k = i;
            break;           
        }
       else
           if (i==(t.length()-1))
           w+=t;
   
    if (x<20){
        byte c=x;
        c++;
        String a = t.replaceFirst("_","x");
        w+=" "+generateCondition(a, c, y);
        }

    if (y<20){
        byte d=y;
        d++;
        String a = t.replaceFirst("_","y");
        w+=" "+generateCondition(a, x, d);        
     }

     return w;        
    }
     
   
    public static void main(String args[]){
    String face1 = new String(generateCondition("x_______________________________________",(byte)1,(byte)0));
    String face2 = new String();
    byte counter = 0;
    char last = 'p';
    for (int i = 0; i<face1.length(); i++){
        if (face1.charAt(i)=='x')
        face2+="y";
        else if (face1.charAt(i)=='y')
            face2+="x";
            else face2+=" ";
        if ( (last==' ') && ( (face1.charAt(i)=='y') || (face1.charAt(i)=='x') ) )
        counter++;       
        last = face1.charAt(i);
    }
    System.out.println(face1+face2);
        System.out.println("Count: "+counter*2);
    }

}


I would like an opinion of efficiency please!

And some links to some know methods of computing combinations!

Thanks!

And don't be picking on me. Java is the most powerful language I know. I don't have any time to learn anything else right now.

Thanks!

Name: Anonymous 2010-08-06 21:13


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List