Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Or you can use a property of vampire numbers described on <a href="http://users.cybercity.dk/~dsl522332/math/vampires/" rel="noreferrer">this page</a> (linked from Wikipedia) :</p> <blockquote> <p>An important theoretical result found by Pete Hartley:</p> <pre><code> If x·y is a vampire number then x·y == x+y (mod 9) </code></pre> <p>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</p> <p>The solutions to the <a href="http://en.wikipedia.org/wiki/Congruence_relation" rel="noreferrer">congruence</a> are (x mod 9, y mod 9) in {(0,0), (2,2), (3,6), (5,8), (6,3), (8,5)}</p> </blockquote> <p>So your code could look like this :</p> <pre class="lang-c prettyprint-override"><code>for(int i=18; i&lt;100; i=i+9){ // 18 is the first multiple of 9 greater than 10 for(int j=i; j&lt;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&lt;100; i=i+9){ // 11 is the first number greater than 10 which is = 2 mod 9 for(int j=i; j&lt;100; j=j+9){ checkVampire(i,j); } } for(int i=12; i&lt;100; i=i+9){ for(int j=i+3; j&lt;100; j=j+9){ checkVampire(i,j); } } for(int i=14; i&lt;100; i=i+9){ for(int j=i+3; j&lt;100; j=j+9){ checkVampire(i,j); } } // We don't do the last 2 loops, again for symmetry reasons </code></pre> <p>Since they are 40 elements in each of the sets like <code>{(x mod 9, y mod 9) = (0,0); 10 &lt;= x &lt;= y &lt;= 100}</code>, you only do <code>4*40 = 160</code> iterations, when a brute-force gives you 10ˆ4 iterations. You can do even less operations if you take into account the <code>&gt;= 1000</code> constraint, for instance you can avoid checking if <code>j &lt; 1000/i</code>.</p> <p>Now you can easily scale up to find vampires with more than 4 digits =)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload