Vampire numbers

This is a bit long, but I'd appreciate the help.

I've been messing about with number series, and now I've managed to confuse myself.

Vampire numbers are those numbers that have an even number of digits. The digits can be mixed and split so that each pair of numbers when multiplied together give the vampire number. For example, 1,435 is a vampire number as you can get 35 * 41 from it, so is 2,187 (27 * 81)

I've managed to write a program to work out the vampire numbers for 4 digit numbers, now I'm working on the 6 digit ones. These are going to have 2, 3 digit numbers as the "fangs". Which give this number series its name.

I've got the numbers into a string then split the string into individual digits and put these into an array using

VampLen = LEN(LTRIM$(VampStr$))
FOR Count = 1 TO VampLen
VampAry$(Count) = MID$(VampStr$, Count, 1)
NEXT Count

I used a bunch of SWAP statements to change the order of the array when creating and testing the 2 digit numbers for the 4 figure vampire numbers.

ReCurs = ReCurs + 1

SELECT CASE ReCurs

CASE 1
SWAP VampAry$(1), VampAry$(2)
CASE 2
SWAP VampAry$(1), VampAry$(2)
SWAP VampAry$(3), VampAry$(4)
CASE 3
SWAP VampAry$(1), VampAry$(2)
CASE 4
SWAP VampAry$(1), VampAry$(2)
SWAP VampAry$(2), VampAry$(3)
CASE 5
SWAP VampAry$(3), VampAry$(4)
CASE 6
SWAP VampAry$(1), VampAry$(2)
CASE 7
SWAP VampAry$(3), VampAry$(4)
CASE 8
SWAP VampAry$(1), VampAry$(2)
SWAP VampAry$(2), VampAry$(4)
CASE 9
SWAP VampAry$(3), VampAry$(4)
CASE 10
SWAP VampAry$(1), VampAry$(2)
CASE 11
SWAP VampAry$(3), VampAry$(4)
END SELECT

If I try to do this creating the 2, 3 digit numbers for 6 digit vampire numbers I'm going to end up writing at least 350 swap statements. Can anyone suggest a quicker way of creating all the possible position variations for these numbers? I know I'm going to have to do at least 405,450 calculataions for these numbers, and over 40 million if I extend the program to 8 digit numbers, so any help will be greatly appreciated.

If I've managed to confuse anyone besides myself, think of this. I want all possible positional combinations of the string ABCDEF, such as ACBDEF, ACDBEF etc. etc. For my purposes ABC DEF is the same as DEF ABC but I can take care of the duplications myself.

If you'd like to know more about vampire numbers then see http://www.grenvillecc.ca/faculty/jchilds/vampire.htm and http://www.madras.fife.sch.uk/maths/amazingnofacts/fact020.html

Ray

