Search code examples
vb.netalgorithmformulapolynomial-mathrecurring-events

Formula to calculate how hours correlate to fixed interval maintenance schedule


I have a database driven vb.net program that records the number of hours a piece of equipment has operated each day. This is a totalizer number that always increases for the life of the equipment, like an automobile odometer.

The equipment has a fixed interval maintenance schedule, like oil changes every 3,000 hours.

After today's hours (odometer) reading is entered, I need to calculate how close it is to the closest service interval, either in the past or future. So, if service happens every 10 hours and the equipment is operated for an hour each day the result for each day would be:

Hours (Odometer) Result
1 1
2 2
3 3
4 4
5 5
6 4
7 3
8 2
9 1
10 0
11 1
12 2
13 3
14 4
15 5
16 4
17 3
18 2
19 1
20 0
21 1
22 2
23 3
24 4
25 5

Is there a formula that can produce these results for a given reading and service interval?

I imagine modulo division may be involved somehow as (Hours MOD Interval) = 0 identifies the service interval.

The page, Simple Algorithm for Recurring Tasks discusses a somewhat similar problem and uses the formula score = max(0, (1 + interval_since_last - minimum) / (1 + maximum - minimum)).

Similar to this Stack Exchange, I entered the result sequence into OEIS and got this but I don't understand how to derive the equation from that page.


Solution

  • The URL in your OP, OEIS (Online Encyclopedia of Integer Sequences), gives a formula of:

    a(n) = abs(n - 10*round(n/10))
    

    In VB.NET one can do the following:

    Private Function Calculate(hours As Integer) As Double
        Return Math.Abs(hours - 10 * Math.Round(hours / 10.0))
    End Function
    

    Usage:

    Dim hours as Integer = 11
    Debug.WriteLine($"result: {Calculate(hours)}")
    

    Result: 1