# Vampire Numbers

• May 11th, 2012, 09:00 AM
Saintroi
Vampire Numbers
I have to write a program that finds what are called "Vampire Numbers." These are numbers in which the numbers of the number can be rearranged to make a set of numbers that multiply to get that number. For example, 10251 = 51 * 201 and 29632 = 32 * 926.
They can be arranged in any way. The program will be given an input of one integer, then it has to find and print the vampire number that is closest to that integer. for example, if the input was 10, the output would be 126. I have no code on this program yet, because I have no idea where to start really. I can set it up to accept input, but I need help as to making/finding an algorithm that will correctly find the vampire numbers as needed. Thanks if you can help!
• May 11th, 2012, 02:24 PM
KevinWorkman
Re: Vampire Numbers
• May 11th, 2012, 04:13 PM
Saintroi
Re: Vampire Numbers
Alright I guess. But to clarify, I was not asking anyone to do this for me, I wanted a basic starting algorithm or tips. But I guess I will code what I know I can until I get to something I dont know how to do. Thanks.
• May 11th, 2012, 04:20 PM
Norm
Re: Vampire Numbers
It looks like the major part of the assignment is getting the algorithm. Given that the coding shouldn't be too difficult. What algorithm have you come up with?
• May 11th, 2012, 04:45 PM
Saintroi
Re: Vampire Numbers
Yeah that is the major part, I agree. I havent come up with an algorithm yet though, ive set my program up to accept an int and I have a string set to it as well as an int variable, I just dont know where to start with them. I would assume I need a loop somewhere. I am foreseeing a lot of substrings, parsing, and if statements with multiplication. But I just dont know.. I have to find some way to efficiently transverse the int and find somewhere to cut it in half where each side multiplies to equal the base int. Hmm.. I might be getting an idea.. but not sure yet. Because if nothing equals the int, I have to increment the int and do it all over again until I find one.
• May 11th, 2012, 04:55 PM
Norm
Re: Vampire Numbers
Look at generating all possible combinations of the digits in the given number. Then split each number into all the possible two number groups.
• May 11th, 2012, 05:22 PM
Saintroi
Re: Vampire Numbers
Alright sounds good. I just.. ugh.. i'm trying to think of how to start this and ive hit a wall. I guess im gonna go play some games and go to a softball game and see if it hits me. Hopefully by tomorrow i'll have some kind of revolution. Thanks for your help so far.
• May 14th, 2012, 09:01 AM
Saintroi
Re: Vampire Numbers
Well, ive got some code. Which is better than what I had before. But it doesn't really do anything.. I dont know really how to test these things.. Its frustrating.

Code :

```import java.util.*; public class VampireNumbers { private static int getNextVamp(int i) { int a = i;   //Code to find the closest Vampire Number greater than i   return a; }   private static boolean testVamp(int i, String s) { boolean valid = false;   if (i % 100 == 0) { int [] arr = new int [s.length()];   for(int c = 0; i < s.length() - 1; c++) { arr[c] = (int) s.charAt(c); System.out.println(arr[c]); } //code to test if the number is a Vampire Number;   valid = true; }   return valid;   }   public static void main(String [] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number, the nearest vampire number will be returned: "); String num = in.next(); int numb = Integer.parseInt(num);   testVamp(numb, num);   if(testVamp(numb, num)) { System.out.println(numb); }   else { System.out.println(getNextVamp(numb)); }       } }```

You can use the charAt method to pick out each number from an int, correct? I wasnt too sure if that would work.

But again, I need suggestions on what to do algorithm wise. There is nothing on google about this at all. Only stuff about finding all the 4 digit ones. That code doesnt really help me at all. Its different circumstances. I dont know if there is something in java that I dont know about that would help me, or if there is some easy math to do this without testing every possible outcome, or if not then advice on how to go about testing each outcome would be nice. I have what it needs to do in my head, i'm just afraid I dont have the knowledge to put it in code :/

Thanks.
• May 14th, 2012, 09:12 AM
Norm
Re: Vampire Numbers
Quote:

use the charAt method to pick out each number from an int
You can only use methods with objects like a String. int is a primitive.

If you found code that works with 4 digits, can you figure out what its algorithm is and use that for any number of digits?
• May 15th, 2012, 07:56 AM
Saintroi
Re: Vampire Numbers
Yeah thats what I meant. I'm a doofus and put int, but what it is is a String of a number that I need to separate into an array of ints. I think I did it right.

