Given a message, append checksum digits using prime numbers as weights.
A checksum digit is used as an error-detection method.
Take, for instance, the error-detection method of the EAN-13 code:
The checksum digit is generated by:
- Multiplying each digit in the message alternating by
1
and3
(the weights) - Summing it up
- Calculating the difference between the nearest multiple of 10 (Maximum possible value) and the sum
E.g:
1214563 -> 1*1 + 2*3 + 1*1 + 4*3 + 5*1 + 6*3 + 3*1 = 46
50 - 46 = 4
Although the EAN-13 code can detect transposition errors (i.e. when two adjacent bits are switched, the checksum will be different) it can't detect an error when two bits with the same weight are swapped.
E.g.: 163
-> checksum digit 8
, 361
-> checkdum digit 8
Using a prime checksum, with the first weight being the first prime number, the second weight being the second prime number, etc., errors can be detected when any two bits are swapped.
Because the primes act as weights, you have an extra factor, the length of the message l
:
- Multiply each digit in the message by the
n
th prime (First digit times the first prime, second digit times the second prime, etc.) - Sum it up
- Calculate the sum of each prime up to the
l
th prime multiplied by9
plus1
(Maximum possible value) - Subtract the second sum by the first sum
E.g.:
1214563 -> 1*2 + 2*3 + 1*5 + 4*7 + 5*11 + 6*13 + 3*17 = 225
9*(2+3+5+7+11+13+17) + 1 -> 523
523 - 225 = 298
Rules
- Take a decimal string, leading zeros are prohibited
- Output the checksum digits
- Standard Loopholes apply
- This is code-golf, the shortest answer wins!
Examples
[In]: 213
[Out]: 69
[In]: 89561
[Out]: 132
[In]: 999999
[Out]: 1
[In]: 532608352
[Out]: 534