a quick challenge

For all coding issues - MODers and programmers, HTML and more.

Moderators: Jeff250, fliptw

Post Reply
User avatar
fliptw
DBB DemiGod
DBB DemiGod
Posts: 6415
Joined: Sat Oct 24, 1998 2:01 am
Location: Calgary Alberta Canada

a quick challenge

Post by fliptw » Thu Dec 22, 2011 1:56 pm

A UPC is a 12-digit number.
Typically its starts as a eleven digit number and a check digit is computed and stuck at the end.

The procedure to compute the check digit for UPC thusly follows:
  1. sum the digits in the odd positions and multiply the result by 3
  2. add to that the even digits
  3. modulus the sum so far by 10
  4. if the result isn't 0, subtract from 10
the challenge is not to compute the check digit without using any string manipulation.
User avatar
Sergeant Thorne
DBB Material Defender
DBB Material Defender
Posts: 4627
Joined: Sun Nov 25, 2001 3:01 am
Location: Indiana, U.S.A.

Re: a quick challenge

Post by Sergeant Thorne » Tue Dec 27, 2011 8:05 pm

I was doing good until you threw that double-negative at me in the last sentence. Screw this, this stuff is hard. ;P
fliptw wrote:the challenge is not to compute the check digit without using any string manipulation.
BTW I've already not computed it without using string manipulation, I'm just keeping it to myself because it wouldn't be unfair to everyone else. :P
User avatar
fliptw
DBB DemiGod
DBB DemiGod
Posts: 6415
Joined: Sat Oct 24, 1998 2:01 am
Location: Calgary Alberta Canada

Re: a quick challenge

Post by fliptw » Tue Dec 27, 2011 11:39 pm

ahh I was in a rush when I wrote it.

the challenge is to compute the check digit without using any string manipulation.
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6387
Joined: Sun Sep 05, 1999 2:01 am
Location: ☃☃☃

Re: a quick challenge

Post by Jeff250 » Wed Dec 28, 2011 2:59 am

Code: Select all

unsigned long long upc_check( unsigned long upc )
{
    unsigned long left = upc;
    int i, mod, sum = 0;
    for (i = 0; i < 11; ++i) {
        mod = left % 10;
        sum +=  mod + ((mod & ((i & 1) - 1)) << 1);
        left = left / 10;
    }
    return (unsigned long long) upc * 10 + (10 - (sum % 10)) % 10;
}
This C code should work as long as you have two's complement integers. It's a bit pedantic though, espcially if the C compiler partially or fully unrolls the loop.
User avatar
Isaac
DBB Artist
DBB Artist
Posts: 7127
Joined: Mon Aug 01, 2005 8:47 am
Location: Ơ̸̦͇̲̬̭̱̰͎̞͈̣͎͚̳ͬ͋̃̀̇͊͂͋͐ͦ̽ͣ̂ͥ͊̅̀̚͠ B̶͖̯͉̜̰̲̓̔͋̈́ͅ È̯ Y̪̤̼͉̠̙͝

Re: a quick challenge

Post by Isaac » Sun Feb 05, 2012 8:39 pm

Darn it. I love string manipulation! I'll get back to this.
s☼-£♦и̫͍ͥ̍ͪ͌̓͗͡о̡̹̱͊̅ͮ̓̕͢б̧̝̻̪̤̳̜͐̓̉ͤ͢͜ ͙̬͙̆̑ͮ̐ͭ̾̂́͘i̎̌̾̓̽̀̈̓̀҉͉̙̦͎̘̝͕f̻͕͔̘ͣͣ̓͊̿͢͜ ͍͔͈͕̮̫ͣ̆ͮ̊͋/♂6Æ!♪╩"▲L└уͭ̂͐̇҉̴̣̼̞̠̯͓̺̞ф̜̊͌̈́̋̏̐́ц̨͔̮̿̇ ̨̛͖̙͖̖̮̗̱ͩ̆͞ͅа̥͇̞̖͚̟̅͐ͤ͞͠͠э̜̘̩̳̬͔̾ͯ̀ͫ̒̐̿ͅͅг̭̖̀ͦ̒̑ͥ̌ͮͫ͞ё͔̟̃ͬ̾̓͟ё̦̞̙̫͔̩͑̀͂ͯ̄̔̃̑̀͠ͅͅ
User avatar
Jeff250
DBB Master
DBB Master
Posts: 6387
Joined: Sun Sep 05, 1999 2:01 am
Location: ☃☃☃

Re: a quick challenge

Post by Jeff250 » Sun Feb 05, 2012 9:31 pm

There still has yet to be a good solution. :-P
Post Reply