Tuesday, January 3, 2012

ProjectEuler Problem 8

No posts in a long time, but this will now become a blog about projecteuler :) This problem was very easy to do in C++ (Or any other language). Just throw the big number into a string and get substrings of it. here it is:

#include
#include
using namespace std;

int main() {

    string big_number = "7316717...........52963450";

    unsigned long long biggest = 0;
    unsigned long long test_biggest = 0;

    for(int i=0; i        string convert_me = big_number.substr(i, 5); //Get the substring


        for(int j=0; j<5; j++) {
            if(convert_me[j]-'0' == 0)
                break;

            if(test_biggest == 0) {
                test_biggest = convert_me[j]-'0';
                continue;
            }

            test_biggest *= convert_me[j]-'0';

            if(j==4)
                biggest = ( (test_biggest > biggest) ? test_biggest : biggest);
        }

        test_biggest = 0;
    }

    cout << biggest << endl;
    return 0;
}


time: 0.002s



Optimizations would be simple but not needed as its such a simple problem. (e.g, increment i by 4 if it finds a zero)

      --NotMyFault

1 comment: