# ints and chars

posted 4 years ago

i am writing a utility class to help me with project Euler problems. how would you finish this???

as you can see i cannot check if characters.contains(i) since i is an int.

as you can see i cannot check if characters.contains(i) since i is an int.

SCJP

Visit my download page

dennis deems

Ranch Hand

Posts: 808

Matthew Brown

Bartender

Posts: 4566

8

Matthew Brown

Bartender

Posts: 4566

8

posted 4 years ago

ok pseudocode

actually there is an error here. it should be for i= 1; etc.

euler41

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?

the first for loop makes sure there are no repeat digits. the second for loop is supposed to make sure the number contains all the digits from 1 to the length of the string(size of the ArrayList)

actually there is an error here. it should be for i= 1; etc.

euler41

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.

What is the largest n-digit pandigital prime that exists?

the first for loop makes sure there are no repeat digits. the second for loop is supposed to make sure the number contains all the digits from 1 to the length of the string(size of the ArrayList)

SCJP

Visit my download page

posted 4 years ago

Matthew, no. because an 8 digit number could be 12345679 which is not pandigital.

it is probably just a matter of casting but i am not sure.

i am also curious how a set is simpler than a list, but that is just one more thing for me to research

it is probably just a matter of casting but i am not sure.

i am also curious how a set is simpler than a list, but that is just one more thing for me to research

SCJP

Visit my download page

posted 4 years ago

i think i see a solution. if i change my ArrayList to hold ints instead, and convert the chars to ints before storing them. also as Matthew suggested using a Set will be better. i still haven't changed to a Set yet, or tried it, but it compiles. i never would have got away with ArrayList<char> in the first place.

SCJP

Visit my download page

dennis deems

Ranch Hand

Posts: 808

posted 4 years ago

He's not talking about the size of the number. He's talking about the size of the set. Dump all the digits into a Set. If the set contains fewer than 10 elements, the number is not pandigital. No need for contains or any other comparison of characters.

Randall Twede wrote:Matthew, no. because an 8 digit number could be 12345679 which is not pandigital.

He's not talking about the size of the number. He's talking about the size of the set. Dump all the digits into a Set. If the set contains fewer than 10 elements, the number is not pandigital. No need for contains or any other comparison of characters.

posted 4 years ago

no Dennis, 1234 is a pandigital number.

anyway i think i have solved it. i should change ArrayList to a Set though.

anyway i think i have solved it. i should change ArrayList to a Set though.

SCJP

Visit my download page

Matthew Brown

Bartender

Posts: 4566

8

posted 4 years ago

Ah, it's a slightly different definition to pan-digital to the one I linked to. Fair enough. Though you can still avoid checking every value.

Because you can replace this:

With this:

And you can check the return value if you want to be able to return early.

Randall Twede wrote:Matthew, no. because an 8 digit number could be 12345679 which is not pandigital.

Ah, it's a slightly different definition to pan-digital to the one I linked to. Fair enough. Though you can still avoid checking every value.

I am also curious how a set is simpler than a list, but that is just one more thing for me to research

Because you can replace this:

With this:

And you can check the return value if you want to be able to return early.

posted 4 years ago

i got it working using ArrayList, but when i changed to Hashset i get the wrong answer. i cant seem to see the problem right now

the new code allows duplicates

and as you can see with a 9 digit number it returned true instead of false when digits did not contain 9

What is the largest n-digit pandigital prime that exists?";//7652413 or 887645321

the first number is correct the second number is what i get with the new code using a Set

i am pretty sure this is the old code that gave the correct answer.

i changed the local variables to class variables so as not to create so many objects.

i figured it out.

line 16: for(int i = 1; i <= digits.size(); i++)

should be: for(int i = 1; i <= string.length(); i++)

the new code allows duplicates

and as you can see with a 9 digit number it returned true instead of false when digits did not contain 9

What is the largest n-digit pandigital prime that exists?";//7652413 or 887645321

the first number is correct the second number is what i get with the new code using a Set

i am pretty sure this is the old code that gave the correct answer.

i changed the local variables to class variables so as not to create so many objects.

i figured it out.

line 16: for(int i = 1; i <= digits.size(); i++)

should be: for(int i = 1; i <= string.length(); i++)

SCJP

Visit my download page

It is sorta covered in the JavaRanch Style Guide. |