Last active
August 29, 2015 14:01
-
-
Save huwr/083a8bf7754ad6dafae9 to your computer and use it in GitHub Desktop.
Australian TFN validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.