| Bytes | Lang | Time | Link |
|---|---|---|---|
| 038 | Juby | 250505T153450Z | Jordan |
| 072 | Tcl | 171128T235313Z | sergiol |
| 090 | Tcl | 171129T030354Z | sergiol |
| 071 | Tcl | 250425T230654Z | sergiol |
| 008 | Vyxal | 210528T222327Z | emanresu |
| 006 | 05AB1E | 210528T203130Z | Makonede |
| 037 | Ruby | 181022T143903Z | G B |
| 023 | Perl 6 | 181022T142745Z | nwellnho |
| 021 | Wolfram Language Mathematica | 171115T172902Z | Misha La |
| 017 | Jelly | 180125T013959Z | ellie |
| 011 | Japt | 171116T110312Z | Shaggy |
| 022 | Z80 Assembly | 171123T143641Z | introspe |
| 036 | APL Dyalog Unicode | 171123T221021Z | user5898 |
| 010 | Husk | 171121T195237Z | Mr. Xcod |
| 050 | PHP | 171120T191749Z | Titus |
| 118 | C 98 | 171120T065236Z | Peter Li |
| 014 | Befunge98 PyFunge | 171116T061625Z | MercyBea |
| 016 | GolfScript | 171119T061850Z | Marcos |
| 007 | Jelly | 171118T033100Z | Dennis |
| 042 | R | 171117T192832Z | Mark |
| 015 | K oK | 171116T084758Z | mkst |
| 063 | C | 171116T203229Z | micheal6 |
| 051 | C gcc | 171117T112111Z | PlatinTa |
| 062 | C# .NET Core | 171117T063813Z | Ayb4btu |
| 045 | Acc!! | 171117T090254Z | DLosc |
| 052 | QBasic | 171117T084650Z | DLosc |
| 029 | Julia | 171117T042657Z | Frames C |
| nan | 171117T040242Z | user1893 | |
| 084 | ><> | 171117T022632Z | Bolce Bu |
| 009 | Pyke | 171115T171420Z | Blue |
| 052 | GolfScript | 171116T213416Z | FedeWar |
| 022 | Retina | 171115T230755Z | Leo |
| 228 | Brainfuck | 171116T203056Z | Bolce Bu |
| 055 | Java 8 | 171116T083923Z | Kevin Cr |
| 018 | TIBasic 83 series | 171116T184638Z | Misha La |
| 038 | JavaScript ES6 | 171115T162454Z | Rick Hit |
| 041 | Clojure | 171116T163640Z | NikoNyrh |
| 054 | C gcc | 171115T161138Z | pizzapan |
| 041 | Ruby | 171115T185916Z | displayn |
| 038 | Haskell | 171115T185827Z | butterdo |
| 017 | J | 171115T160910Z | Jonah |
| 037 | Excel | 171116T141516Z | Wernisch |
| 009 | 05AB1E | 171116T140923Z | kalsower |
| 321 | Perl 5 | 171116T095938Z | Nahuel F |
| 014 | APL Dyalog | 171116T091759Z | Adá |
| 032 | MATLAB/Octave | 171116T085538Z | Tom Carp |
| 082 | PHP | 171116T054734Z | Jo. |
| 010 | MATL | 171115T160047Z | Luis Men |
| 047 | JavaScript Node.js | 171115T202219Z | Mr. Xcod |
| 008 | Jelly | 171115T164425Z | Mr. Xcod |
| 008 | Pyth | 171115T160953Z | Mr. Xcod |
| 029 | Python 2 | 171115T153752Z | Halvard |
| nan | Perl 5 | 171115T185337Z | Xcali |
| 023 | QBIC | 171115T174440Z | steenber |
| 014 | 05AB1E | 171115T154047Z | Erik the |
| 040 | Octave | 171115T173424Z | rahnema1 |
| 099 | Java OpenJDK 8 | 171115T162920Z | Roberto |
| 016 | Jelly | 171115T153124Z | hyperneu |
| 088 | Batch | 171115T162755Z | Neil |
| 077 | C | 171115T153923Z | Steadybo |
| 085 | PowerShell | 171115T155035Z | AdmBorkB |
Tcl, 72 bytes
proc C L {expr -([lmap c $L {list +$c*([incr i]%8?$i%2?3:1:0)}])%10==$c}
Tcl, 90 bytes
proc C s {expr ([regsub -all (.)(.) [string ra $s 0 6]0 {-3*\1-\2}])%10==[string in $s 7]}
Will golf it more later!
Tcl, 71 bytes
proc C L {expr -([lmap a\ b $L {list +3*$a+([incr i]%4?$b:0)}])%10==$b}
Vyxal, 8 bytes
y$3*J∑₀Ḋ
y # Uninterleave
$ # Swap
3* # Multiply by 3 (vectorised)
J # Join
∑ # Sum
₀Ḋ # Is divisible by 10?
05AB1E, 6 bytes
ιO3βTÖ
Try it online! Beats all other answers. Takes input as a list of digits with leading zeros.
ιO3βTÖ # full program
Ö # is...
# (implicit) list of...
O # sum...
# (implicit) s...
O # of...
# (implicit) each element of...
β # list of base-10 values of base...
3 # literal...
β # digits of...
# implicit input...
ι # split into pieces of...
# (implicit) 2...
ι # with each digit of each element concatenated with digits in corresponding indices in other elements...
β # in decimal...
Ö # divisible by...
T # 10...
Ö # ?
# implicit output
Wolfram Language (Mathematica), 26 21 bytes
10∣(2-9^Range@8).#&
Takes input as a list of 8 digits.
How it works
2-9^Range@8 is congruent modulo 10 to 2-(-1)^Range@8, which is {3,1,3,1,3,1,3,1}. We take the dot product of this list with the input, and check if the result is divisible by 10.
Wolfram Language (Mathematica), 33 bytes and non-competing
Check[#~BarcodeImage~"EAN8";1,0]&
Takes input as a string. Returns 1 for valid barcodes and 0 for invalid ones.
How it works
The best thing I could find in the way of a built-in (since Mathematica is all about those).
The inside bit, #~BarcodeImage~"EAN8";1, generates an image of the EAN8 barcode, then ignores it entirely and evaluates to 1. However, if the barcode is invalid, then BarcodeImage generates a warning, which Check catches, returning 0 in that case.
Jelly, 17 bytes
Ṗµ3,1ṁ×⁸S⁵a%⁵⁼³Ṫ¤
Explanation:
Ṗµ take off the last element
3,1ṁ repeat [3, 1] to the same length as the input without the last element
×⁸ multiply together
S sum
⁵a abs(10 - sum)
%⁵ mod 10
³Ṫ¤ the last element of the original input
⁼ are they equal?
Z80 Assembly, 22 bytes
; INPUT:
; HL = a memory location storing barcode as a sequence of integers
; OUTPUT:
; flag Z signals that barcode is valid, NZ signals invalid barcode
CheckBarcode:
xor a : ld bc,7*256+%10101010 ; B is the number of loop iterations
; bits of C say when factor 3 is to be used
AddLoop:
sub (hl) : rl c : jr nc,Times1 ; negative checksum computation
Times3: sub (hl) : sub (hl)
Times1: inc hl : djnz AddLoop
jr z,Check ; the ugly line to deal with 00000000
Rem: add 10 : jr nc,Rem ; add 10s until the answer is non-negative...
Check: cp (hl) : ret ; ...and compare it with 8th digit!
This task fits into 8-bit assembly quite well.
APL (Dyalog Unicode), 36 bytes
f←{0=10∣+/⍵+2×+/⍵×8⍴0 1}
I just copy and modify (because that formula it seems wrong) the above Excel solution....
PHP, 50 bytes
while($c++<8)$s+=$argv[$c]*($c&1?3:1);echo$s%10<1;
Run with -nr, provide digits as separate command line arguments
or try it online.
C 98, 118 bytes
Ive edited this now to instead of echoing F or T it will return 0 or 1 to the pipe.
int i,j,k;int main(int,char**v){for(;i<7;(j+=(v[1][i]-48)*(i++%2?1:3)));for(;(k+=10)<j;);return v[1][7]-48==(k-j)%10;}
usage:
./a.out XXXXXXXX
where XXXXXXXX is a zero padded string, it will return either a 0 or a 1 as an exit code
this is 118 chars long in total. and the the compiled program is
text data bss dec hex filename
1307 544 16 1867 74b a.out
Befunge-98 (PyFunge), 16 14 bytes
Saved 2 bytes by skipping the second part using j instead of ;s, as well as swapping a ~ and + in the first part to get rid of a + in the second.
~3*+~+6jq!%a+2
Input is in 8 digits (with leading 0s if applicable) and nothing else.
Outputs via exit code (open the debug dropdown on TIO), where 1 is true and 0 is false.
Explanation
This program uses a variety of tricks.
First of all, it takes the digits in one by one through their ASCII values. Normally, this would require subtracting 48 from each value as we read it from the input. However, if we don't modify it, we are left with 16 (3+1+3+1+3+1+3+1) extra copies of 48 in our sum, meaning our total is going to be 768 greater than what it "should" be. Since we are only concerned with the sum mod 10, we can just add 2 to the sum later. Thus, we can take in raw ASCII values, saving 6 bytes or so.
Secondly, this code only checks if every other character is an EOF, because the input is guaranteed to be only 8 characters long.
Thirdly, the # at the end of the line doesn't skip the first character, but will skip the ; if coming from the other direction. This is better than putting a #; at the front instead.
Because the second part of our program is only run once, we don't have to set it up so that it would skip the first half when running backwards. This lets us use the jump command to jump over the second half, as we exit before executing it going backwards.
Step by step
Note: "Odd" and "Even" characters are based on a 0-indexed system. The first character is an even one, with index 0.
~3*+~+ Main loop - sum the digits (with multiplication)
~ If we've reached EOF, reverse; otherwise take char input. This will always
be evenly indexed values, as we take in 2 characters every loop.
3*+ Multiply the even character by 3 and add it to the sum.
~ Then, take an odd digit - we don't have to worry about EOF because
the input is always 8 characters.
+ And add it to the sum.
6j Jump over the second part - We only want to run it going backwards.
q!%a+2 The aftermath (get it? after-MATH?)
+2 Add 2 to the sum to make up for the offset due to reading ASCII
%a Mods the result by 10 - only 0 if the bar code is valid
! Logical not the result, turning 0s into 1s and anything else into 0s
q Prints the top via exit code and exits
GolfScript, 16 bytes
0\~{\3*++}4*10%!
Example Input
2 0 3 7 8 2 4 0
Output:
1
How it Works
Takes a string of 8 digits separated by spaces. For convenience I have included a header which automatically adds spaces to the input.
0\ - Add zero to stack and place under input
~ - Dump digits in input string onto stack
{ }4* - Loop 4 times.
\ - Swap top two indices of stack
3* - Multiply by three
++ - Add twice
10% - Modulo 10
! - Logical(?) not
Jelly, 7 bytes
s2Sḅ3⁵ḍ
How it works
s2Sḅ3⁵ḍ Main link. Argument: [a,b,c,d,e,f,g,h] (digit array)
s2 Split into chunks of length 2, yielding [[a,b], [c,d], [e,f], [g,h]].
S Take the sum of the pairs, yielding [a+c+e+g, b+d+f+h].
ḅ3 Convert from ternary to integer, yielding 3(a+c+e+g) + (b+d+f+h).
⁵ḍ Test if the result is divisible by 10.
R, 45 bytes 42 bytes
x=scan();(140-sum(x[-8]*c(3,1)))%%10==x[8]
Explanation:
140- #the largest value possible is 135 (all 9's)
sum(
x[-8] #drop last value from input vector
*c(3,1) #multiply by a automatically replicating vector of 3,1
)
%%10 #modulo 10
== x[8] #compare to the checksum
K (oK), 16 15 bytes
Solution:
{~10!+/x*8#3 1}
Examples:
{~10!+/x*8#3 1}2 1 0 3 4 9 8 5
1
{~10!+/x*8#3 1}2 1 0 3 4 9 8 4
0
{~10!+/x*8#3 1}2 0 3 7 8 2 4 0
1
Explanation:
K is interpreted right-to-left:
{~10!+/x*8#3 1} / the solution
{ } / lambda with x as implicit input
3 1 / the list [3,1]
8# / 8 take this list = [3,1,3,1,3,1,3,1]
x* / multiply input by this
+/ / sum over the list
10! / 10 mod this sum
~ / not, 0 => 1, anything else => 0
C, 63 bytes
i;s=0;c(int*v){for(i=0;i<8;i++){s+=v[i]*3+v[++i];}return s%10;}
Assumes that 0 is true and any other value is false.
+3 bytes for better return value
i;s=0;c(int*v){for(i=0;i<8;i++){s+=v[i]*3+v[++i];}return s%10==0;}
Add ==0 to the return statement.
Ungolfed
int check(int* values)
{
int result = 0;
for (int index = 0; index < 8; index++)
{
result += v[i] * 3 + v[++i]; // adds this digit times 3 plus the next digit times 1 to the result
}
return result % 10 == 0; // returns true if the result is a multiple of 10
}
This uses the alternative definition of EAN checksums where the check digit is chosen such that the checksum of the entire barcode including the check digit is a multiple of 10. Mathematically this works out the same but it's a lot simpler to write.
Initialising variables inside loop as suggested by Steadybox, 63 bytes
i;s;c(int*v){for(i=s=0;i<8;i++){s+=v[i]*3+v[++i];}return s%10;}
Removing curly brackets as suggested by Steadybox, 61 bytes
i;s;c(int*v){for(i=s=0;i<8;i++)s+=v[i]*3+v[++i];return s%10;}
Using <1 rather than ==0 for better return value as suggested by Kevin Cruijssen
i;s=0;c(int*v){for(i=0;i<8;i++){s+=v[i]*3+v[++i];}return s%10<1;}
Add <1 to the return statement, this adds only 2 bytes rather than adding ==0 which adds 3 bytes.
C (gcc), 54 by pizzapants184 51 bytes
c;i;f(x){for(i=3,c=0;x;x/=10)c+=(i^=2)*x;c=c%10<1;}
This answer heavily builds upon the answer of pizzapants184, but improves the bytecount by 3. i would have commented, but rep is below 50. The 3 bytes are removed, by replacing i=0 (...) (1+2*i++%4) with i=3 (...) (i^=2).
C# (.NET Core), 65 62 bytes
b=>{int s=0,i=0,t=1;while(i<8)s+=b[i++]*(t^=2);return s%10<1;}
Acknowledgements
-3 bytes thanks to @KevinCruijssen and the neat trick using the exclusive-or operator.
DeGolfed
b=>{
int s=0,i=0,t=1;
while(i<8)
s+=b[i++]*(t^=2); // exclusive-or operator alternates t between 3 and 1.
return s%10<1;
}
C# (.NET Core), 53 bytes
b=>(3*(b[0]+b[2]+b[4]+b[6])+b[1]+b[3]+b[5]+b[7])%10<1
A direct port of @Snowman's answer.
Acc!!, 60 45 bytes
3*N+N+3*N+N+3*N+N+3*N+N-8
Write 1/(1+_%10)+48
Explanation
We read eight digits, multiplying the appropriate ones by 3, and add. It's not quite that simple, because what we're actually reading is ASCII codes, and so each digit has 48 added to it. Thus, the resulting sum is too big by 3*48*4 + 48*4 = 768. We only care about the 1's digit, so subtracting 8 is sufficient to get the correct result.
We want to print 1 if the accumulator mod 10 is 0, and 0 otherwise. Acc!! doesn't have comparison operators, so we have to use integer division:
_%10 0 1 9
1+_%10 1 2 10
1/(1+_%10) 1 0 0
Then we add 48 to get an ASCII value and Write it.
Original 60-byte solution with a loop:
Count i while 8-i {
_+(N-48)*(3-i%2*2)
}
Write 1/(1+_%10)+48
QBasic, 54 52 bytes
Ugh, the boring answer turned out to be the shortest:
INPUT a,b,c,d,e,f,g,h
?(3*a+b+3*c+d+3*e+f+3*g+h)MOD 10=0
This inputs the digits comma-separated. My original 54-byte solution, which inputs one digit at a time, uses a "nicer" approach:
m=3
FOR i=1TO 8
INPUT d
s=s+d*m
m=4-m
NEXT
?s MOD 10=0
Julia 29 bytes
~x=[3,1,3,1,3,1,3,1]⋅x%10<1
golfing away that long literal array would be nice.
⋅ is a 3 byte character.
But it is shorter than calling dot(...,x).
and shorter that anything that handles everything as arrays and then grabs the only element.
Java 8, 53 bytes
Golfed:
b->(3*(b[0]+b[2]+b[4]+b[6])+b[1]+b[3]+b[5]+b[7])%10<1
Direct calculation in the lambda appears to the shortest solution. It fits in a single expression, minimizing the lambda overhead and removing extraneous variable declarations and semicolons.
public class IsMyBarcodeValid {
public static void main(String[] args) {
int[][] barcodes = new int[][] { //
{ 2, 0, 3, 7, 8, 2, 4, 0 }, //
{ 3, 3, 7, 6, 5, 1, 2, 9 }, //
{ 7, 7, 2, 3, 4, 5, 7, 5 }, //
{ 0, 0, 0, 0, 0, 0, 0, 0 }, //
{ 2, 1, 0, 3, 4, 9, 8, 4 }, //
{ 6, 9, 1, 6, 5, 4, 3, 0 }, //
{ 1, 1, 9, 6, 5, 4, 2, 1 }, //
{ 1, 2, 3, 4, 5, 6, 7, 8 } };
for (int[] barcode : barcodes) {
boolean result = f(b -> (3 * (b[0] + b[2] + b[4] + b[6]) + b[1] + b[3] + b[5] + b[7]) % 10 < 1, barcode);
System.out.println(java.util.Arrays.toString(barcode) + " = " + result);
}
}
private static boolean f(java.util.function.Function<int[], Boolean> f, int[] n) {
return f.apply(n);
}
}
Output:
[2, 0, 3, 7, 8, 2, 4, 0] = true
[3, 3, 7, 6, 5, 1, 2, 9] = true
[7, 7, 2, 3, 4, 5, 7, 5] = true
[0, 0, 0, 0, 0, 0, 0, 0] = true
[2, 1, 0, 3, 4, 9, 8, 4] = false
[6, 9, 1, 6, 5, 4, 3, 0] = false
[1, 1, 9, 6, 5, 4, 2, 1] = false
[1, 2, 3, 4, 5, 6, 7, 8] = false
><>, 89 84 Bytes
8>i$1\
/\?:-/
\8+ \
/1-:?v~r3*+\
\ } \$>3*+v<
/++-/f} l
\+3ff/\?-4/
/a+++/
\%0=n;
Can probably certainly be improved (this is my second ><> program)
How it works:
8>i$1\
\?:-/
Gets user input, 8 times. Note that since i pushes -1 if the input stack is empty, this may not always return false for inputs shorter than 8 digits.
/
\8+ \
/1-:?v
\ } \$
/++-/f
\+3ff/
Converts the ascii values to their actual values.
~r3*+\
>3*+v<
} l
\?-4/
Remove the counter, and reverse the stack. Then, while the stack is longer than 4, multiply the top value by three, add it to the digit below it, and move the result to the bottom of the stack.
/
/a+++/
\%0=n;
Sum the values on the stack. If the answer is divisible by 10, the code is valid. If so, print 1, otherwise, 0.
Pyke, 14 13 12 9 bytes
2%2*+sT%!
2% - input[::2]
2* - ^ * 2
+ - input + ^
s - sum(^)
T% - ^ % 10
! - not ^
Thanks Mr. Xcoder for saving 3 bytes!
GolfScript - 54 52 bytes
1/[{~}/][):a;[3 1]3*[3]+]zip 0\{{*}*+}/10%10\- 10%a=
- Input:
33765129 - Output:
1
If the input is an array the code can be modified to:
~[):a;[3 1]3*[3]+]zip 0\{{*}*+}/10%10\- 10%a= # 45 bytes
- Input:
[3 3 7 6 5 1 2 9] - Output:
1
Explanation
1/[{~}/] # Parse input
[):a; # Gets last digit
[3 1]3*[3]+ # Generates the vector [3 1 3 1 3 1 3]
]zip # Transposes the vector [[input] [3 1s]]
0\{{*}*+}/ # Vector dot product
10%10\- 10% # (10-ans mod10)mod10
a= # Compare
Retina, 23 22 bytes
-1 byte thanks to Martin Ender!
(.).
$1$1$&
.
$*
M`
1$
Explanation
Example input: 20378240
(.).
$1$1$&
Replace each couple of digits with the first digit repeated twice followed by the couple itself.
We get 2220333788824440
.
$*
Convert each digit to unary. With parentheses added for clarity, we get (11)(11)(11)()(111)(111)...
M`
Count the number of matches of the empty string, which is one more than the number of ones in the string. (With the last two steps we have basically taken the sum of each digit +1) Result: 60
1$
Match a 1 at the end of the string. We have multiplied digits by 3 and 1 alternately and summed them, for a valid barcode this should be divisible by 10 (last digit 0); but we also added 1 in the last step, so we want the last digit to be 1. Final result: 1.
Brainfuck, 228 Bytes
>>>>++++[<++>-]<[[>],>>++++++[<++++++++>-]<--[<->-]+[<]>-]>[->]<<<<[[<+>->+<]<[>+>+<<-]>>[<+>-]<<<<<]>>>>[>>[<<[>>+<<-]]>>]<<<++++[<---->-]+++++[<++<+++>>-]<<[<[>>[<<->>-]]>[>>]++[<+++++>-]<<-]<[[+]-<]<++++++[>++[>++++<-]<-]>>+.
Can probably be improved a fair bit. Input is taken 1 digit at a time, outputs 1 for true, 0 for false.
How it works:
>>>>++++[<++>-]<
Put 8 at position 3.
[[>],>>++++++[<++++++++>-]<--[<->-]+[<]>-]
Takes input 8 times, changing it from the ascii value to the actual value +2 each time. Inputs are spaced out by ones, which will be removed, to allow for easier multiplication later.
>[->]
Subtract one from each item. Our tape now looks something like
0 0 0 0 4 0 4 0 8 0 7 0 6 0 2 0 3 0 10 0 0
^
With each value 1 more than it should be. This is because zeros will mess up our multiplication process.
Now we're ready to start multiplying.
<<<<
Go to the second to last item.
[[<+>->+<]<[>+>+<<-]>>[<+>-]<<<<<]
While zero, multiply the item it's at by three, then move two items to the left. Now we've multiplied everything we needed to by three, and we're at the very first position on the tape.
>>>>[>>[<<[>>+<<-]]>>]
Sum the entire list.
<<<++++[<---->-]
The value we have is 16 more than the actual value. Fix this by subtracting 16.
+++++[<++<+++>>-]
We need to test whether the sum is a multiple of 10. The maximum sum is with all 9s, which is 144. Since no sum will be greater than 10*15, put 15 and 10 on the tape, in that order and right to the right of the sum.
<<[<[>>[<<->>-]]>[>>]++[<+++++>-]<<-]
Move to where 15 is. While it's non-zero, test if the sum is non-zero. If it is, subtract 10 from it. Now we're either on the (empty) sum position, or on the (also empty) ten position. Move one right. If we were on the sum position, we're now on the non-zero 15 position. If so, move right twice. Now we're in the same position in both cases. Add ten to the ten position, and subtract one from the 15 position.
The rest is for output:
<[[+]-<]<++++++[>++[>++++<-]<-]>>+.
Move to the sum position. If it is non-zero (negative), the barcode is invalid; set the position to -1. Now add 49 to get the correct ascii value: 1 if it's valid, 0 if it's invalid.
Java 8, 58 56 55 bytes
a->{int r=0,m=1;for(int i:a)r+=(m^=2)*i;return r%10<1;}
-2 bytes indirectly thanks to @RickHitchcock, by using (m=4-m)*i instead of m++%2*2*i+i after seeing it in his JavaScript answer.
-1 byte indirectly thanks to @ETHProductions (and @RickHitchcock), by using (m^=2)*i instead of (m=4-m)*i.
Explanation:
a->{ // Method with integer-array parameter and boolean return-type
int r=0, // Result-sum
m=1; // Multiplier
for(int i:a) // Loop over the input-array
r+= // Add to the result-sum:
(m^=2) // Either 3 or 1,
*i; // multiplied by the digit
// End of loop (implicit / single-line body)
return r%10<1; // Return if the trailing digit is a 0
} // End of method
TI-Basic (83 series), 18 bytes
not(fPart(.1sum(2Ans-Ans9^cumSum(binomcdf(7,0
Takes input as a list in Ans. Returns 1 for valid barcodes and 0 for invalid ones.
A port of my Mathematica answer. Includes screenshot, in lieu of an online testing environment:
Notable feature: binomcdf(7,0 is used to generate the list {1,1,1,1,1,1,1,1} (the list of probabilities that from 7 trials with success probability 0, there will be at most N successes, for N=0,1,...,7). Then, cumSum( turns this into {1,2,3,4,5,6,7,8}.
This is one byte shorter than using the seq( command, though historically the point was that it's also significantly faster.
JavaScript (ES6), 41 40 38 bytes
Saved 2 bytes thanks to @ETHProductions and 1 byte thanks to @Craig Ayre.
s=>s.map(e=>t+=e*(i^=2),t=i=1)|t%10==1
Takes input as a list of digits.
Determines the sum of all digits, including the checksum.
If the sum is a multiple of 10, then it's a valid barcode.
Test Cases
let f=
s=>s.map(e=>t+=e*(i^=2),t=i=1)|t%10==1
console.log(f([2,0,3,7,8,2,4,0]));
console.log(f([3,3,7,6,5,1,2,9]));
console.log(f([7,7,2,3,4,5,7,5]));
console.log(f([0,0,0,0,0,0,0,0]));
console.log(f([2,1,0,3,4,9,8,4]));
console.log(f([6,9,1,6,5,4,3,0]));
console.log(f([1,1,9,6,5,4,2,1]));
console.log(f([1,2,3,4,5,6,7,8]));
Clojure, 41 bytes
#(=(mod(apply +(map *(cycle[3 1])%))10)0)
C (gcc), 84 82 72 61 54 bytes
c;i;f(x){for(i=c=0;x;x/=10)c+=(1+2*i++%4)*x;c=c%10<1;}
-21 bytes from Neil
-7 bytes from Nahuel Fouilleul
Developed independently of Steadybox's answer
'f' is a function that takes the barcode as an int, and returns 1 for True and 0 for False.
fstores the last digit ofxins(s=x%10),Then calculates the sum in
c(for(i=c=0;x;x/=10)c+=(1+2*i++%4)*x;)cis the sum,iis a counterfor each digit including the first, add
1+2*i%4times the digit (x%10) to the checksum and incrementi(thei++in3-2*i++%4)1+2*i%4is 1 wheniis even and 0 wheniis odd
Then returns whether the sum is a multiple of ten, and since we added the last digit (multiplied by 1), the sum will be a multiple of ten iff the barcode is valid. (uses GCC-dependent undefined behavior to omit
return).
Ruby, 41 Bytes
Takes an array of integers. -6 bytes thanks to Jordan.
->n{n.zip([3,1]*4){|x,y|$.+=x*y};$.%10<1}
Haskell, 40 38 bytes
a=3:1:a
f x=mod(sum$zipWith(*)a x)10<1
Takes input as a list of 8 integers. A practical example of using infinite lists.
Edit: Saved 2 bytes thanks to GolfWolf
J, 17 bytes
-10 bytes thanks to cole
0=10|1#.(8$3 1)*]
This uses multiplication of equal sized lists to avoid the zip/multiply combo of the original solution, as well as the "base 1 trick" 1#. to add the products together. The high level approach is similar to the original explanation.
original, 27 bytes
0=10|{:+[:+/[:*/(7$3 1),:}:
explained
0 = is 0 equal to...
10 | the remainder when 10 divides...
{: + the last input item plus...
[: +/ the sum of...
[: */ the pairwise product of...
7$(3 1) ,: 3 1 3 1 3 1 3 zipped with...
}: all but the last item of the input
Excel, 37 bytes
Interpreting "A list of 8 integers" as allowing 8 separate cells in Excel:
=MOD(SUM(A1:H1)+2*(A1+C1+E1+G1),10)=0
05AB1E, 9 bytes
3X‚7∍*OTÖ
3X‚7∍*OTÖ # Argument a
3X‚ # Push [3, 1]
7∍ # Extend to length 7
* # Multiply elements with elements at same index in a
O # Total sum
TÖ # Divisible by 10
Perl 5, 37 32 + 1 (-p) bytes
s/./$-+=$&*(--$|*2+1)/ge;$_=/0$/
-5 bytes thanks to Dom Hastings. 37 +1 bytes was
$s+=$_*(++$i%2*2+1)for/./g;$_=!!$s%10
APL (Dyalog), 14 bytes
Equivalent with streetster's solution.
Full program body. Prompts for list of numbers from STDIN.
0=10|+/⎕×8⍴3 1
Is…
0= zero equal to
10| the mod-10 of
+/ the sum of
⎕× the input times
8⍴3 1 eight elements cyclically taken from [3,1]
?
MATLAB/Octave, 32 bytes
@(x)~mod(sum([2*x(1:2:7),x]),10)
I'm going to post this despite the other Octave answer as I developed this code and approach without looking at the other answers.
Here we have an anonymous function which takes the input as an array of 8 values, and return true if a valid barcode, false otherwise..
The result is calculated as follows.
2*x(1:2:7)
[ ,x]
sum( )
mod( ,10)
@(x)~
- Odd digits (one indexed) are multiplied by 2.
- The result is prepended to the input array, giving an array whose sum will contain the odd digits three times, and the even digits once.
- We do the sum which will also include the supplied checksum within our sum.
- Next the modulo 10 is performed. If the checksum supplied was valid, the sum of all multiplied digits including the checksum value would end up being a multiple of 10. Therefore only a valid barcode would return 0.
- The result is inverted to get a logical output of true if valid.
PHP, 82 bytes
<?for(;$i<strlen($p=$argv[1])-1;)$t+=($i%2?1:3)*$p[$i++];echo(10-$t%10)%10==$p[7];
Input as a string; prints 1 for valid, nothing for invalid.
MATL, 10 bytes
Thanks to @Zgarb for pointing out a mistake, now corrected.
IlhY"s10\~
Try it online! Or verify all test cases.
Explanation
Ilh % Push [1 3]
Y" % Implicit input. Run-length decoding. For each entry in the
% first input, this produces as many copies as indicated by
% the corresponding entry of the second input. Entries of
% the second input are reused cyclically
s % Sum of array
10\ % Modulo 10
~ % Logical negate. Implicit display
JavaScript (Node.js), 47 bytes
e=>eval(e.map((a,i)=>(3-i%2*2)*a).join`+`)%10<1
Although there is a much shorter answer already, this is my first attempt of golfing in JavaScript so I'd like to hear golfing recommendations :-)
Testing
let f=
e=>eval(e.map((a,i)=>(3-i%2*2)*a).join`+`)%10<1
console.log(f([2,0,3,7,8,2,4,0]));
console.log(f([3,3,7,6,5,1,2,9]));
console.log(f([7,7,2,3,4,5,7,5]));
console.log(f([0,0,0,0,0,0,0,0]));
console.log(f([2,1,0,3,4,9,8,4]));
console.log(f([6,9,1,6,5,4,3,0]));
console.log(f([1,1,9,6,5,4,2,1]));
console.log(f([1,2,3,4,5,6,7,8]));
Alternatively, you can Try it online!
Jelly, 8 bytes
m2Ḥ+µS⁵ḍ
Jelly, 9 bytes
JḂḤ‘×µS⁵ḍ
Try it online or Try the test suite.
How this works
m2Ḥ+µS⁵ḍ ~ Full program.
m2 ~ Modular 2. Return every second element of the input.
Ḥ ~ Double each.
+µ ~ Append the input and start a new monadic chain.
S ~ Sum.
⁵ḍ ~ Is divisible by 10?
JḂḤ‘×µS⁵ḍ ~ Full program (monadic).
J ~ 1-indexed length range.
Ḃ ~ Bit; Modulo each number in the range above by 2.
Ḥ ~ Double each.
‘ ~ Increment each.
× ~ Pairwise multiplication with the input.
µ ~ Starts a new monadic chain.
S ~ Sum.
⁵ḍ ~ Is the sum divisible by 10?
The result for the first 7 digits of the barcode and the checksum digit must add to a multiple of 10 for it to be valid. Thus, the checksum is valid iff the algorithm applied to the whole list is divisible by 10.
Pyth, 8 bytes
!es+*2%2
Pyth, 13 bytes
If we can assume the input always has exactly 8 digits:
!es.e*bhy%hk2
How does this work?
!es+*2%2 ~ Full program.
%2 ~ Input[::2]. Every second element of the input.
*2 ~ Double (repeat list twice).
+ ~ Append the input.
s ~ Sum.
e ~ Last digit.
! ~ Logical NOT.
!es.e*sbhy%hk2 ~ Full program.
~ Convert the input to a String.
.e ~ Enumerated map, storing the current value in b and the index in k.
%hk2 ~ Inverted parity of the index. (k + 1) % 2.
hy ~ Double, increment. This maps odd integers to 1 and even ones to 3.
b ~ The current digit.
* ~ Multiply.
s ~ Sum.
e ~ Last digit.
! ~ Logical negation.
If the sum of the first 7 digit after being applied the algorithm is subtracted from 10 and then compared to the last digit, this is equivalent to checking whether the sum of all the digits, after the algorithm is applied is a multiple of 10.
QBIC, 23 bytes
[4|_!_!p=p+b*3+c]?p%z=0
Explanation
[4| DO four times
_! Ask the user for a digit, save as 'b' ('a' is taken by [4| )
_! Ask the user for a digit, save as 'c'
p=p Add to running total p
+b*3 the first digit times 3
+c and the second digit
] NEXT
?p%z=0 PRINT -1 if the seven digits + checksum MOD 10 == 0
Octave, 40 bytes
@(a)9-mod(a*("31313130"'-48)-1,10)==a(8)
Accepts an array of integers as input and returns true/false.
Java (OpenJDK 8), 104 99 bytes
s->{int y=0,i=0;for(;i<7;y+=(3-i%2*2)*(s.charAt(i++)-48));return 58-(y%10<1?10:y%10)==s.charAt(7);}
Batch, 88 bytes
@set/as=0,n=%1
@for %%i in (1 2 3 4)do @set/as+=n+n/10*3,n/=100
@if %s:~-1%==0 echo 1
N.B. Leading zeros are not acceptable as they will cause the input to be treated as octal. As the numbers are always 8 digits the loop size can be hardcoded which saves 2 bytes.
C, 78 77 bytes
i,s,c,d=10;f(b){for(i=s=0,c=b%d;b/=d;)s+=b%d*(3-i++%2*2);return(d-s%d)%d==c;}
C (gcc), 72 bytes
i,s,c,d=10;f(b){for(i=s=0,c=b%d;b/=d;)s+=b%d*(i++%2?:3);b=(d-s%d)%d==c;}
PowerShell, 85 bytes
param($a)(10-(("$a"[0..6]|%{+"$_"*(3,1)[$i++%2]})-join'+'|iex)%10)%10-eq+"$("$a"[7])"
Try it online! or Verify all test cases
Implements the algorithm as defined. Takes input $a, pulls out each digit with "$a"[0..6] and loops through them with |%{...}. Each iteration, we take the digit, cast it as a string "$_" then cast it as an int + before multiplying it by either 3 or 1 (chosen by incrementing $i modulo 2).
Those results are all gathered together and summed -join'+'|iex. We take that result mod 10, subtract that from 10, and again take the result mod 10 (this second mod is necessary to account for the 00000000 test case). We then check whether that's -equal to the last digit. That Boolean result is left on the pipeline and output is implicit.
