Algorithm for verifying digit calculation

1
  

Create an algorithm to request a five digit code (COD), and generate the checker digit (DIGV) module 7 for that code.

     Assuming that the five digits of the code are ABCDE, a way of calculating the desired digit with modulus of 7 is:

S = (6xA) + (5xB) + (4xC) + (3xD) + (2xE)
DIGV = resto da divisão S por 7
     

Use quotient and rest operators

I thought of developing this exercise by turning what the user will type into a string, picking up each index of that string and storing it in a variable, and doing the math, what do you think? Could someone explain me better?

    
asked by anonymous 28.03.2016 / 19:18

3 answers

2

Whereas

S = (6xA) + (5xB) + (4xC) + (3xD) + (2xE)
DIGV = resto da divisão S por 7

It would be equivalent to

S = (6 * A) + (5 * B) + (4 * C) + (3 * D) + (2 * E)
DIGV = S % 7;

We can already implement in several languages, remembering that A, B, C, D and E are COD digits. Deploying in Javascript:

COD_NUM = 55480;
COD = COD_NUM+"";

A = parseInt(COD.charAt(0));
B = parseInt(COD.charAt(1));
C = parseInt(COD.charAt(2));
D = parseInt(COD.charAt(3));
E = parseInt(COD.charAt(4));

S = (6 * A) + (5 * B) + (4 * C) + (3 * D) + (2 * E);
DIGV = S % 7;

Demo

    
10.03.2017 / 05:39
2

Python

In Python, you can do the following:

code = "55480"

def get_divg (code):

    code = map(int, code)
    weights = [6, 5, 4, 3, 2]

    return sum(w * c for w, c in zip(weights, code)) % 7

print(get_divg(code))
  

See working at Ideone .

We first convert string to a list of integers using map ; we make the inner product between the code and its respective weights through list comprehensions ; we add all the values through the function sum and finally we obtain the remainder of division by 7 with the % operator.

    
22.06.2017 / 03:05
1

Not considering language specificities (given the theme of Algoritmos ):

Your solution seems appropriate. You can take user input as a string and work with each character. Please be careful to make the calculations with the digits equivalent to those characters.

For example, if the user types 12345 , we would have A = 1 , not A = '1' ( A is the 1 , not the '1' character that has decimal integer equal to 49 , see ASCII Table ).

Another possible solution would be to read the user input, character the character, in a loop. This solution is quite similar to the previous one. The biggest difference would be the fact that the input is taken character by character, not the complete string.

A third output would be to take the integer value entered by the user and use integer divisions to find the characters of each position.

In this way, for a 12345 entry and considering that:

  • the // operator gives us the result of the entire division;
  • the % operator gives us the result of the rest of the entire division.

We would have:

Entrada <- 12345
X <- Entrada

A <- X // 10000
X <- X % 10000

B <- X // 1000
X <- X % 1000

C <- X // 100
X <- X % 100

D <- X // 10
X <- X % 10

E <- X

There are probably other ways to do this, but these are some I could think of.

I believe that the simplest (allying the issue of implementation and clarity of code to others) is the first, also suggested by you.

    
22.06.2017 / 03:03