#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
#

func luhn (n) {
    var chars = n.digits
    var (i, sum) = (0, 0);
    static a = (range(10) -> map {|j| (2*j / 10 -> int) + (2*j % 10 -> int) })
    chars.each { |j|
        sum += (i++ -> is_odd ? a[j] : j)
    }
    return (sum % 10 == 0)
}

assert_eq(luhn(49927398716), true)
assert_eq(luhn(49927398717), false)
assert_eq(luhn(1234567812345678), false)
assert_eq(luhn(1234567812345670), true)

say "** Test passed!"
