0

Imagine a range of numbers from -133 to +71.

I want to find the first and last numbers in the range that divide by 20: in this case it would be -120 and +60.

I can write a For loop that tests each value and stores the required values:

Dim resultFirst, resultLast As Integer
Dim FirstFound As Boolean = False
For a As Integer = -133 To 71
    If a Mod 20 = 0 Then
        If FirstFound = False Then
            resultFirst = a
            FirstFound = True
        End If
        resultLast = a
    End If
Next

but I suspect there is a simpler formula.

MatSnow
  • 7,357
  • 3
  • 19
  • 31
Tim Makins
  • 394
  • 4
  • 12

3 Answers3

4

You can use Enumerable.Range() and the LINQ-methods Where, Min and Max

Dim resultFirst As Integer
Dim resultLast As Integer
Dim min As Integer = -133
Dim max As Integer = 71
Dim div As Integer = 20

resultFirst = Enumerable.Range(min, max - min + 1).Where(Function(x) x Mod div = 0).Min()
resultLast = Enumerable.Range(min, max - min + 1).Where(Function(x) x Mod div = 0).Max()
MatSnow
  • 7,357
  • 3
  • 19
  • 31
1

You can use the following to get the first and last value which is dividable by 20:

Dim fromValue As Integer = -133
Dim first As Integer = (fromValue - (fromValue Mod 20)) + IIf(fromValue > 0 And fromValue Mod 20 <> 0, 20, 0)

Dim toValue As Integer = 71
Dim last As Integer = (toValue - (toValue Mod 20)) - IIf(toValue < 0 And toValue Mod 20 <> 0, 20, 0)

You can also create a function using the above formula:

Private Function GetResult(ByVal fromInt As Integer, ByVal toInt As Integer, ByVal divider As Integer) As Integer()

    'set the real from and to value from parameter.
    Dim fromValue As Integer = Math.Min(fromInt, toInt)
    Dim toValue As Integer = Math.Max(fromInt, toInt)

    'get the first and last number dividable by divider between numbers.
    Dim first As Integer = (fromValue - (fromValue Mod divider)) + IIf(fromValue > 0 And fromValue Mod divider <> 0, divider, 0)
    Dim last As Integer = (toValue - (toValue Mod divider)) - IIf(toValue < 0 And toValue Mod divider <> 0, divider, 0)

    If first > toValue Or last < fromValue Then
        Return {}
    Else
        Return {first, last}
    End If
End Function

Some test cases for the above function:

GetResult(-133, 71, 20)   '0: -120; 1: 60
GetResult(71, -133, 20)   '0: -120; 1: 60
GetResult(100, 119, 20)   '0: 100;  1: 100
GetResult(-113, -112, 20) 'empty array
GetResult(120, 140, 20)   '0: 120;  1: 140
Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
  • This version is also useful as it can also be converted to floating point numbers: for instance if you wanted to find the first and last points in the range: -133 to 71 that divide by 2.5 – Tim Makins Apr 30 '19 at 12:03
  • ... this solution is also working since .NET Framework 2.0 – Sebastian Brosch Apr 30 '19 at 12:15
1

Try this one

  Dim s As IEnumerable(Of Integer) =
                Enumerable.Range(-133, 133 + 72)

        Dim minV As Integer = s.AsEnumerable().Where(Function(n) n Mod 20 = 0).Min(Function(n) n)

        Dim maxV As Integer = s.AsEnumerable().Where(Function(n) n Mod 20 = 0).Max(Function(n) n)

        Console.WriteLine(minV.ToString() & " " & maxV.ToString())
        Console.ReadLine()