Last active
December 26, 2015 15:49
-
-
Save AlexanderEkdahl/7175378 to your computer and use it in GitHub Desktop.
Validate swedish personal identity number
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
def validate_personal_identity_number(number) | |
multiplier = 2 | |
sum = 0 | |
number[0..8].each_char do |digit| | |
x = multiplier * digit.to_i | |
if x > 9 | |
sum += x % 10 + 1 | |
else | |
sum += x | |
end | |
multiplier = multiplier == 1 ? 2 : 1 | |
end | |
((sum.to_f / 10).ceil * 10 - sum) % 10 == number[9].to_i | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment