Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Help in writing a better program for Vampire numbers

 
Greenhorn
Posts: 2
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 13286
292
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
moose poop looks like football shaped elk poop. About the size of this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic