How do I calculate similarity of two integers?

2020-07-13 07:18发布

Actually it's quite hard to describe:
I want to implement an algorithm which compares figure by figure of the same position (as I do my calculations in a 10-based system it's rather the same "power of ten") of two given integers/number (with the same "length"). It should return the grade of equality as following:

  • 4491 and 1020 = 0
  • 4491 and 4123 = 1
  • 4491 and 4400 = 2
  • 4491 and 4493 = 3
  • 4491 and 4491 = 4
  • 4491 and 4091 = 1

I do not want to do my calculations based on a string-comparison, as I'll doing this in a way bigger scenario :)

标签: c#
6条回答
放我归山
2楼-- · 2020-07-13 07:42

For all cases where X and Y are not equal:

Length - Math.Floor(Math.Log10(Math.Abs(X - Y)) + 1)

4491 and 1020

4 - Math.Floor(Math.Log10(Math.Abs(4491 - 1020)) + 1) = 0

4491 and 4493

4 - Math.Floor(Math.Log10(Math.Abs(4491 - 4493)) + 1) = 3
查看更多
别忘想泡老子
3楼-- · 2020-07-13 07:46

It sounds like the Levenshtein Distance would be appropriate. This is a standard way to measure the difference between two strings. In your case, the strings are the decimal representations of the numbers.

查看更多
神经病院院长
4楼-- · 2020-07-13 07:57

Just to try to salvage something from this question after my last attempt...

int Compare(int x, int y)
{
    int pow10 = (int)Math.Pow(10, Math.Floor(Math.Log(Math.Max(x, y), 10)));
    int matches = 0;
    while(pow10 > 0 && (x / pow10) == (y / pow10))
    {
        matches++;
        pow10 /= 10;
    }
    return matches;
}
查看更多
成全新的幸福
5楼-- · 2020-07-13 08:03
public static int Compare(int i1, int i2)
{
    int result = 0;
    while(i1 != 0 && i2 != 0)
    {
        var d1 = i1 % 10;
        var d2 = i2 % 10;
        i1 /= 10;
        i2 /= 10;
        if(d1 == d2)
        {
            ++result;
        }
        else
        {
            result = 0;
        }
    }
    if(i1 != 0 || i2 != 0)
    {
        throw new ArgumentException("Integers must be of same length.");
    }
    return result;
}

Note: it does not handle negative integers

Update: fixed after question update

查看更多
放荡不羁爱自由
6楼-- · 2020-07-13 08:06

See the Answer to this SO Question

You can Split the digits by the first method and Get the Similarity from the Second Method:

int[] GetIntArray(int num)
{
    List<int> listOfInts = new List<int>();
    while(num > 0)
    {
        listOfInts.Add(num % 10);
        num /= 10;
    }
    listOfInts.Reverse();
    return listOfInts.ToArray();
}

int GetSimilarity(int firstNo, int secondNo)
{
    int[] firstintarray = GetIntArray(firstNo)
    int[] secondintarray = GetIntArray(secondNo)
    if (firstintarray.Count != secondintarray.Count)
    {
        throw new ArgumentException("Numbers Unequal in Length!");
    }
    int similarity = 0;
    for(i = 0; i < firstintarray.Count; i++)
    {
        if (secondintarray[i] = firstintarray[i])
        {
            similarity++;
            continue;
        }
        break;
    }
}

Now you can Compare the the two int arrays like this :

int Similarity = GetSimilarity(4491, 4461);// Returns 2
查看更多
一夜七次
7楼-- · 2020-07-13 08:08

I thing the best way to calculate it is using Euclidean Similarity.

Please see this link: http://stackoverflow.com/questions/11555355/calculating-the-distance-between-2-points

查看更多
登录 后发表回答