Results 1 to 19 of 19
Thread: Vampire Numbers
- 06-29-2010, 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:
If you try and run this, it will run perfectly and find all the 4 digit vampire numbers. However, I commented out the line "if(kStr.length() != 4) break;" because if you include that line, it somehow misses some of the vampire numbers. That line of code will supposedly decrease the processing time because it will skip all non-4 digit numbers, because as the exercise asked, I need to find only the 4-digit numbers. Can anyone find out why that line of code doesn't work? Try running the code without that line, then try running it with that line.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); } } } } }
- 06-29-2010, 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 ~-'
- 06-29-2010, 04:18 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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; 06-29-2010 at 04:22 PM.
- 06-29-2010, 04:23 PM #4
- 06-29-2010, 04:25 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 06-29-2010, 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 ~-'
- 06-29-2010, 04:44 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 06-29-2010, 04:51 PM #8
Good thinking! That will certainly eliminate all the 3 Digit results
- 06-29-2010, 06:09 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 06-30-2010, 11:39 PM #10
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
- 07-01-2010, 07:33 AM #11
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
- 07-02-2010, 05:47 AM #12
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
- 07-02-2010, 12:22 PM #13
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
- 07-02-2010, 12:53 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 07-02-2010, 03:39 PM #15
Member
- 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 *///:~
- 07-02-2010, 03:54 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
- 07-02-2010, 04:14 PM #17
Member
- Join Date
- Jun 2010
- Posts
- 5
- Rep Power
- 0
- 08-03-2011, 10:51 AM #18
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
- 08-03-2011, 12:26 PM #19
Similar Threads
-
Counting numbers up and down
By radio in forum New To JavaReplies: 4Last Post: 05-06-2011, 03:03 PM -
Sum of 4 numbers...!?
By xmenus in forum New To JavaReplies: 7Last Post: 02-14-2010, 03:36 PM -
How to Spell out numbers?
By syntrax in forum New To JavaReplies: 5Last Post: 10-22-2009, 03:34 PM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:56 AM


LinkBack URL
About LinkBacks


Bookmarks