| Bytes | Lang | Time | Link |
|---|---|---|---|
| 034 | TIBASIC TI83 Plus | 250523T172801Z | madeforl |
| 069 | Tcl | 250123T172203Z | sergiol |
| 060 | CASIO BASIC CASIO fx9750GIII | 250204T200014Z | madeforl |
| 011 | Japt h | 240607T001110Z | Shaggy |
| 040 | ☾ | 250123T053242Z | Used_Bra |
| 041 | Haskell | 241205T181642Z | matteo_c |
| 034 | Perl 5 MListUtil=sum pl | 241205T165258Z | Xcali |
| 036 | AWK | 241205T153527Z | xrs |
| 092 | Common Lisp | 240609T085909Z | Paul Ric |
| 024 | R | 240607T194936Z | pajonk |
| 008 | Jelly | 240608T154145Z | Jonathan |
| 047 | Python 3.8 | 240607T014259Z | Jonathan |
| 029 | TIBASIC | 240608T110019Z | Sylveste |
| 040 | JavaScript ES6 | 240606T173719Z | Arnauld |
| 058 | Google Sheets | 240608T023550Z | z.. |
| 015 | J | 240608T004757Z | south |
| 031 | Wolfram Language Mathematica | 240607T230203Z | Greg Mar |
| 016 | Charcoal | 240607T222102Z | Neil |
| 042 | Arturo | 240607T175246Z | chunes |
| 042 | Python | 240607T135124Z | Albert.L |
| 008 | 05AB1E | 240607T080842Z | Kevin Cr |
| 008 | Nekomata + e | 240607T015744Z | alephalp |
| 017 | K ngn/k | 240607T020509Z | akamayu |
| 040 | JavaScript Node.js | 240607T014800Z | l4m2 |
| 043 | PowerShell Core | 240606T215038Z | Julian |
| 011 | MATL | 240606T210345Z | Luis Men |
| 051 | C gcc | 240606T221708Z | AZTECCO |
| 023 | APL+WIN | 240606T213104Z | Graham |
| 016 | Uiua SBCS | 240606T205309Z | chunes |
| 060 | Python 3 | 240606T194540Z | corvus_1 |
TI-BASIC (TI-83 Plus), 34 bytes
input as a list
Input A
ʟAcumSum(binomcdf(7,0
ʟA(8)=11fPart(11⁻¹sum(ΔList(cumSum(Ans)-Ans
based off of sylvester's answer in TI-BASIC (TI-84 Plus CE)
explanation
:Input A get input, set it to ʟA
:ʟAcumSum(binomcdf(7,0 multiply list by its weight, set to Ans
ΔList(cumSum(Ans)-Ans remove last element in Ans
sum( sum Ans together
11fPart(11⁻¹ modulo Ans by 11
:ʟA(8)= compare it to the check digit and output truthy or falsy
CASIO BASIC (CASIO fx-9750GIII), 60 bytes
?→Str 1
0
For 1→N To StrLen(Str 1)-1
Ans+NExp(StrMid(Str 1,N,1))
Next
Ans Rmdr 11=Exp(StrRight(Str 1,1))
outputs 1 or 0 if its valid or not (respectively)
Japt -h, 11 bytes
ǶUxÈ*°YÃuB
ǶUxÈ*°YÃuB :Implcit input of digit array U
Ç :Modify last element
¶ : Is it equal to
Ux : The remaining elements of U reduced by addition after
È : Passing each, at 0-based index Y, through the following function
* : Multiply by
°Y : Prefix incremented Y
à : End reduce
u : Modulo
B : 11
:Implicit output of last element
☾, 18 17 15 chars (46 47 40 bytes)
ᴙϜ+⨁⟞₇2∣ᵜ11
(Note: ☾ uses a custom font, you can copy-paste the above code into the Web UI)
Since ascii values are 48 off of their numerical counterpart, the difficulty here is keeping track of what initial value for the accumulate will counteract the continuum of 48's that arise in the list. We initialize the sum with 2 of the check number because the accumulate will give us 8 copies, so to get back to -1 mod 11, we add 2 more copies.
AWK, 36 bytes
{for(;++i<NF;)s+=$i*++x}$0=s%11==$NF
{for(;++i<NF;) # each digit
s+= # sum
$i*++x} # digit times location
$0= # set output
s%11==$NF # sum mod 11 = last digit
Prints 1 if valid or nothing.
Common Lisp, 92 bytes
Input is a list of numbers
(defun p(n)(=(mod(reduce'+(mapcar(lambda(x y)(* x y))n'(1 2 3 4 5 6 7 0)))11)(car(last n))))
R, 28 24 bytes
\(d)sum(d*c(1:7,-1))%%11
Takes input as a vector of digits. Outputs swapped truthy (non-zero) / falsy (zero).
Jelly, 8 bytes
ṖḋJ%11⁼Ṫ
A monadic Link that accepts the list of eight digits and yields 1 (truthy) if a valid PZN or 0 (falsey) otherwise.
Try it online! Or see the test-suite.
How?
Direct implementation of the specification.
ṖḋJ%11⁼Ṫ - Link: list of eight digits, A = [a,b,c,d,e,f,g,h]
Ṗ - pop {A} -> P = [a,b,c,d,e,f,g]
J - indices {A} -> I = [1,2,3,4,5,6,7,8]
ḋ - {P} dot product {I} -> S = 1×a+2×b+3×c+4×d+5×e+6×f+7×g+8×0
%11 - {S} mod 11
Ṫ - tail {A} -> h
⁼ - {S mod 11} equals {h}?
Python 3.8, 50 47 bytes
-1 thanks to l4m2* leading to two more.
Could save at least two bytes by outputting a falsey value for valid and a truthy value for invalid; potentially quite a few using a recursive function.
lambda d,i=0:sum(v*(i:=i+5)for v in d)%11==d[7]
An unnamed function that accepts the list of digits and returns True or False.
How?
Call the weighted sum of the first seven digits \$S\$ and the final digit \$h\$.
A PZN is valid iff \$S \equiv h \mod 11\$
We want an equivalent condition using all eight digits in the sum, i.e. \$S+8h\$, so we can do
for v in d
rather than
for v in d[:7]
So we find \$m\$ such that \$m(S+8h) \equiv h \mod 11\$.
Substituting in \$S \equiv h \mod 11\$ we have \$m(h+8h) \equiv h \mod 11\$, so \$9mh \equiv h \mod 11\$ thus \$9m \equiv 1 \mod 11\$, which is satisfied by \$m=5\$ since \$45 \equiv 1 \mod 11\$.
This gives us
lambda d,i=0:sum(v*(i:=i+1)for v in d)*5%11==d[7]
For 49 bytes, as suggested by l4m2.
Now we can move the multiplication inside our weighted sum to give the code at the top.
My original 50 byte answer
lambda d,i=0:sum(v*(i:=i+1)+3*d[7]for v in d)%11<1
An unnamed function that accepts the list of digits and returns True or False.
Note that \$8\times 3 + 8 = 32 \equiv -1 \mod 11 \$
* arnauld also made a fairly trivial 49er that avoids much real trickery (just make the right multiplier for each digit!):
lambda d,i=1:sum(v*~-((i:=i+1)%9)for v in d)%11<1
TI-BASIC, 29 bytes
Input A
ʟA(8)=remainder(Σ(IʟA(I),I,1,7),11
This program takes an array of digits as inputs, and outputs 1 (for true) or 0 (for false). It uses a summation to multiply the digits by their weights and add them together. It then checks if the result modulo eleven is equal to the last digit.
Example input and output:
JavaScript (ES6), 40 bytes
Expects an array of 8 digits and returns a Boolean value.
a=>!a.reduce(p=>p%11+a[i++]*~(~i%9),i=0)
Commented
a => // a[] = input array
!a.reduce(p => // for each value in a[] with accumulator p:
p % 11 + // reduce p modulo 11
a[i++] * // add the next value from a[] multiplied by
~(~i % 9), // 1 .. 7 for the first 7 elements
// or -1 for the last one
i = 0 // starting with p = 0 and i = 0
) // end of reduce() -> turned into a Boolean
Google Sheets, 58 bytes
=SORT(MOD(A1,10)=MOD(SUM(MID(A1,ROW(1:7),1)*ROW(1:7)),11))
J, 15 bytes
{:=11|1#.7$[*#\
{:=11|1#.7$[*#\
#\ NB. 1,2,3,...,8
* NB. multiplication
[ NB. input digit array
7$ NB. first 7 digits
1#. NB. sum
11| NB. mod 11
= NB. equals
{: NB. last digit of input
Wolfram Language (Mathematica), 31 bytes
Mod[Most@#.Range@7,11]==Last@#&
Try all test cases online! Unnamed function taking a list of eight digits as input and returning True or False.
Charcoal, 16 bytes
⁼﹪Σ⭆…θ⁷×ι⊕κ¹¹﹪Nχ
Try it online! Takes input as a string and outputs a Charcoal boolean, i.e. - for a valid PZN, nothing if not. Explanation:
θ Input string
… Truncated to length
⁷ Literal integer `7`
⭆ Map over characters and join
ι Current character
× Repeated by
κ Current index
⊕ Incremented
Σ Take the digital sum
﹪ Modulo
¹¹ Literal integer `11`
⁼ Is equal to
N Input as a number
﹪ Modulo
χ Predefined variable `10`
Implicitly print
13 bytes by taking input as an array of integers:
⁼⊟θ﹪ΣEθ×ι⊕κ¹¹
Try it online! Link is to verbose version of code. Explanation:
θ Input array
⊟ Remove last element
⁼ Equals
θ Remainder of input array
E Map over elements
ι Current element
× Multiplied by
κ Current index
⊕ Incremented
Σ Take the sum
﹪ Modulo
¹¹ Literal integer `11`
Implicitly print
Arturo, 42 bytes
$->a[0x:∑map a\[0..6]=>[&*<=1+]a\7=x%11]
Explanation
$->a[] ; a function taking a list a
0 ; push zero, our multiplier, to stack
x: ; let x be...
∑ ; sum of...
map a\[0..6]=>[] ; first seven numbers of input mapped to...
& ; current digit...
* ; multiplied by...
<= ; extra copy of...
1+ ; top of stack plus one
a\7= ; is the last number of input equal to...
x%11 ; x modulo eleven?
Python, 42 bytes
lambda d:int(d[6::-1].hex())//.99%11==d[7]
Expects input as a byte string.
How?
This abuses the hex method of bytes objects to implement a poor man's base 100 conversion. The old divide by base-1 trick is then used to get partial sums over the digits (we had to reverse their order for this to come out right). To get the sum over digits we would normally have done mod 99 but as we have to do mod 11 in the end anyway we can save it here.
05AB1E, 8 bytes
¨ƶO11%Qθ
Input as a list of digits.
Try it online or verify all test cases.
Explanation:
¨ # Remove the last digit of the (implicit) input-list
ƶ # Multiply each value by their 1-based index
O # Sum
11% # Modulo-11
Q # Check for each digit in the (implicit) input-list whether it equals this
θ # Pop and only leave the last check
# (which is output implicitly as result)
Nekomata + -e, 8 bytes
7R¢ɔ∙11¦
7R¢ɔ∙11¦
7R [1, 2, 3, 4, 5, 6, 7]
¢ɔ Append 10
∙ Dot product with the input
11¦ Check if the result is divisible by 11
K (ngn/k), 17 bytes
It takes a digit array.
{*|x=11!+/1+&7#x}
7#x First 7 digits
1+& a b c d ... -> 1 for a times, 2 for b times, ...
+/ Inner product between first 7 digits and 1 2 3 4 5 6 7
11! Mod 11
*|x= Same as the last digit?
JavaScript (Node.js), 40 bytes
a=>1>a.reduce((p,v,i)=>p-~i*v,a[7]*2)%11
From Arnauld's. 8th digit * 8 + 8th digit * 2 = 8th digit * (-1), modulo 11
MATL, 12 11 bytes
0&)7:*s11\=
Try it online! Or verify all test cases.
Explanation
% Implicit input: numeric vector
0&) % 2-output indexing (modular, 1-based) with 0: pushes the last element,
% then a vector with the remaining elements
7: % Push [1 2 ... 7]
* % Multiply, element-wise
s % Sum
11\ % Modulo 11
= % Equal?
% Implicit display
C (gcc), 51 bytes
s,i;f(int*l){for(i=s=0;i<7;)s+=*l++*++i;s=s%11-*l;}
function returning true if invalid, false if valid PZN code.
APL+WIN, 23 bytes
Prompts for number as a string of digits.
(7↓n)=11|+/(⍳7)×7↑n←⍎¨⎕


