0

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.

ESS
  • 175
  • 1
  • 15

2 Answers2

2

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

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • Excellent @user09938, that's what I needed. I didn't understand how to read the OEIS result and you've helped me to do that. – ESS Mar 02 '23 at 14:26
1

Here are some simple calculations you can do to determine the total hours for the previous and next interval:

Dim Hours As Integer = 47
Dim Interval As Integer = 10

Dim PrevIntervalHours As Integer = (Hours \ Interval) * Interval
Dim NextIntervalHours As Integer = PrevIntervalHours + Interval
Dim PrevDelta As Integer = Math.Abs(Hours - PrevIntervalHours)
Dim NextDelta As Integer = Math.Abs(Hours - NextIntervalHours)
Dim ClosestIntervalHours As Integer = If(PrevDelta < NextDelta, PrevIntervalHours, NextIntervalHours)

Debug.Print("Hours = " & Hours)
Debug.Print("PrevIntervalHours = " & PrevIntervalHours)
Debug.Print("NextIntervalHours = " & NextIntervalHours)
Debug.Print("ClosestIntervalHours = " & ClosestIntervalHours)

Output:

Hours = 47
PrevIntervalHours = 40
NextIntervalHours = 50
ClosestIntervalHours = 50
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Thanks @Idle_Mind that's brilliant. It's not exactly what I was looking for, but I will need that for a different part of my project. – ESS Mar 02 '23 at 14:18