Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

-7 from Nahuel Fouilleul
Source Link
pizzapants184
  • 3.6k
  • 16
  • 18

#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

Try it online!

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.

  • f stores the last digit of x in s (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;)

  • c is the sum, i is a counter

  • for each digit including the first, add 1+2*i%4 times the digit (x%10) to the checksum and increment i (the i++ in 3-2*i++%4)

    • 1+2*i%4 is 1 when i is even and 0 when i is 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

Try it online!

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.

  • f stores the last digit of x in s (s=x%10),

  • Then calculates the sum in c (for(i=c=0;i<8;x/=10)c+=(1+2*i++%4)*x;)

  • c is the sum, i is a counter

  • for each digit including the first, add 1+2*i%4 times the digit (x%10) to the checksum and increment i (the i++ in 3-2*i++%4)

    • 1+2*i%4 is 1 when i is even and 0 when i is 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

Try it online!

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.

  • f stores the last digit of x in s (s=x%10),

  • Then calculates the sum in c (for(i=c=0;x;x/=10)c+=(1+2*i++%4)*x;)

  • c is the sum, i is a counter

  • for each digit including the first, add 1+2*i%4 times the digit (x%10) to the checksum and increment i (the i++ in 3-2*i++%4)

    • 1+2*i%4 is 1 when i is even and 0 when i is 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).

-11 from Neil (again)
Source Link
pizzapants184
  • 3.6k
  • 16
  • 18

#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

Try it online! Try it online!

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.

  • f stores the last digit of x in s (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;)

  • c is the sum, i is a counter

  • for each digit afterincluding the first, add 3-2*i%41+2*i%4 times the digit (x%10) to the checksum and increment i (the i++ in 3-2*i++%4)

    • 3-2*i%41+2*i%4 is 31 when i is even and 10 when i is odd
  • Then returns whethers is 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

Try it online!

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.

  • f stores the last digit of x in s (s=x%10),

  • Then calculates the sum in c (for(i=c=0;i<7;x/=10,c+=(3-2*i++%4)*(x%10));)

  • c is the sum, i is a counter

  • for each digit after the first, add 3-2*i%4 times the digit (x%10) to the checksum and increment i (the i++ in 3-2*i++%4)

    • 3-2*i%4 is 3 when i is even and 1 when i is odd
  • Then returns whethers is 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

Try it online!

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.

  • f stores the last digit of x in s (s=x%10),

  • Then calculates the sum in c (for(i=c=0;i<8;x/=10)c+=(1+2*i++%4)*x;)

  • c is the sum, i is a counter

  • for each digit including the first, add 1+2*i%4 times the digit (x%10) to the checksum and increment i (the i++ in 3-2*i++%4)

    • 1+2*i%4 is 1 when i is even and 0 when i is 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.

-1 bytes from Neil
Source Link
pizzapants184
  • 3.6k
  • 16
  • 18

#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

Try it online!

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.

  • f stores the last digit of x in s (s=x%10),

  • Then calculates the sum in c (for(i=c=0;i<7;x/=10,c+=(3-2*i++%4)*(x%10));)

  • c is the sum, i is a counter

  • for each digit after the first, add 3-2*i%4 times the digit (x%10) to the checksum and increment i (the i++ in 3-2*i++%4)

    • 3-2*i%4 is 3 when i is even and 1 when i is odd
  • Then returns whether s is 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;}

Try it online!

'f' is a function that takes the barcode as an int, and returns 1 for True and 0 for False.

  • f stores the last digit of x in s (s=x%10),

  • Then calculates the sum in c (for(i=c=0;i<7;x/=10,c+=(3-2*i++%4)*(x%10));)

  • c is the sum, i is a counter

  • for each digit after the first, add 3-2*i%4 times the digit (x%10) to the checksum and increment i (the i++ in 3-2*i++%4)

    • 3-2*i%4 is 3 when i is even and 1 when i is odd
  • Then returns whether s is 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

Try it online!

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.

  • f stores the last digit of x in s (s=x%10),

  • Then calculates the sum in c (for(i=c=0;i<7;x/=10,c+=(3-2*i++%4)*(x%10));)

  • c is the sum, i is a counter

  • for each digit after the first, add 3-2*i%4 times the digit (x%10) to the checksum and increment i (the i++ in 3-2*i++%4)

    • 3-2*i%4 is 3 when i is even and 1 when i is odd
  • Then returns whether s is equal to the next multiple of ten minus the sum (return(c+9)%10==9-s).

-2 bytes
Source Link
pizzapants184
  • 3.6k
  • 16
  • 18
Loading
Source Link
pizzapants184
  • 3.6k
  • 16
  • 18
Loading

AltStyle によって変換されたページ (->オリジナル) /