ISBN13 check digit: Difference between revisions
Content deleted Content added
m →{{header|Phix}}: added syntax colouring, marked p2js compatible |
add Red |
||
Line 2,460:
978-2-74839-908-0 : Good
978-2-74839-908-5 : Bad check-digit 5; should be 0
</pre>
=={{header|Red}}==
<lang Red>check_valid_isbn13: function [str] [
is_digit: charset [#"0" - #"9"]
remove-each i str [not pick is_digit i] ; remove non-digits
either 13 = length? str [ ; reject strings of incorrect length
sum: 0
repeat i 13 [
mul: either even? i [3] [1] ; multiplier for odd/even digits
sum: sum + (mul * to integer! to string! pick str i)
]
zero? (sum % 10) ; check if remainder mod 10 is zero
] [
false
]
]
; check given examples
foreach [str] ["978-1734314502" "978-1734314509" "978-1788399081" "978-1788399083"] [
prin str
prin " - "
print check_valid_isbn13 str
]
</lang>
{{out}}
<pre>
978-1734314502 - true
978-1734314509 - false
978-1788399081 - true
978-1788399083 - false
</pre>
|