#C (gcc), (削除) 84 (削除ここまで) (削除) 82 (削除ここまで) 72(削除) 72 (削除ここまで)(削除) 61 (削除ここまで) 54 bytes
c;i;f(x){s=x;forfor(i=c=0;i<8;xi=c=0;x;x/=10)c+=(1+2*i++%4)*x;return c%10<1;*x;c=c%10<1;}
-21 bytes from Neil
Try it online! -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;i<8;xi=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).
#C (gcc), (削除) 84 (削除ここまで) (削除) 82 (削除ここまで) 72 bytes
c;i;f(x){s=x;for(i=c=0;i<8;x/=10)c+=(1+2*i++%4)*x;return c%10<1;}
-21 bytes from Neil
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;i<8;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.
#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).
#C (gcc), (削除) 84 (削除ここまで) (削除) 82 (削除ここまで) 72 bytes
c;s;i;fc;i;f(x){s=x;for(i=c=0;i<7;xi=c=0;i<8;x/=10,c+=(3-2*i++%4)*x);return!(c+=(c+s)%101+2*i++%4);*x;return c%10<1;}
-1021 bytes from Neil
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;i<7;xi=c=0;i<8;x/=10,c+=(3-2*i++%4)*c+=(x%10)1+2*i++%4);*x;)cis the sum,iis a counterfor each digit afterincluding the first, add
3-2*i%41+2*i%4times the digit (x%10) to the checksum and incrementi(thei++in3-2*i++%4)3-2*i%41+2*i%4is 31 wheniis even and 10 wheniis odd
Then returns whether
sis equal to the nextsum is a multiple of ten minus, and since we added the sumlast digit (return(c+9)%10==9-smultiplied by 1), the sum will be a multiple of ten iff the barcode is valid.
#C (gcc), (削除) 84 (削除ここまで) (削除) 82 (削除ここまで) 72 bytes
c;s;i;f(x){s=x;for(i=c=0;i<7;x/=10,c+=(3-2*i++%4)*x);return!((c+s)%10);}
-10 bytes from Neil
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;i<7;x/=10,c+=(3-2*i++%4)*(x%10));)cis the sum,iis a counterfor each digit after the first, add
3-2*i%4times the digit (x%10) to the checksum and incrementi(thei++in3-2*i++%4)3-2*i%4is 3 wheniis even and 1 wheniis odd
Then returns whether
sis equal to the next multiple of ten minus the sum (return(c+9)%10==9-s).
#C (gcc), (削除) 84 (削除ここまで) (削除) 82 (削除ここまで) 72 bytes
c;i;f(x){s=x;for(i=c=0;i<8;x/=10)c+=(1+2*i++%4)*x;return c%10<1;}
-21 bytes from Neil
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;i<8;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.
#C (gcc), (削除) 84 (削除ここまで) 82(削除) 82 (削除ここまで) 72 bytes
c;s;i;f(x){s=x%10;fors=x;for(i=c=0;i<7;x/=10,c+=(3-2*i++%4)*(x%10)*x);return!(c+9(c+s)%10==9-s;%10);}
Try it online! -10 bytes from Neil
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;i<7;x/=10,c+=(3-2*i++%4)*(x%10));)cis the sum,iis a counterfor each digit after the first, add
3-2*i%4times the digit (x%10) to the checksum and incrementi(thei++in3-2*i++%4)3-2*i%4is 3 wheniis even and 1 wheniis odd
Then returns whether
sis equal to the next multiple of ten minus the sum (return(c+9)%10==9-s).
#C (gcc), (削除) 84 (削除ここまで) 82 bytes
c;s;i;f(x){s=x%10;for(i=c=0;i<7;x/=10,c+=(3-2*i++%4)*(x%10));return(c+9)%10==9-s;}
'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;i<7;x/=10,c+=(3-2*i++%4)*(x%10));)cis the sum,iis a counterfor each digit after the first, add
3-2*i%4times the digit (x%10) to the checksum and incrementi(thei++in3-2*i++%4)3-2*i%4is 3 wheniis even and 1 wheniis odd
Then returns whether
sis equal to the next multiple of ten minus the sum (return(c+9)%10==9-s).
#C (gcc), (削除) 84 (削除ここまで) (削除) 82 (削除ここまで) 72 bytes
c;s;i;f(x){s=x;for(i=c=0;i<7;x/=10,c+=(3-2*i++%4)*x);return!((c+s)%10);}
-10 bytes from Neil
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;i<7;x/=10,c+=(3-2*i++%4)*(x%10));)cis the sum,iis a counterfor each digit after the first, add
3-2*i%4times the digit (x%10) to the checksum and incrementi(thei++in3-2*i++%4)3-2*i%4is 3 wheniis even and 1 wheniis odd
Then returns whether
sis equal to the next multiple of ten minus the sum (return(c+9)%10==9-s).