natToSeq: nat -> seqof Digit
natToSeq(n) == if n < 10 then [n] else natToSeq(n div10) ^ [n rem10] measure id;
id: nat -> nat
id(n) == n;
traces /** *Generateallthepossible1,2and3digitsequencesandcheckthattheluhn *calculationcompleteswithoutbreakinganyconstraints.
*/
First1000: let a,b,c,d inset {0,...,9} in
(
luhn([a]);
luhn([a,b]);
luhn([a,b,c]);
luhn([a,b,c,d])
);
/** *TheLuhnalgorithmwilldetectanysingle-digiterror,aswellasalmostall *transpositionsofadjacentdigits.Itwillnot,however,detecttransposition *ofthetwo-digitsequence09to90(orviceversa). * *Seehttp://en.wikipedia.org/wiki/Luhn_algorithm
*/
AllOneDigitErrors: let input = [7,9,9,2,7,3,9,8,7,1] in let pos insetinds input in let replacement inset {0,...,9} \ {input(pos)} in let corrupt = input(1,...,pos-1) ^ [replacement] ^ input(pos+1,...,len input) in
checkFail(corrupt, 3);
AllAdjacentTranspositions: let input = [7,9,9,2,7,3,9,8,7,1] in let pos insetindstl input best-- ie. one less that the length
input(pos+1) <> input(pos) and {input(pos+1), input(pos)} <> {0,9} in let replacement = [input(pos+1), input(pos)] in let corrupt = input(1,...,pos-1) ^ replacement ^ input(pos+2,...,len input) in
checkFail(corrupt, 3);
-- AllTwoDigitErrors: -- let input = [7,9,9,2,7,3,9,8,7,1] in -- let p1, p2 in set inds input in -- let r1 in set {0,...,9} \ {input(p1)} in -- let r2 in set {0,...,9} \ {input(p2)} in -- let first = input(1,...,p1-1) ^ [r1] ^ input(p1+1,...,len input) in -- let second = first(1,...,p2-1) ^ [r2] ^ first(p2+1,...,len first) in -- checkFail(second, 3);
/** *Itwilldetect7ofthe10possibletwinerrors(itwillnotdetect *22<>55,33<>66or44<>77). * *Seehttp://en.wikipedia.org/wiki/Luhn_algorithm
*/
AllTwinErrors: let input = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,0,0] in let pos insetindstl input best input(pos) = input(pos+1) in let rep inset {0,...,9} \ {input(pos)} in let corrupt = input(1,...,pos-1) ^ [rep, rep] ^ input(pos+2,...,len input) in
checkFail(corrupt, 0);
/** *Becausethealgorithmoperatesonthedigitsinaright-to-leftmannerandzero *digitsaffecttheresultonlyiftheycauseshiftinposition,zero-paddingthe *beginningofastringofnumbersdoesnotaffectthecalculation. * *Seehttp://en.wikipedia.org/wiki/Luhn_algorithm
*/
ZeroPadding: let input = [7,9,9,2,7,3,9,8,7,1] in let number inset {1, ..., 10} in let padding = [p-p | p inset {1, ..., number}] in
checkOK(padding ^ input, 3);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.