Project Euler

| Sunday, November 16, 2008

Hi all, it's been a while since I've updated, since I've been quite busy. Anyway, I found out about Project Euler through Lifehacker, and it's really challenging and fun. Taken from Project Euler's About page:

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

It's quite good for de-stressing and taking your mind off certain things. So far, I've managed to solve questions 1, 2, 4, 5, 6 and 9. I'm using Java to solve them, and you're definitely free to use whatever you like. Let's take a look at the problems and solutions:

Project Euler Question 1:

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.

Solution:

public class Question1 {
    public static void main(String[] args) {
        int belowNum = 1000;
        int total = 0;
  
        for (int i = 0; i < 1000; i++) {
            if (i % 3 == 0 || i % 5 == 0) {
                total += i;
            }
        }
  
        System.out.println(total);
    }
}

Project Euler Question 2:

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.

Solution:

public class Question2 {
    public static void main(String[] args) {
        int one = 1;
        int two = 2;
        int temp = 0;
        int total = 0;  
  
        while (two < 4000000) {
               if (two % 2 == 0)
                    total += two;
      
            temp = one + two;
            one = two;
            two = temp;              
        }
  
        System.out.println(total);
  
    }

}

Project Euler Question 4:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 x 99.

Find the largest palindrome made from the product of two 3-digit numbers.

Solution:

public class Question4 {
    public static void main(String[] args) {
        int sum = 0;
        int result = 0;
  
        for (int i = 100; i <= 999; i++) {
            for (int j = 100; j <= 999; j++) {
                sum = i * j;
          
                if (isPalindrome(sum) && sum > result) {
                    result = sum;
                }
            }
        }
  
        System.out.println(result);
  
    }

    static boolean isPalindrome(int s) {
        String sum = Integer.toString(s);
        StringBuffer reverseString = new StringBuffer(sum);
        reverseString.reverse();
        String temp = reverseString.toString();
  
        if (sum.equals(temp)) {
            return true;
        }
        else {
            return false;
        }
    }
}

Project Euler Question 5:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

Solution:
public class Question5 {
    public static void main(String[] args) {
  
        // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
        // Elminating some numbers, example: 14 = 7 x 2, we can thus eliminate
        // both 7 and 2. We'll be left with:
        // 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
        // Something tell me I can eliminate 12 and 15 too, but I can't
        // really describe why, so we'll just leave it at that.
        // Start at 20 since. For a number to be divisible by 20, it has to be a multiple of
        // 20. Thus, we can increment by 20;
  
        int start = 20;
        boolean found = false;
  
        while (true && !found) {
            found = true; //assume it's found
      
            for (int i = 11; i <= 20; i++) {
                if (start % i != 0) {
                    found = false;
                    break;
                }
            }
      
            if (found) {
                System.out.println(start);
            }
      
            start += 20;
      
        }  
  
          
    }
}

Project Euler Question 6:

The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 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.

Solution:

public class Question6 {
    public static void main(String[] args) {
        int sumOfSquare = 0;
        int sum = 0;
  
        for (int i = 1; i <= 100; i++) {
            sum += i;      
            sumOfSquare += (i*i);
        }
  
        System.out.println((sum*sum)-sumOfSquare);
    }
}

Project Euler Question 9:

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product abc.

Solution:

public class Question9 {  
    public static void main(String[] args) {
  
        // a^2 + b^2 = c^2
        // a + b + c = 1000
        // Therefore, c = 1000 - a - b
        // a^2 + b^2 = (1000 - a - b)^2
        // Simplifying:
        // b = (500000 - 1000a) / (1000 - a)
        // Therefore, we got to find a values of a and b that satisfy the above.
        // Since total is 1000, a and b must also be less than 1000. Also (1000 - a),
        // Meaning, if a is 1000, there will be a divide-by-zero. Secondly,
        // 500000 - 1000a, thus I assume that a cannot exceed 500.
  
        int b = 0;
        int c = 0;
  
        for (int a = 1; a < 500; a++) {
            b = (500000 - 1000*a) / (1000 - a);
            c = 1000 - a - b;
                              
            if (a*a + b*b == c*c) {
                System.out.println("a: " + a);
                System.out.println("b: " + b);
                System.out.println("c: " + c);
                int product = a*b*c;
                System.out.println("Product: " + product);
                break;
            }
        } 
    }
}

Alright. That's about it. The cool thing about Project Euler is that for some of them, after solving the questions you get access to a PDF file that gives you ideas and MUCH more elegant solutions than what I've done. Also, you get to see the forums for that particular question, and some of the solutions are really, really elegant. It's just too bad that my understanding of Mathematics has yet to reach that level, but it's always good to have a goal =)! On another note, for those of you who are interested, but have no programming background, you may want to try out Small Basic (link to Lifehacker.com, again). Personally, I'd suggest starting out with Java, but well, it's always good to have choices!

No comments: