Just to add to what Allen began...

531
 Code:
function a($i)

Dim $,$s,$m,$x,$y,$t,$d,$o[9],$b[9]
$o = 6,2,5,5,4,5,6,4,7,6
$b = "1110111", "0000011", "0111110", "0011111", "1001011", "1011101", "1111101", "1010011", "1111111", "1011111"

For $=1 to 0^$i     ;len($i)
  $s = $s + (2*$o[SubStr($i, $, 1)])
Next
$m = $s/2

Do
   $y = 0

   For $=1 to 0^$i     ;len($i)
      $y = $y + SubStr($i, $, 1)
   Next

   $t = Left($i, Len($i) - Len($y))

   For $=1 to Len($y)
      $s = $s + (2*$o[SubStr($y, $, 1)])

      For $x=0 to 7
         If substr($b[SubStr(Right($i, Len($y)), $, 1)], $x, 1) <> substr($b[SubStr($y, $, 1)], $x, 1)
            $d = 1 + $d
         Endif
      Next
   Next

   For $=1 to 0^$t     ;len($t)
      $d = $d + $o[SubStr($t, $, 1)]
   Next

  $i = $y
Until $i < 10

$a = $s - ($m + $d + $o[$y])

endfunction