# Help in writing a better program for Vampire numbers

mohammed amer
Greenhorn
Posts: 2
Hi, new to java programming. I was reading "thinking in java" and there is an assignment where the goal is to find all four digit vampire numbers. I have written a program which does the required task. I was wondering whether there is a better way to program the same. I have used many if statements, can anyone show a more cleaner way with less code. Any help will be greatly appreciated. Thanks in advance.

A vampire number has an even number of digits and is formed by multiplying a pair of numbers containing half the number of digits of the result. The digits are taken from the original number in any order. Pairs of trailing zeroes are not allowed. Examples include:
1260 = 21 * 60
1827 = 21 * 87
2187 = 27 * 81
Write a program that finds all the 4-digit vampire numbers.

solution;::::::::::::::::::::::::::::

Output:----

1260 = 21 * 60
1395 = 15 * 93
1435 = 41 * 35
1530 = 51 * 30
1827 = 21 * 87
2187 = 27 * 81
6880 = 86 * 80

Stephan van Hulst
Bartender
Posts: 6472
83
• 1
Hello Mohammed, welcome to CodeRanch!

Yes, right now you manually rearrange the numbers. Can you imagine what a long boring task that would be if you had to find the vampire numbers of 6 or 8 digits?

Thankfully, you have a computer that can rearrange the digits for you. Think about how you can write a loop that will do this.

Ralph Cook
Ranch Hand
Posts: 479
• 1
An interesting early exercise. My comments are mostly about style, obviously, since you have the code running correctly. Congratulations, by the way.

I have set up VampireNumber to be a class that only holds numbers that have tested to fit that definition. The constructor goes through the test, and throws an exception if the number fails.

Someone interested in compute-time efficiency could well argue that the test should be done outside a constructor; that constructing an object, even partially, is an expensive computing operation that we do not need to do most of the time. If I were interested in compute-time efficiency, I would make a static method that checked whether a number was a vampire, and only construct the object if it were.

I think a static method fits well in that version -- it is directly and only concerned with vampire numbers, but is not needed for an instance of the class.

Any time I see a repeating pattern in code such as your "result = calc(...); if (result == number) {...}", I look for some way to reduce it to its essentials. It reduces code, sometimes dramatically, but more importantly, to me, it allows me to examine patterns in the essentials much more easily. Look at what I've done with that -- I submit that it is far easier to tell, in my version, what combinations of four numbers are submitted to testing than in your version. I think this is the biggest advantage -- not reducing lines just by itself, not any kind of runtime efficiency, but that anyone reading the code can tell more easily what it is doing, at either a detailed level or an overall level.

I put the number beginning your loop into the beginning of your loop. Perhaps you didn't know you could declare the loop variable within the "for" statement. Again, just style, but I think most readers of Java would find this a very little bit more understandable.

Anyway, here is my version. No doubt we can improve it further.

mohammed amer
Greenhorn
Posts: 2
• 1
Thanks a lot Stephan and Ralph for your valuable input on this program.

I have taken your inputs and modified the program. I have used a static method which takes in a number and an array. I use this array to return the two numbers that should be multiplied to get the vampire number. Also i noticed that I was not analyzing all permutations of the number. I was looking at only 12 combinations whereas there were a total of 24 combinations, interestingly the output is still the same. I have written a loop to do the required task. Here is the code. If you have any input on this then you are most welcome.

Thanks again for your prompt replies.

Here is the latest code:;

Output:------

1260 = 21 * 60
1395 = 15 * 93
1435 = 41 * 35
1530 = 51 * 30
1827 = 87 * 21
2187 = 27 * 81
6880 = 86 * 80