Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all the 4 digit vampire numbers

Tags:

algorithm

I am solving a problem to find out all the 4 digit Vampire numbers.

A Vampire Number v=x*y is defined as a number with 'n' even number of digits formed by multiplying a pair of 'n/2'-digit numbers (where the digits are taken from the original number in any order)x and y together. If v is a vampire number, then x&y and are called its "fangs."

Examples of vampire numbers are:

    1.    1260=21*60     2.    1395=15*93     3.    1530=30*51 

I have tried the brute force algorithm to combine different digits of a given number and multiply them together . But this method is highly inefficient and takes up a lot of time.

Is there a more efficient algorithmic solution to this problem?

like image 305
poorvank Avatar asked Jun 27 '13 19:06

poorvank


People also ask

Is 1827 a vampire number?

The sequence of vampire numbers is: 1260, 1395, 1435, 1530, 1827, 2187, 6880, 102510, 104260, 105210, 105264, 105750, 108135, 110758, 115672, 116725, 117067, 118440, 120600, 123354, 124483, 125248, 125433, 125460, 125500, ...

Why is 1395 a vampire number?

Search a number cannot end both in '0'. The first vampire numbers are 1260 = 21 ⋅ 60, 1395 = 15 ⋅ 93, 1435 = 35 ⋅ 41, 1530 = 30 ⋅ 51 and 1827 = 21 ⋅ 87. It can be shown easily that when divided by 9 a vampire number has a remainder equal to 0 or 4.

What does vampire number mean?

In mathematics, a vampire number (or true vampire number) is a composite natural number v, with an even number of digits n, that can be factored into two integers x and y each with n/2 digits and not both with trailing zeroes, where v contains precisely all the digits from x and from y, in any order, counting ...


2 Answers

Or you can use a property of vampire numbers described on this page (linked from Wikipedia) :

An important theoretical result found by Pete Hartley:

      If x·y is a vampire number then x·y == x+y (mod 9)  

Proof: Let mod be the binary modulo operator and d(x) the sum of the decimal digits of x. It is well-known that d(x) mod 9 = x mod 9, for all x. Assume x·y is a vampire. Then it contains the same digits as x and y, and in particular d(x·y) = d(x)+d(y). This leads to: (x·y) mod 9 = d(x·y) mod 9 = (d(x)+d(y)) mod 9 = (d(x) mod 9 + d(y) mod 9) mod 9 = (x mod 9 + y mod 9) mod 9 = (x+y) mod 9

The solutions to the congruence are (x mod 9, y mod 9) in {(0,0), (2,2), (3,6), (5,8), (6,3), (8,5)}

So your code could look like this :

for(int i=18; i<100; i=i+9){          // 18 is the first multiple of 9 greater than 10    for(int j=i; j<100; j=j+9){        // Start at i because as @sh1 said it's useless to check both x*y and y*x        checkVampire(i,j);    } }  for(int i=11; i<100; i=i+9){          // 11 is the first number greater than 10 which is = 2 mod 9    for(int j=i; j<100; j=j+9){        checkVampire(i,j);    } }  for(int i=12; i<100; i=i+9){    for(int j=i+3; j<100; j=j+9){        checkVampire(i,j);    } }  for(int i=14; i<100; i=i+9){    for(int j=i+3; j<100; j=j+9){        checkVampire(i,j);    } }  // We don't do the last 2 loops, again for symmetry reasons 

Since they are 40 elements in each of the sets like {(x mod 9, y mod 9) = (0,0); 10 <= x <= y <= 100}, you only do 4*40 = 160 iterations, when a brute-force gives you 10ˆ4 iterations. You can do even less operations if you take into account the >= 1000 constraint, for instance you can avoid checking if j < 1000/i.

Now you can easily scale up to find vampires with more than 4 digits =)

like image 68
Samuel Peter Avatar answered Sep 21 '22 05:09

Samuel Peter


Iterate over all possible fangs (100 x 100 = 10000 possibilities), and find if their product has the same digits as the fangs.

like image 44
rmmh Avatar answered Sep 19 '22 05:09

rmmh