Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Wednesday, December 1, 2010

Coding Journey, Euler Problems 16, 20, 25 and 48

So, I worked on Project Euler problems 16, 2025 and 48 over the break.  The entire idea of each of these problems is that the numbers involved get way too big for the program to store, so you have to get creative with the number storage.

I decided to store the obscenely large numbers in an array of integers.  Then, I needed some way to manipulate the array so that we can add to and multiply numbers into it.  All of my solutions to these problems revolve around the following functions:



//assigns source to target
void assign(int target[], int length, int source[])
{
        for(int k = 0; k < length; k++)
        {
                target[k] = source[k];
        }
        return;
}

//Had to alter from problem 48.
void add(int number[], unsigned& lengthNum, int add[], unsigned lengthAdd)
{
        int remainder = 0;
        int temp;
        unsigned k = 0;
        //Adds the arrays together until one or the other array ends
        for(k = 0; k < lengthNum && k < lengthAdd; k++)
        {
                temp = (number[k] + add[k]);
                number[k] = (temp + remainder)%10;
                remainder = (temp + remainder)/10;
        }
        //the following deals with adding the rest of the arrays together once they diverge
        //if lengthAdd ended first
        if(k < lengthNum)
        {
                while(remainder > 0 && k < lengthNum)
                {
                        number[k] = (remainder%10+number[k]);
                        remainder /= 10;
                        k++;
                }
        }
        //if lengthNum ended first
        else if(k < lengthAdd)
        {
                while(remainder > 0 && k < lengthAdd)
                {
                        number[lengthNum++] = (remainder%10+add[k]);
                        remainder /= 10;
                        k++;
                }
        }
        //deals with the remainder
        while(remainder > 0)
        {
                number[lengthNum++] = remainder%10;
                remainder /= 10;
        }
        return;
}

//multiplies the multiplier into the array
void mult(int number[], unsigned& length, const unsigned multiplier)
{
        int temp;
        int remainder = 0;
        //does the multiplying
        for(unsigned k = 0; k < length; k++)
        {
                temp = multiplier*static_cast<int>(number[k]);
                number[k] = (temp+remainder)%10;
                remainder = (temp+remainder)/10;
        }
        //deals with the remainder
        while(remainder > 0)
        {
                number[length++] = remainder%10;
                remainder /= 10;
        }
        return;
}

//adds all the digits in the array together
long sumOfDigits(const int number[], const unsigned length)
{
        long retVal = 0;
        //adds each character together
        for(unsigned k = 0; k < length; k++)
        {
                retVal += static_cast<int>(number[k]);
        }
        return retVal;
}

//prints out the array as a number
void out(int number[], const unsigned length)
{
        cout << "Number: ";
        //prints out each character in reverse order
        for(int k = length - 1; k >= 0; k--)
        {
                cout << static_cast<int>(number[k]);
        }
        cout << endl;
}

I have the individual solutions to the problems, if anyone's interested. Just leave a comment if you have any requests.

Wednesday, November 10, 2010

Coding Journey: Euler Problem 22

Last night I decided that I wanted to install some of my old games on my computer.  I found out that I've lost the file I had saved containing all of the keys to my pc games (all legal games and keys, fyi).  So many keys lost!

I hate drm.  It makes games more trouble than they're worth.  It still angers me to no end that I can't play Half-Life or Portal without an internet connection.

Anyway.

I spent some time last night and this morning working on problem 22 of Project Euler.  It's in C++ because I figured the speed would be an advantage in processing everything.

It took me a while to figure out the answer, mostly because I was mistakenly using the ASCII values of each word in my calculations rather than alphabetic values (e.i. a = 1, b = 2, c = 3, ect).

Once I got it working, I went through and tried to comment most of it, but I have to say this whole program seems clunky to me.  It works, but I think I could've done a better job.  Anyway, here's my solution, for your coding pleasure.