And thats what I thought too, but with most of them they use an algorithm that only works with 4 digit numbers like a*10 + b*10 + c + d or something. Its weird really.
• May 15th, 2012, 08:53 AM
Norm
Re: Vampire Numbers
That formula does make sense. I'd expect something like: a*1000 + b*100 + c*10 + d
Quote:

String of a number that I need to separate into an array of ints
The value of the char '0' is not the int 0. You can get the int value for a numeric char by subtracting '0' from it. For example: '2' - '0' = 2
• May 16th, 2012, 08:55 AM
Saintroi
Re: Vampire Numbers
Hm, well when I put in a number like 126, its printing 126 instead of
1
2
6

like I want it to. Actually I need it to.
I am really stumped on this program. Geez :/
• May 16th, 2012, 09:00 AM
Norm
Re: Vampire Numbers
Can you explain the steps you are doing?
Is the number input as a String: "126" or as an int 126?
Then what do you do with it?
• May 16th, 2012, 04:13 PM
Saintroi
Re: Vampire Numbers
The number is input as a string, then put in a variable as a string. I then make another variable and use parseInt to make it the input, but as an Integer.

Thats all in the main method. I then run another method that has the int and the String as parameters, and returns a boolean that tells whether it is a vampire number or not. Right now I dont have the actual test code for that, but i run an if statement- if(i (the int) % 100 == 0)
that is supposed to just go past numbers with double zeros, because obviously they would be false. Although I dont know if I set that up right, because wouldnt a number with double zeros % 100 be 0? So maybe I should do the whole not equal to thing? But anyway, in that if statement it has an array, which I made outside of the if statement and forgot to mention, in a for loop and it says
arr[c] = s.charAt(c) - '0';

taking the String and separating it into an array. Or so I thought. It then prints each element of the array. It makes sense though, right? I need them to be separated in order to rearrange them I think. But after that it claims the boolean, which was false before the if statement, true, then closes the if statement and returns the boolean.

Back in the main method, if the boolean was true, it prints the number.
else, it prints the result of a method designed to get the next vampire number after the original number. That method has also not been written.

Here is the code if you want to read that and walk through it at the same time:

Code :

```import java.util.*; public class VampireNumbers { private static int getNextVamp(int i) { int a = i;   //Code to find the closest Vampire Number greater than i   return a; }   private static boolean testVamp(int i, String s) { boolean valid = false;   if (i % 100 == 0) { int [] arr = new int [s.length()];   for(int c = 0; i < s.length() - 1; c++) { arr[c] = s.charAt(c) - '0'; System.out.println(arr[c]); } //code to test if the number is a Vampire Number;   valid = true; }   return valid;   }   public static void main(String [] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a number, the nearest vampire number will be returned: "); String num = in.next(); int numb = Integer.parseInt(num);   testVamp(numb, num);   if(testVamp(numb, num)) { System.out.println(numb); }   else { System.out.println(getNextVamp(numb)); }       } }```
• May 16th, 2012, 04:37 PM
Norm
Re: Vampire Numbers
Can you explain why you thought the output of the code should be:
1
2
6

That would require three calls to println.
• May 16th, 2012, 06:02 PM
Saintroi
Re: Vampire Numbers
Yeah I didnt mention did I? I think the it should print 126, I belive, but before that it should have it in that format due to the for loop in the testVamp method, which has a println for each element of the array. I did say that it printed each element though. The array should contain the number, but chopped up.
• May 16th, 2012, 06:47 PM
Norm
Re: Vampire Numbers
How is the search for the algorithm going? Not much you can do until you get that.
• May 16th, 2012, 06:55 PM
Saintroi
Re: Vampire Numbers
Not good. I cant find anything on this subject really. I dont know why. I guess i'll just keep looking. Its hard to find time though.
• May 16th, 2012, 06:58 PM
Norm
Re: Vampire Numbers
Here's the top of the list from Google:
Vampire Numbers
• May 16th, 2012, 07:03 PM
Saintroi
Re: Vampire Numbers
Woah. Your top of the list in Google is a lot better than mine. Lol. I'll check this out and see if it helps. If not, I may just find a list of all the vampire numbers from 1 - 100,000 and say if the number doesnt equal one of those, increment till it does. Thats not exactly right, but maybe my teacher wouldnt notice and id still get credit. Haha. Its just extra credit anyways.
• May 16th, 2012, 07:27 PM
Saintroi
Re: Vampire Numbers
Heck, I cant even find a list like that. I may just skip this one. Hopefully I can get the one I posted about in another thread on here solved. 5 out of 6 isnt bad.