bobhope - 6 months ago 9x
Python Question

# Calculating multiplier based on distance between 2 numbers

I'm not sure how to word the question correctly but I'll try.

I'm trying to find a formula to provide a multiplier based on the difference between 2 integers.

### Examples

• 1 and 1 = distance is 0 = 1

• 1 and 2 = distance is +1 = 1.5

• 1 and 3 = distance is +2 = 1.75

• 1 and 4 = distance is +3 = 1.89

• 1 and 5 = distance is +4 = 1.95

• 1 and 6+ = distance is +5 = 2.0

• 20 and 21 = distance is +1 = 1.5

I need this to work for negative distances too.

• 2 and 1 = distance is -1 = 0.5

• 3 and 1 = distance is -2 = 0.75

all the multipliers I used above are examples.

I'm trying to implement this in Python so any sort of example in python would be ideal.

### Thanks to Pedro, John Moeller and ayhan I have this:

``````from math import sqrt

def multiplier(x, y):
dist = y - x
return 1 + dist/sqrt(5+dist**2)
``````

This gives the output:

``````Distance: -5 = 0.09
Distance: -4 = 0.13
Distance: -3 = 0.20
Distance: -2 = 0.33
Distance: -1 = 0.59
Distance: 0 = 1.00
Distance: 1 = 1.41
Distance: 2 = 1.67
Distance: 3 = 1.80
Distance: 4 = 1.87
Distance: 5 = 1.91
``````

Close enough, Thanks!

``````import math