Comments

  • : This is a bit long, but I'd appreciate the help.
    :
    : I've been messing about with number series, and now I've managed to confuse myself.
    :
    : Vampire numbers are those numbers that have an even number of digits. The digits can be mixed and split so that each pair of numbers when multiplied together give the vampire number. For example, 1,435 is a vampire number as you can get 35 * 41 from it, so is 2,187 (27 * 81)
    :
    : I've managed to write a program to work out the vampire numbers for 4 digit numbers, now I'm working on the 6 digit ones. These are going to have 2, 3 digit numbers as the "fangs". Which give this number series its name.
    :
    : I've got the numbers into a string then split the string into individual digits and put these into an array using
    :
    : VampLen = LEN(LTRIM$(VampStr$))
    : FOR Count = 1 TO VampLen
    : VampAry$(Count) = MID$(VampStr$, Count, 1)
    : NEXT Count
    :
    : I used a bunch of SWAP statements to change the order of the array when creating and testing the 2 digit numbers for the 4 figure vampire numbers.
    :
    : ReCurs = ReCurs + 1
    :
    : SELECT CASE ReCurs
    :
    : CASE 1
    : SWAP VampAry$(1), VampAry$(2)
    : CASE 2
    : SWAP VampAry$(1), VampAry$(2)
    : SWAP VampAry$(3), VampAry$(4)
    : CASE 3
    : SWAP VampAry$(1), VampAry$(2)
    : CASE 4
    : SWAP VampAry$(1), VampAry$(2)
    : SWAP VampAry$(2), VampAry$(3)
    : CASE 5
    : SWAP VampAry$(3), VampAry$(4)
    : CASE 6
    : SWAP VampAry$(1), VampAry$(2)
    : CASE 7
    : SWAP VampAry$(3), VampAry$(4)
    : CASE 8
    : SWAP VampAry$(1), VampAry$(2)
    : SWAP VampAry$(2), VampAry$(4)
    : CASE 9
    : SWAP VampAry$(3), VampAry$(4)
    : CASE 10
    : SWAP VampAry$(1), VampAry$(2)
    : CASE 11
    : SWAP VampAry$(3), VampAry$(4)
    : END SELECT
    :
    : If I try to do this creating the 2, 3 digit numbers for 6 digit vampire numbers I'm going to end up writing at least 350 swap statements. Can anyone suggest a quicker way of creating all the possible position variations for these numbers? I know I'm going to have to do at least 405,450 calculataions for these numbers, and over 40 million if I extend the program to 8 digit numbers, so any help will be greatly appreciated.
    :
    : If I've managed to confuse anyone besides myself, think of this. I want all possible positional combinations of the string ABCDEF, such as ACBDEF, ACDBEF etc. etc. For my purposes ABC DEF is the same as DEF ABC but I can take care of the duplications myself.
    :
    : If you'd like to know more about vampire numbers then see http://www.grenvillecc.ca/faculty/jchilds/vampire.htm and http://www.madras.fife.sch.uk/maths/amazingnofacts/fact020.html
    :
    : Ray
    :
    :

    A suggestion, not an answer:

    Might it be easier to multiply two 3-digit numbers together and see if the result matches?
    [code]
    For I = 100 To 999
    For J = 100 To 999
    Product = 100 * 999
    CheckValues I, J, Product
    Next
    Next
    [/code]
    And then in the sub CheckValues, you can check the numbers to see if they work out:
    [code]
    Public Sub CheckValues(ByVal N1 As Long, ByVal N2 As Long, ByVal Prod As Long)

    Dim t1 As String
    Dim t2 As String
    Dim x As Long
    Dim I As Long
    Dim ER As String
    t1 = CStr(N1) & CStr(N2)
    t2 = CStr(Prod)
    ER = String$(Len(t2), "a")
    I = I + 1
    x = InStr(1, N1, Mid$(N2, I, 1)
    Do
    If X = 0 Then Exit Sub
    Mid$(N1, x, 1) = "a"
    I = I + 1
    If I = 7 Then Exit Sub
    x = InStr(1, N1, Mid$(N2, I, 1)
    Loop Until N1 = ER
    'If it makes it here, then the digits match up. Check other props of numbers

    End Sub
    [/code]
    This is untested code, but it should be close enough. Note also that this is probably far from the best method!

    Oh, one other thing. This is VB code, not QB, so you might have to make a few minor changes. CStr isn't a QB function but this is what it does:
    [code]
    Function CSTR$(Num As Long)

    Dim t As String
    t = STR$(Num)
    If Left$(t, 1) = " " Then
    CSTR$ = Right$(t, Len(t) - 1)
    Else
    CSTR$ = t
    End If

    End Function
    [/code]
  • Thanks for the suggestion KDivad, when I wrote the post I'd let myself become transfixed by the method I'd chosen and couldn't see another way of doing this.

    This started out as a "homework" question posted by someone around a month ago about the Fibonacci numbers in another forum. Since then I've written a program that can calculate (within limits) Primes, Mersenne Primes, Factionals, Perfect, Square and Triangular numbers.

    I've a couple of more number series to go before I either get bored with or complete the project. You can see the program (which I really should really "tidy up") and some of the logic I've used in it at http://members.lycos.co.uk/brisray/qbasic/qnumber.htm

    Ray
  • : Thanks for the suggestion KDivad, when I wrote the post I'd let myself become transfixed by the method I'd chosen and couldn't see another way of doing this.
    :
    I often do that myself. Then someone suggests another way that turns out to be better (I have no idea if my idea actually is better) and I sit there a moment wondering how I missed it...

    : This started out as a "homework" question posted by someone around a month ago about the Fibonacci numbers in another forum. Since then I've written a program that can calculate (within limits) Primes, Mersenne Primes, Factionals, Perfect, Square and Triangular numbers.
    :
    : I've a couple of more number series to go before I either get bored with or complete the project. You can see the program (which I really should really "tidy up") and some of the logic I've used in it at http://members.lycos.co.uk/brisray/qbasic/qnumber.htm
    :
    I glanced at it and it seems like you did a good bit of work. I will probably take a good look at some of the code and see if I can learn anything from the way you handle it.

    Good luck!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories