# Java solution to Project Euler Problem 22

Using names.txt (right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 * 53 = 49714.

What is the total of all the name scores in the file?

Running time:

• File IO: 23ms
• 5163 names sorted: 79ms
• Built the list of names: 154ms
• Total runtime: 209ms

Assessment: I broke my time measurements up so I could see how fast each major piece is. I thought that file IO would be the slowest piece of the equation–I don’t have an SSD–but it isn’t. In fact, no single piece is really the bottleneck. At less than 0.3 seconds, this is, for practical purposes, instantaneous.

If I were to do this problem again–probably in C#–I would approach it the same way, but the code would look a bit cleaner, and certainly less verbose. Using higher-level data structures makes this problem pretty simple.

## 1 thought on “Java solution to Project Euler Problem 22”

1. elvirag says:

you could have used to sum the letters their ASCII value: each character minus 48 is the value of the letter. :) cases are tedious if don’t have to use them.