Generate sequence key based on sum of digits

3
  

I have a cyclic function where an initial string can have any value in the check digit, but all future submissions receive a previously calculated value.

In this function, I get a string (which will always be a number) and in this method, I take the last digit and add up the rest. At the end, return that number + 1 and concatenate with the sum. Complicated in this way I explained, no?

Explaining better, this would be it: I get the string 10000 and the return should be 10011. Where 1000 is + 1 and the next last digit is the sum of the remainder (1 + 0 + 0 + 0 = 1).

My method is as follows:

public static int GenerateKey(string s)
        {           
            s = s.Remove(s.Length - 1);

            var i = Convert.ToInt32(s);
            var sum = 0;

            while (i != 0)
            {
                sum += i % 10;
                i /= 10;
            }

            var somatorio = Convert.ToInt32(s) + 1;

            var id = Convert.ToString(somatorio);

            if (sum.ToString().Length > 1)
            {
                sum = sum % 10;
            }

            id = id += sum;

            return Convert.ToInt32(id);
        } 

1st: This method needs improvement and / or is "poorly written"?

2nd: Using this method is it possible for the return to be repeated with one already used before? Remembering that the next time you execute the method the number will be the return of the previously executed method.

Ex: If I started with 10000, the return will be 10011, so the next time the method is run the string will have the value of 10011, and so on.     

asked by anonymous 16.10.2015 / 20:00

1 answer

2

Badly written is not (in my opinion), I would change some small details, but it is cosmetic:

using static System.Console;
using static System.Convert;

public class Program {
    public static void Main() {
        WriteLine(GenerateKey("10000"));
        WriteLine(GenerateKey("10011"));
        WriteLine(GenerateKey("10100"));
        WriteLine(GenerateKey("00000"));
        WriteLine(GenerateKey("999999999"));
    }
    public static int GenerateKey(string s) {           
        s = s.Remove(s.Length - 1);
        var i = ToInt32(s);
        var sum = 0;
        while (i != 0) {
            sum += i % 10;
            i /= 10;
        }
        var somatorio = ToInt32(s) + 1;
        var id = somatorio.ToString();
        if (sum.ToString().Length > 1) {
            sum %= 10;
        }
        id += sum;
        return ToInt32(id);
    }
}

See working on dotNetFiddle .

I did not analyze deeply if there were any errors, but it seems that not. The id = id += sum; line does not cause an error in this case, but it is quite strange to do this.

I can not prove you mathematically but it does not seem like it will repeat under normal conditions. Of course you would need to see what happens when there is inconsistency in the number if it is too large. Maybe you can ensure that this does not occur anywhere else in the code or have full control of the data used. Have you thought about what might happen if you send a "00000"? Or a "0"? Or a "99999"? OR "999999999"? Or "9999999999"?

Obviously I can not guarantee if you do what you want. It looks like a strange algorithm, but it must have a reason for it to exist.

    
16.10.2015 / 20:21