Skip to content

Instantly share code, notes, and snippets.

@AlexanderEkdahl
Last active December 26, 2015 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexanderEkdahl/7175378 to your computer and use it in GitHub Desktop.
Save AlexanderEkdahl/7175378 to your computer and use it in GitHub Desktop.
Validate swedish personal identity number
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