Difference between revisions of "Luhn algorithm/fr"
From Free Pascal wiki
Jump to navigationJump to search (Created page with "{{Luhn algorithm}} Cet algorithme a été créé par Hans Peter Luhn. Il est spécifié dans la norme [https://en.wikipedia.org/wiki/ISO/IEC_7812 ISO/IEC 7812-1] et est utili...") |
m (Fixed syntax highlighting) |
||
Line 3: | Line 3: | ||
Cet algorithme a été créé par Hans Peter Luhn. Il est spécifié dans la norme [https://en.wikipedia.org/wiki/ISO/IEC_7812 ISO/IEC 7812-1] et est utilisé par certaines cartes de crédit par exemple. | Cet algorithme a été créé par Hans Peter Luhn. Il est spécifié dans la norme [https://en.wikipedia.org/wiki/ISO/IEC_7812 ISO/IEC 7812-1] et est utilisé par certaines cartes de crédit par exemple. | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang=pascal> |
− | |||
− | |||
function LuhnAlgorithmCheckDigit(const s:string):integer; | function LuhnAlgorithmCheckDigit(const s:string):integer; | ||
var | var | ||
Line 33: | Line 31: | ||
result := i = StrToInt( s[ l ] ); | result := i = StrToInt( s[ l ] ); | ||
end; | end; | ||
− | |||
</syntaxhighlight> | </syntaxhighlight> | ||
− |
Latest revision as of 07:00, 19 February 2020
│
English (en) │
français (fr) │
Cet algorithme a été créé par Hans Peter Luhn. Il est spécifié dans la norme ISO/IEC 7812-1 et est utilisé par certaines cartes de crédit par exemple.
function LuhnAlgorithmCheckDigit(const s:string):integer;
var
l, i, sum, deo : integer;
begin
l := length( s );
sum := 0;
for i := l downto 1 do
begin
if odd(i) then sum := sum + StrToInt(s[i])
else
begin
deo := StrToInt(s[i]) * 2 ;
if deo > 9 then deo := deo mod 10 + 1;
sum := sum + deo;
end;
end;
result := 10 - sum mod 10;
end;
function isLuhnValid( const s : string ): boolean;
var
i, l :integer;
begin
l := length( s );
i := LuhnAlgorithmCheckDigit( copy( s, 1, l-1) );
result := i = StrToInt( s[ l ] );
end;