In the times when I was still superstitious I was playing around with the number 13 and came up with a formula to get a new multiple of 13 from an existing multiple of 13. The process of how to do it is described below.

Given a number m = a0 + a1 * 10 + a2 * 102 + … + am * 10m that is a M13 (i.e. m mod 13 = 0), you can apply the following formula to it and get another number n that is also a M13:
n = 2 * ( am + am-1 * 10 + am-2 * 102 + … + a0 * 10m) + 2 * ( a1 + a2 - a4 - a5 + a7 + a8 - … )

The code below - written in Ruby - gets the corresponding n number from an initial m number:

FACTOR = 13

def getNewMultiple(initial)
  result = 2 * initial.to_s.reverse.to_i

  sign = -1
  initial.to_s.each_char.each_with_index do |char, index|
    if index % 3 == 0
      sign = -sign
      next
    end
    result += sign * char.to_i
  end

  raise "#{result} is not multiple of #{FACTOR}" if result % FACTOR > 0
  result
end

Posted in Mathematics and tagged with Prime Numbers.