title: Groovy Turkish tax number verification
body: <p>I need to calculate Turkish Tax number via Groovy.. Basically it's 10 digit number..</p>
  
  <p>Here is the math behind it..</p>
  
  <hr>
  
  <p>First 9 digit number (d), last 10 digit control ( c )value</p>
  
  <p>Tr tax number = d1 d2 d3 d4 d5 d6 d7 d8 d9 c1</p>
  
  <p>So  d1..d9  >> d[i] </p>
  
  <p>p[i] = (d[i] + 10 — i) mod 10</p>
  
  <p>p[i] = 9 => q[i] = 9</p>
  
  <p>p[i] != 9 => q[i] = (p[i] * 2^(10 — i)) mod 9</p>
  
  <p>c1 = (10 — (Σ q[i] mod 10)) mod 10</p>
  
  <p>Let’s say tax number : 018273645x </p>
  
  <p>p[1] = ( d[1] + 10–1 ) mod 10 = (0 + 10–1) mod 10 = 9</p>
  
  <p>p[2] = ( d[2] + 10–2) mod 10 = (1 + 10–2) mod 10 = 9</p>
  
  <p>p[3] = ( d[3] + 10–3 ) mod 10 = (8 + 10–3) mod 10 = 5</p>
  
  <p>p[4] = ( d[4] + 10–4 ) mod 10 = (2 + 10–4) mod 10 = 8</p>
  
  <p>p[5] = ( d[5] + 10–5 ) mod 10 = (7 + 10–5) mod 10 = 2</p>
  
  <p>p[6] = ( d[6] + 10–6 ) mod 10 = (3 + 10–6) mod 10 = 7</p>
  
  <p>p[7] = ( d[7] + 10–7 ) mod 10 = (6 + 10–7) mod 10 = 9</p>
  
  <p>p[8] = ( d[8] + 10–8 ) mod 10 = (4 + 10–8) mod 10 = 6</p>
  
  <p>p[9] = ( d[9] + 10–9 ) mod 10 = (5 + 10–9) mod 10 = 6</p>
  
  <p>q[1] = 9</p>
  
  <p>q[2] = 9</p>
  
  <p>q[3] = (p[3] * 2^(10–3)) mod 9 = (5 * 2⁷) mod 9 = 1</p>
  
  <p>q[4] = (p[4] * 2^(10–4)) mod 9 = (8 * 2⁶) mod 9 = 8</p>
  
  <p>q[5] = (p[5] * 2^(10–5)) mod 9 = (2 * 2⁵) mod 9 = 1</p>
  
  <p>q[6] = (p[6] * 2^(10–6)) mod 9 = (7 * 2⁴) mod 9 = 4</p>
  
  <p>q[7] = 9</p>
  
  <p>q[8] = (p[8] * 2^(10–8)) mod 9 = (6 * 2²) mod 9 = 6</p>
  
  <p>q[9] = (p[9] * 2^(10–9)) mod 9 = (6 * 2¹) mod 9 = 3</p>
  
  <p>i = 1..9  >>    Σ q[i] = 9 + 9 + 1 + 8 + 1 + 4 + 9 + 6 + 3 = 50</p>
  
  <p>c1 = (10 — (Σ q[i] mod 10)) mod 10 = (10–50 mod 10) mod 10</p>
  
  <p>c1 = (10–0) mod 10 = 0</p>
  
  <p>So it would be >> 0182736450</p>
  
  <hr>
  
  <p>Here is my code.</p>
  
  <pre><code>boolean isTurkishVID(String idnum) { 
      d=idnum;
      int v1 = 0;
      int v2 = 0;
      int v3 = 0;
      int v4 = 0;
      int v5 = 0;
      int v6 = 0;
      int v7 = 0;
      int v8 = 0;
      int v9 = 0;
      int v11 = 0;
      int v22 = 0;
      int v33 = 0;
      int v44 = 0;
      int v55 = 0;
      int v66 = 0;
      int v77 = 0;
      int v88 = 0;
      int v99 = 0;
      int v_last_digit = 0;
      int total = 0;
  
  
      if (d.size()==10) {
  
          v1 =  (d[0].toInteger() + 9) % 10;
          v2 =  (d[1].toInteger() + 8) % 10;
          v3 =  (d[2].toInteger() + 7) % 10;
          v4 =  (d[3].toInteger() + 6) % 10;
          v5 =  (d[4].toInteger() + 5) % 10;
          v6 =  (d[5].toInteger() + 4) % 10;
          v7 =  (d[6].toInteger() + 3) % 10;
          v8 =  (d[7].toInteger() + 2) % 10;
          v9 =  (d[8].toInteger() + 1) % 10;
  
  
  
          v11 = (v1 * 512) % 9;
          v22 = (v2 * 256) % 9;
          v33 = (v3 * 128) % 9;
          v44 = (v4 * 64) % 9;
          v55 = (v5 * 32) % 9;
          v66 = (v6 * 16) % 9;
          v77 = (v7 * 8) % 9;
          v88 = (v8 * 4) % 9;
          v99 = (v9 * 2) % 9;
  
          if (v1 != 0 &amp;&amp; v11 == 0) v11 = 9;
          if (v2 != 0 &amp;&amp; v22 == 0) v22 = 9;
          if (v3 != 0 &amp;&amp; v33 == 0) v33 = 9;
          if (v4 != 0 &amp;&amp; v44 == 0) v44 = 9;
          if (v5 != 0 &amp;&amp; v55 == 0) v55 = 9;
          if (v6 != 0 &amp;&amp; v66 == 0) v66 = 9;
          if (v7 != 0 &amp;&amp; v77 == 0) v77 = 9;
          if (v8 != 0 &amp;&amp; v88 == 0) v88 = 9;
          if (v9 != 0 &amp;&amp; v99 == 0) v99 = 9;
  
          total = v11 + v22 + v33 + v44 + v55 + v66 + v77 + v88 + v99;
  
  
          if (total % 10 == 0) total = 0;
          else total = (10 - (total % 10));
  
          if (total == v_last_digit) {    
              return true;
          } else return false;
  
      } else return false;
  
  }
  </code></pre>
  
  <p>It returns error when I use it like</p>
  
  <pre><code>isTurkishVI("1234567891");
  </code></pre>
  
  <p>Returns;</p>
  
  <pre><code>groovy.lang.MissingMethodException: No signature of method: ConsoleScript20.isTurkishVI() is applicable for argument types: (java.lang.Integer) values: [1234567891]
    Possible solutions: isTurkishVID(java.lang.String)
    at ConsoleScript20.run(ConsoleScript20:74)
  </code></pre>