I was wondering what the fastest way is to check for divisibility in VB.NET.
I tried the following two functions, but I feel as if there are more efficient techniques.
Function isDivisible(x As Integer, d As Integer) As Boolean
Return Math.floor(x / d) = x / d
End Function
Another one I came up with:
Function isDivisible(x As Integer, d As Integer) As Boolean
Dim v = x / d
Dim w As Integer = v
Return v = w
End Function
Is this a more practical way?
Use 'Mod' which returns the remainder of number1 divided by number2. So if remainder is zero then number1 is divisible by number2. e.g.
Evenly divisible means that you have no remainder. So, 20 is evenly divisible by 5 since 20 / 5 = 4. Though, 21 is not evenly divisible by 5 since 21 / 5 = 4 R 1, or 4.2.
A number is divisible by 3 if sum of its digits is divisible by 3. Illustration: For example n = 1332 Sum of digits = 1 + 3 + 3 + 2 = 9 Since sum is divisible by 3, answer is Yes.
Use Mod
:
Function isDivisible(x As Integer, d As Integer) As Boolean
Return (x Mod d) = 0
End Function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With