Jump to content

So, I'm playing with Project Euler and am having trouble with my code. This is not complete, I have yet to venture into actually reading the a file. For now I am just using an ArrayList that holds a few of the words. I am trying to solve Problem 22. I am using Java to solve this. Here is my code:

public static void euler22test(){ //flaw somewhere in this
    ArrayList<String> list = new ArrayList<>(read()); //gets information from file
    java.util.Collections.sort(list);
    int sum = 0;
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //for calculating namescore
    for (int i = 0; i < list.size(); i++){ //for item in list
        String word = list.get(i); //gets string at index i in array
        for (int k = 0; k < word.length(); k++){ //for each letter in word
            int wordSum = 0; //the sum for current word
            char c = word.charAt(k); //gets letter for each index of word
            for (int a = 0; a < alphabet.length(); a++){ //checks what letter it is
                if (c == alphabet.charAt(a)){ //if they are the same
                    wordSum += alphabet.indexOf(c) + 1; //gets place in alphabet
                    sum += wordSum * i; //adds wordSum * place in list to sum.
                }
            }
        }
    }
    System.out.println("Actual: 1720"); //what it should print out
    System.out.println("Calculated: "+sum); //prints 1295
}
public static ArrayList<String> read(){//reads from file
    //currently just returns the below list
    //will eventually read from a file
    ArrayList<String> list = new ArrayList<>();
    list.add("MARY");//5
    list.add("PATRICIA");//6
    list.add("LINDA");//4
    list.add("BARBARA"); //1
    list.add("ELIZABETH");//2
    list.add("JENNIFER");//3
    list.add("MARIA");//4
    return list;
}

 

As the comments say, when ran, this prints out 1290. I've hand calculated that the real value is 1720. Here is my math:

BARBARA
wordSum: 1(2+1+18+2+1+18+1)
sum: 43
ELIZABETH
wordSum: 2(5+12+9+26+1+2+5+20+8)
sum: 219
JENNIFER
wordSum: 3(10+5+14+14+9+6+5+18)
sum: 462
LINDA
wordSum: 4(12+9+14+4+1)
sum: 622
MARIA
wordSum: 5(13+1+18+9+1)
sum: 832
MARY
wordSum: 6(13+1+18+25)
sum: 1174
PATRICIA
wordSum: 7(17+1+20+18+9+3+9+1)
sum: 1720

I can't exactly find the error in the code.

Link to comment
https://linustechtips.com/topic/782428-trouble-with-project-euler/
Share on other sites

Link to post
Share on other sites

Here's a list of my responses:

  • Please fix your post to use code tags. For bonus points, tag me after you've done that.
  • Please tell us the number of the problem you are working on.
  • Please tell us what language you are using.
  • I will not tell you the answer, but I will give you plenty of hints and examples. I hope others that reply to this will follow suit.

 

ENCRYPTION IS NOT A CRIME

Link to post
Share on other sites

15 hours ago, fizzlesticks said:

Please use code tags, fix your text colors (it's currently unreadable on night theme) and a link to the problem you're trying to solve would be helpful.

 

11 hours ago, straight_stewie said:

Here's a list of my responses:

  • Please fix your post to use code tags. For bonus points, tag me after you've done that.
  • Please tell us the number of the problem you are working on.
  • Please tell us what language you are using.
  • I will not tell you the answer, but I will give you plenty of hints and examples. I hope others that reply to this will follow suit.

 

Sorry about those issues, should be fixed now.

Link to post
Share on other sites

sum += wordSum * i; //adds wordSum * place in list to sum.

The index of an arraylist starts at 0, not 1. So you are multiplying the first word in your list by 0, instead of by 1, with this line. 

 

Your calculation is also slightly off, just for 'patricia' since P is the 16th, not 17th letter, of the alphabet. I think it should come out to 1713. 

Link to post
Share on other sites

29 minutes ago, elpiop said:

sum += wordSum * i; //adds wordSum * place in list to sum.

The index of an arraylist starts at 0, not 1. So you are multiplying the first word in your list by 0, instead of by 1, with this line. 

 

Your calculation is also slightly off, just for 'patricia' since P is the 16th, not 17th letter, of the alphabet. I think it should come out to 1713. 

Oh dear god I'm an idiot. Thank you so much! It does come out to 1713.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×