Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: Vampire Numbers

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

    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.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vampire Numbers

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vampire Numbers

    That formula does make sense. I'd expect something like: a*1000 + b*100 + c*10 + d
    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
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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 :/

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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:

    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));
             }
     
     
     
            }
        }

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vampire Numbers

    How is the search for the algorithm going? Not much you can do until you get that.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Vampire Numbers

    Here's the top of the list from Google:
    Vampire Numbers
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

  21. #21
    Member
    Join Date
    Apr 2012
    Posts
    60
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. [METHOD] How: Count how many prime numbers there is between two numbers!
    By Secret20 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 18th, 2011, 02:30 PM
  2. [SOLVED] Scale Numbers from 0.0f to 1.0f
    By techwiz24 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 09:50 PM
  3. Rounding the numbers
    By lakshmivaraprasad in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 2nd, 2011, 12:45 AM
  4. prime numbers
    By tdz013 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 13th, 2011, 11:24 AM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM