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 19:06

Haha, oh wow. My SEPPLES solution was retarded.

int Projects::EulerProject4() {
    /*
        A palindromic number reads the same both ways.
        The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

        Find the largest palindrome made from the product of two 3-digit numbers.
    */
    int palindrome = 0;
    const int LOOP_MAX = 999;

    for (int loopiter1 = 100; loopiter1 < LOOP_MAX; loopiter1++) {
        for (int loopiter2 = 100; loopiter2 < LOOP_MAX; loopiter2++) {
            int result = loopiter1 * loopiter2;
            int grumblyface = result;
            char len = (result > 99999) ? 6 : 5;
            bool flag = true;
            int* grumblyarray = new int[len];
            for (char b = 0; b < len; b++) {
                int digit = grumblyface % 10;
                grumblyface /= 10;
                grumblyarray[b] = digit;
            }
            for (int i = 0; i < (len / 2); i++) {
                if (grumblyarray[i] != grumblyarray[(len - 1) - i]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                palindrome = result;
            }
            delete grumblyarray;
        }
    }
    return palindrome;
}

Name: Anonymous 2010-05-18 19:13

>>39
'fails at greentext when facing aphostrophes. still thinks he[code]'s a cool guy.

Name: Anonymous 2010-05-18 19:52

>>42
BBCoed fail > missing apostrophe.

Name: Anonymous 2010-05-18 21:37

'implying that there's a valid way to make apostrophes work while in greentext
`implying that using an accent grave isn't the better solution overall

Name: Anonymous 2010-05-19 2:34

>>44
It's not an accent if it isn't combined with another glyph. It's just a backtick.
Your solution breaks on ``proper quotes'', though.

Name: Anonymous 2010-05-19 3:00

>>45
`it works just fine with your ``faggot quotes".

Name: Anonymous 2010-05-19 4:29

>>46
Another victory for ``proper quotes''!

Name: Anonymous 2010-11-26 16:09

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