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

Improving my answer to Euler #4

Name: Sam 2010-05-17 5:57

//The sollution to Project Euler Problem 4
//By Sam

#include <iostream>
#include <math.h>

int reverse(int);
int digitCount(int);
bool palindrome(int);

int main(){

    /* Lazy block comments ;)
    int test;
    while(true){
        std::cout << "Test if palindromic: ";
        std::cin >> test;
        if(palindrome(test))
            std::cout << "This is a palindrome.";
        else
            std::cout << "This is not a palindrome.";
        std::cout << std::endl;
    }
    //*/
   
    int result=0;
    int second=0;
    int highest=0;
    //Loop through all possible numbers that we can times together
    for(int first = 999;first > 100;first--){
        for(second = first;second > 100; second--){
           
            //Find the result
            result = first * second;
            //Record the highest one we get
            if(palindrome(result) && result > highest)
                highest = result;
        }

    }

    std::cout << "The largest palindrome made from the product of two 3-digit numbers is " << highest;

    return 0;
}

bool palindrome(int number){
    //if the number = the reverse number
    return (number == reverse(number));
}

int reverse(int number){
   
    int currentUnit = 0;
    int oppositeUnit = 0;
    int newPosition=0;
    int progressive = 0;
    int finalNumber=0;
    int previous=0;

    //For the program, the number of digits start from an index of 0 hence -1
    int digits = digitCount(number) - 1;

    //Loop through the number backwards
    for(int i = digits;i >= 0;i--){
        //Where the target digit is units wise, and where it should be
        currentUnit = pow(10,i);
        oppositeUnit = pow(10,digits - i);
        //Take crap out from the right
        progressive = floor(number / currentUnit);
        //Take crap out from the left
        newPosition = progressive - (previous * 10);
        //Calculate what we will be taking out next iteration
        previous = progressive;
        //Put isolated number in correct units and add to final number
        finalNumber += newPosition * oppositeUnit;
    }
    return finalNumber;
}

int digitCount(int number){
    //Start the variables, size needs to be larger than 10
    int digits=0;
    float size=10.1;
    //See if dividing by 1, 10, 100.. etc reduces the units of the number
    while(size > 10){
        //If the size is under 10, we know we have hit the end of the number.
        size = number / pow(10,digits);
        digits++;
    }
    return digits;
}

Name: Anonymous 2010-05-18 13:11

>>23
You're a fucking idiot. The real reason my version is slower than your version is because you're comparing an implementation-quality function with the plain algorithm. fucking dumbass CL programmers came out of a university with no real experience and they don't get shit, talk shit and code shit. eat a fucking dick

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