Skip to content

Instantly share code, notes, and snippets.

@christianmeichtry
Last active February 19, 2021 02:59
Show Gist options
  • Save christianmeichtry/9348451 to your computer and use it in GitHub Desktop.
Save christianmeichtry/9348451 to your computer and use it in GitHub Desktop.
Calcul du chiffre-clé, modulo 10, récursif BVR Suisse
# Calcul du chiffre-clé, modulo 10, récursif BVR Suisse
# Berechnung der Prüfziffer nach Modulo 10, rekursiv BESR Schweiz
# Calculating the Check Digits Using Module 10, Recursively
def modulo10_checksum(number) # Best to send a string, an integer with leading 0's will be interpreted as octal...
# table from https://www.credit-suisse.com/media/production/pb/docs/unternehmen/kmugrossunternehmen/besr_technische_dokumentation_fr.pdf
table = [
[0,9,4,6,8,2,7,1,3,5],
[9,4,6,8,2,7,1,3,5,0],
[4,6,8,2,7,1,3,5,0,9],
[6,8,2,7,1,3,5,0,9,4],
[8,2,7,1,3,5,0,9,4,6],
[2,7,1,3,5,0,9,4,6,8],
[7,1,3,5,0,9,4,6,8,2],
[1,3,5,0,9,4,6,8,2,7],
[3,5,0,9,4,6,8,2,7,1],
[5,0,9,4,6,8,2,7,1,3]
]
numbers = number.to_s.chars.map(&:to_i) # Map integer to array of digits
report = 0 # Start at row 0
numbers.each do |n|
report = table[report][n] # Traverse table for each digit
end
return (10-report)
end
@kuon
Copy link

kuon commented Nov 18, 2015

La ligne 28 doit être modulo aussi:

return (10 - report) % 10

@omalaspinas
Copy link

Est-ce que vous savez pourquoi on a choisi cette table particulière ?

@loohango
Copy link

loohango commented Feb 19, 2021

Merci pour ces informations très utiles :)
Il est alors encore possible de simplifier un peu avec un tableau à une seule dimension:

def modulo10_checksum(number) # Best to send a string, an integer with leading 0's will be interpreted as octal

table = [0,9,4,6,8,2,7,1,3,5]

numbers = number.to_s.chars.map(&:to_i) # Map integer to array of digits
report = 0 # Start at row 0
numbers.each do |n|
report = table[(report+n)%10] # Traverse table for each digit
end

return (10-report)%10
end

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