Project Euler: Problem 45 – Triangular, pentagonal, and hexagonal

Problem 45:

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, …
Pentagonal Pn=n(3n-1)/2 1, 5, 12, 22, 35, …
Hexagonal Hn=n(2n-1) 1, 6, 15, 28, 45, …

It can be verified that T285 = P165 = H143 = 40755.
 
Find the next triangle number that is also pentagonal and hexagonal.

Idea:

Looking at the list of triangle and hexagonal numbers, I made an educated guess that all hexagonal numbers are triangle numbers, so that narrowed down the problem to “Find the next pentagonal number that is also hexagonal”.
 
From there I just generated the next hexagonal number (since it grows faster than pentagonal numbers), and started generating pentagonal numbers until they were no longer smaller than the hexagonal number. If it just so happened that the pentagonal number was equal to the hexagonal number, I found my answer.

String answer = "";

BigInteger pent = new BigInteger("40755");
BigInteger hex = new BigInteger("40755");
int h = 143;
int p = 165;
boolean matches = false;

while(!matches) {
   h++;
   hex = BigInteger.valueOf(h).multiply(
            BigInteger.valueOf(2).multiply(
               BigInteger.valueOf(h)).subtract(BigInteger.ONE));
   while(pent.compareTo(hex) < 0) {
      p++;
      pent = BigInteger.valueOf(p).multiply(
               BigInteger.valueOf(3).multiply(
                  BigInteger.valueOf(p)).subtract(BigInteger.ONE))
                   .divide(BigInteger.valueOf(2));
   }
   if(pent.compareTo(hex) == 0) {
      matches = true;
   }
}

answer = hex.toString();

Project Euler: Problem 42 – Coded triangle numbers

Problem 42:

The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:
 
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
 
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
 
Using words.txt (right click and ‘Save Link/Target As…’), a 16K text file containing nearly two-thousand common English words, how many are triangle words?

Idea:

I started off generating a list of the first 100 triangle numbers (as a guess, not knowing how long the words would be). Noticing that all the words were uppercase, I took advantage of the fact that a character in java can also be represented by an number, so I subtracted the character ‘A’ from each character and added 1. That gave me the alphabetical position of each letter (‘A’ – ‘A’ + 1 = 0 + 1).
 
Calculated the word value for each word and checked to see if that total was a triangle number. If it was, added that to the number of triangle words found.

int answer = 0;

List<String> words = EulerUtils.readFile22("Problem_42.txt");
List<Integer> triangleNumbers = EulerUtils.triangleNumbers(100);
for(String word:words) {
   int sum = 0;
   for(int i = 0; i < word.length(); i++) {
      sum += word.charAt(i)-'A'+1;
   }
   if(triangleNumbers.contains(sum)) {
      answer++;
   }
}

Note: readFile22() is a utility method that reads the comma separated, quoted names into a list. triangleNumbers() generates a list of triangle numbers as defined above.