Monday, August 24, 2009

Project Euler Problem 6

I didn't get this on the first try because I initially tried to use i^2 and sum^2 to perform the squares, and rashly submitted my answer without stepping through using a debugger first.

After I was denied on my first attempt, I replaced i^2 with i*i and sum^2 with sum*sum. I'm probably going to have to start using some sort of numerical package if I continue to program these in Java. Octave (the language I used to do my Numerical Computation assignments at CU) was not listed in the preferred language section of the profile, but since I only need to submit answers for these, I may switch to using Octave soon.

These have been way too easy so far. I'm looking forward to when they get harder.

Solution for Problem 6 below:


/*
* The sum of the squares of the first ten natural numbers is,
* 1^(2) + 2^(2) + ... + 10^(2) = 385
*
* The square of the sum of the first ten natural numbers is,
* (1 + 2 + ... + 10)^(2) = 55^(2) = 3025
*
* Hence the difference between the sum of the squares of the first ten natural
* numbers and the square of the sum is 3025 − 385 = 2640.
*
* Find the difference between the sum of the squares of the first one hundred
* natural numbers and the square of the sum.
*
*/

public class PE0006
{
public static void main(String[] args)
{
int sumOfTheSquares = 0;
int sum = 0;

for (int i = 1; i <= 100; i++)
{
sumOfTheSquares += i * i;
sum += i;
}

System.out.println(sum * sum - sumOfTheSquares);
}
}


On a different note, my Hibernate book is at the Loveland post office and should be delivered soon. I'll probably take a break from Project Euler and make posts related to the things I learn in Hibernate. I know the basics, and can get by with what I know for work, but I'm really interested in learning the more arcane aspects of Hibernate...

Sunday, August 23, 2009

Project Euler Problem 2

Woohoo! I was able to get the second problem right on the first try as well.


/*
* Each new term in the Fibonacci sequence is generated by adding the previous
* two terms. By starting with 1 and 2, the first 10 terms will be:
*
* 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
*
* Find the sum of all the even-valued terms in the sequence which do not
* exceed four million.
*/

public class PE0002
{
public static void main(String[] args)
{
long[] fibonacciSequence = createFibonacciSequence();
long sum = 0;

for (Long currentNumberInSequence : fibonacciSequence)
if (isEven(currentNumberInSequence))
sum += currentNumberInSequence;

System.out.println(sum);
}

private static long[] createFibonacciSequence()
{
long[] fibonacciSequence = new long[33]; // originally set to 100000

fibonacciSequence[0] = 1l;
fibonacciSequence[1] = 2l;

int index = 2;
long currentNumberInSequence = 2l;

while (currentNumberInSequence <= 4000000)
{
fibonacciSequence[index] = fibonacciSequence[index - 1] + fibonacciSequence[index - 2];
currentNumberInSequence = fibonacciSequence[index];
index++;
}

return fibonacciSequence;
}

private static boolean isEven(long number)
{
return number % 2 == 0;
}
}

Saturday, August 22, 2009

Project Euler Problem 1

A colleague of mine (Steve) let me know about a web site known as Project Euler. I signed up today and took a quick crack at problem 1. I knew I could optimize this to iterate through multiples of 3 and 5 instead of iterating through all numbers and checking if each was a multiple of 3 or 5, but I wanted to quickly get an answer so I took the brute-force route, which I won't do in the future.

My solution, which was correct on the first try, is listed below:


/*
* Problem 1
* 05 October 2001
*
* If we list all the natural numbers below 10 that are multiples of 3 or 5, we
* get 3, 5, 6 and 9. The sum of these multiples is 23.
*
* Find the sum of all the multiples of 3 or 5 below 1000.
*
*/

public class PE0001
{
public static void main(String[] args)
{
long sum = 0;
for (int i = 0; i < 1000; i++)
if (isMultipleOf3Or5(i))
sum += i;

System.out.println(sum);
}

private static boolean isMultipleOf3Or5(int i)
{
if (i % 3 == 0 || i % 5 == 0)
return true;
return false;
}
}