Skip to content

Instantly share code, notes, and snippets.

@huwr
Last active August 29, 2015 14:01
Show Gist options
  • Save huwr/083a8bf7754ad6dafae9 to your computer and use it in GitHub Desktop.
Save huwr/083a8bf7754ad6dafae9 to your computer and use it in GitHub Desktop.
Australian TFN validator
#Algorithm for working out the validity of an Australian Tax File Number.
#
#The algorithm is 'public domain', according to an ANAO report in 1999: Management of Tax File Numbers by the Australian National Audit Office, 1999 (ISBN 0 644 38866 8)
#
#See: http://bioinf.wehi.edu.au/folders/fred/tfn.html
#
#An example TFN: 123 456 782
WEIGHTS = [1, 4, 3, 7, 5, 8, 6, 9, 10]
def valid? tfn=""
tfn.gsub!(/[^\d]*/, "")
return false if tfn.empty?
return false if tfn.size != 9
sum = 0
tfn.chars.map(&:to_i).map.with_index{|x,i| x * WEIGHTS[i]}.map{|x| sum += x}
return (sum % 11 == 0)
end
@huwr
Copy link
Author

huwr commented May 15, 2014

You could, therefore, generate a huge list of valid TFN inside a range using something like:

(100000000..1000000000).select{|n| valid? n.to_s}

except it'd take a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment