Results 1 to 19 of 19
Thread: Vampire Numbers
 06292010, 03:31 PM #1
Vampire Numbers
This is an exercise in the book "Thinking in Java" and it says I need to find all the 4 digit Vampire Numbers
I have indeed finished the exercise and produced working code:
Java Code:import java.util.Arrays; class Vampire { public static void main(String[] args) { for(int i=11; i<100; i++) { for(int j=i; j<100; j++) { int k = i * j; String kStr = Integer.toString(k); String checkStr = Integer.toString(i) + Integer.toString(j); //if(kStr.length() != 4) break; char[] kChar = kStr.toCharArray(); char[] checkChar = checkStr.toCharArray(); Arrays.sort(kChar); Arrays.sort(checkChar); boolean isVampire = Arrays.equals(kChar, checkChar); if(isVampire) { System.out.println(i + " * " + j + " = " + k); } } } } }
 06292010, 04:17 PM #2
so 
i = 11
j = 11
k = 121
kStr = 121
checkStr = 1111
if(kStr.length() != 4) break; (Right now that would be false) Whats the point of this line?
shouldn't it be
Java Code:if(kStr.length() != 4){ break; } else { char[] kChar = kStr.toCharArray(); char[] checkChar = checkStr.toCharArray(); Arrays.sort(kChar); Arrays.sort(checkChar); boolean isVampire = Arrays.equals(kChar, checkChar); if(isVampire) { System.out.println(i + " * " + j + " = " + k); }
:rolleyes: ~ Sno ~ :rolleyes:
'~ B.S. Computer Science ~'
 06292010, 04:18 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,651
 Blog Entries
 7
 Rep Power
 21
If you break from the inner loop if the product k doesn't have four digits you may stop the search too early, e.g. 15x15 has only three digits (225); if you break out of the loop you'll never find 15x93 which is a Vampire number. You can break out of the loop when k has more than four digits (which is never going to happen).
kind regards,
Jos
edit: you can start at j=Math.max(1000/i, i)Last edited by JosAH; 06292010 at 04:22 PM.
 06292010, 04:23 PM #4
 06292010, 04:25 PM #5
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,651
 Blog Entries
 7
 Rep Power
 21
 06292010, 04:36 PM #6
Well the way he had it,
I was taught, that if you did not have brackets, it would only effect the line that it is on, and the one below,
two lines below it wouldn't consider it to be part of the if statement.
so I figured his if statement was only affecting the break, and not dealing with anything else.:rolleyes: ~ Sno ~ :rolleyes:
'~ B.S. Computer Science ~'
 06292010, 04:44 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,651
 Blog Entries
 7
 Rep Power
 21
 06292010, 04:51 PM #8
Good thinking! That will certainly eliminate all the 3 Digit results
 06292010, 06:09 PM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,651
 Blog Entries
 7
 Rep Power
 21
 06302010, 11:39 PM #10Member
 Join Date
 Jun 2010
 Posts
 5
 Rep Power
 0
 07012010, 07:33 AM #11Member
 Join Date
 Jun 2010
 Posts
 5
 Rep Power
 0
 07022010, 05:47 AM #12Senior Member
 Join Date
 Mar 2009
 Posts
 552
 Rep Power
 6
 07022010, 12:22 PM #13Member
 Join Date
 Jun 2010
 Posts
 5
 Rep Power
 0
 07022010, 12:53 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,651
 Blog Entries
 7
 Rep Power
 21
 07022010, 03:39 PM #15Member
 Join Date
 Jun 2010
 Posts
 5
 Rep Power
 0
Here it is:
Java Code:public class E10_Vampire { public static void main(String[] args) { int[] startDigit = new int[4]; int[] productDigit = new int[4]; for(int num1 = 10; num1 <= 99; num1++) for(int num2 = num1; num2 <= 99; num2++) { // Pete Hartley's theoretical result: // If x·y is a vampire number then // x·y == x+y (mod 9) if((num1 * num2) % 9 != (num1 + num2) % 9) continue; int product = num1 * num2; startDigit[0] = num1 / 10; startDigit[1] = num1 % 10; startDigit[2] = num2 / 10; startDigit[3] = num2 % 10; productDigit[0] = product / 1000; productDigit[1] = (product % 1000) / 100; productDigit[2] = product % 1000 % 100 / 10; productDigit[3] = product % 1000 % 100 % 10; int count = 0; for(int x = 0; x < 4; x++) for(int y = 0; y < 4; y++) { if(productDigit[x] == startDigit[y]) { count++; productDigit[x] = 1; startDigit[y] = 2; if(count == 4) System.out.println(num1 + " * " + num2 + " : " + product); } } } } } /* Output: 15 * 93 : 1395 21 * 60 : 1260 21 * 87 : 1827 27 * 81 : 2187 30 * 51 : 1530 35 * 41 : 1435 80 * 86 : 6880 *///:~
 07022010, 03:54 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,651
 Blog Entries
 7
 Rep Power
 21
 07022010, 04:14 PM #17Member
 Join Date
 Jun 2010
 Posts
 5
 Rep Power
 0
 08032011, 10:51 AM #18Member
 Join Date
 Aug 2011
 Posts
 1
 Rep Power
 0
 08032011, 12:26 PM #19
Similar Threads

Counting numbers up and down
By radio in forum New To JavaReplies: 4Last Post: 05062011, 03:03 PM 
Sum of 4 numbers...!?
By xmenus in forum New To JavaReplies: 7Last Post: 02142010, 03:36 PM 
How to Spell out numbers?
By syntrax in forum New To JavaReplies: 5Last Post: 10222009, 03:34 PM 
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10142008, 11:46 PM 
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12222007, 02:56 AM
Bookmarks