I use a numericupdown set min = 0 maximum = 59 and increment = 1
When i check the debugger at
Private Sub Numericsec_ValueChanged(sender As Object, e As System.EventArgs) Handles Numericsec.ValueChanged
unitsec = Convert.ToInt32(DirectCast(sender, NumericUpDown).Value Mod 10)
tensec = Convert.ToInt32(DirectCast(sender, NumericUpDown).Value / 10)
when value is 5 then
unitsec = 5
tensec = 0
but when value is 6
unitsec = 5
tensec = 1 ????
Thanks
There's nothing special about NumericUpDown.Value; it's just a Decimal. Test this code and confirm that you get the same results. It might just be an issue with understanding the various arithmetic and conversions involved.
Test code:
Console.WriteLine($"Convert.ToInt32(5D Mod 10): {Convert.ToInt32(5D Mod 10)}")
Console.WriteLine($"Convert.ToInt32(5D / 10): {Convert.ToInt32(5D / 10)}")
Console.WriteLine($"Convert.ToInt32(6D Mod 10): {Convert.ToInt32(6D Mod 10)}")
Console.WriteLine($"Convert.ToInt32(6D / 10): {Convert.ToInt32(6D / 10)}")
Console.WriteLine($"Convert.ToInt32(6D) \ 10: {Convert.ToInt32(6D) \ 10}")
Console.WriteLine($"Convert.ToInt32(5D) \ 10: {Convert.ToInt32(5D) \ 10}")
Output:
Convert.ToInt32(5D Mod 10): 5
Convert.ToInt32(5D / 10): 0
Convert.ToInt32(6D Mod 10): 6
Convert.ToInt32(6D / 10): 1
Convert.ToInt32(6D) \ 10: 0
Convert.ToInt32(5D) \ 10: 0
Explanation:
5D Mod 10: No secret here. 5 Mod 10 is 5. Conversion does nothing
5D / 10: Convert.ToInt32 uses banker's rounding i.e. midpoint rounding goes to the nearest even number
6D Mod 10: Again, no secret here
6D / 10: Here it might be confusing because now you have 0.6 and it is rounded to the nearest integer. Midpoint rounding down to 0 doesn't apply because it is > the midpoint.
5D \ 10, 6D \ 10: Integer division discards decimals. Both result in 0.
Related
List
L= [23, 91, 0, -11, 4, 23, 49]
Code
for i in L:
if i > 10:
num = i * 30
else:
num = i * 1
if num % 2 == 0:
num += 6
if i > 50:
num -= 10
if i != -11:
num += 10
print(num)
Output
696
2736
6
-11
10
696
1476
I'm trying to sum the numbers in the output and then divide the total by 2.
Initialize a variable sum outside the loop then, add the value of num to sum before the print statement. Finally, print(sum/2) once outside the loop.
sum = 0
for i in L:
...
sum += num
print(num)
print(sum/2)
I want to create a function which give me what contribution an employee has to make to a pension plan in function of its salary (with given down and upper limit) and in function of its age (doesn't contribute before 25 and after 64).
With the following code, I try to achieve that. I may have spend too much time on front of my screen but i don't see the problem. The debugger runs fine but I keep getting a "#Value!" alert. It may be the If-statment within another If. If someone may see where the problem could be, it would be very helpful
Option Explicit
Constant M = 1175
Function coti_min_LPP(x As Integer, salaire_AVS As Double)
Dim salaire_coord As Double
'The basis for the calculation uses the salary in different layers
If salaire_AVS < 18 * M Then
salaire_coord = 0
ElseIf salaire_AVS > 18 * M And salaire_AVS < 24 * M Then
salaire_coord = 3 * M
ElseIf salaire_AVS > 24 * M Then
salaire_coord = salaire_AVS - 21 * M
End If
'Under a certain limit, the contribution is a percentage of the basis
'according to the age
If salaire_AVS < 72 * M Then
If x < 25 Then
coti_min_LPP = 0
ElseIf x > 24 And x < 35 Then
coti_min_LPP = salaire_coord * 0.07
ElseIf x > 34 And x < 45 Then
coti_min_LPP = salaire_coord * 0.1
ElseIf x > 44 And x < 55 Then
coti_min_LPP = salaire_coord * 0.15
ElseIf x > 54 And x < 65 Then
coti_min_LPP = salaire_coord * 0.18
ElseIf x > 64 Then
coti_min_LPP = 0
End If
Else
'Above the limit, it is the percentage of a fixed amount
If x < 25 Then
coti_min_LPP = 0
ElseIf x > 24 And x < 35 Then
coti_min_LPP = 51 * M * 0.07
ElseIf x > 34 And x < 45 Then
coti_min_LPP = 51 * M * 0.1
ElseIf x > 44 And x < 55 Then
coti_min_LPP = 51 * M * 0.15
ElseIf x > 54 And x < 65 Then
coti_min_LPP = 51 * M * 0.19
ElseIf x > 64 Then
coti_min_LPP = 0
End If
End If
End Function
There are two immediate things that result in an issue:
Instead of Constant M = 1175 you should use Private Const M As Long = 1175. Why Long? Please look at the line If salaire_AVS < 72 * M Then if you add watch and go through your code in the debbuger you will find out that, if M is declared as an integer, after this line (according to watch window) M has value "out of context". This is basically due to the fact that you have achieved integer overflow. Please consider following code:
Example 1:
Sub test2()
Dim a As Integer
Dim b As Double
a = 1175
b = 15
Debug.Print b < 72 * a
End Sub
Option Explicit is a good practice, however requires you to declare all of the variables in your code. Declare them. You may remove Option Explicit for testing purposes. Some lecture on variable declaration: http://www.excelfunctions.net/VBA-Variables-And-Constants.html
After above changes your function works for me (tested =coti_min_LPP(27,19*1175))
Hope that helps. Btw. Please notice that having so many nested ifs with hardcoded values is troublesome to maintain, control and usually is not considered as good coding practice.
I have been trying to solve question 3 on project euler with the following vb code but I do not under stand why it is not working. Can someone please point me in the right direction?
Sub Main()
Dim p As Int64 = 600851475143
Dim y As Integer
For i As Int64 = p / 2 To 1 Step -1
If p Mod i = 0 Then
y = 0
For n As Int64 = 1 To Math.Floor(i ^ 0.5) Step 1
If i Mod n = 0 Then
y = y + 1
End If
Next
If y = 0 Then
Console.WriteLine(i)
Console.ReadLine()
End If
End If
Next
End Sub
The question is "The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?"
For n As Int64 = 1 To Math.Floor(i ^ 0.5) Step 1
If i Mod n = 0 Then
You start with n=1. Every number divides evenly by 1.
(so y = y+1 every time, and If y = 0 Then can never happen).
I've been involved to a challenge.
Here is the question given:
This question involves a game with teddy bears. The game starts when I
give you some bears. You can then give back some bears, but you must
follow these rules (where n is the number of bears that you have):
If n is even, then you may give back exactly n/2 bears. If n is
divisible by 3 or 4, then you may multiply the last two digits of n
and give back this many bears. (By the way, the last digit of n is
n%10, and the next-to-last digit is ((n%100)/10). If n is divisible by
5, then you may give back exactly 42 bears. The goal of the game is to
end up with EXACTLY 42 bears.
For example, suppose that you start with
250 bears. Then you could make these moves:
--Start with 250 bears.
--Since 250 is divisible by 5, you may return 42 of the bears, leaving you with 208 bears.
--Since 208 is even, you may return half of the bears, leaving you with 104 bears.
--Since 104 is even, you may return half of the bears, leaving you with 52 bears.
--Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return these 10 bears. This leaves you with 42
bears.
--You have reached the goal!
Write a recursive function to meet this specification:
bool bears(int n)
// Postcondition: A true return value means that it is possible to win
// the bear game by starting with n bears. A false return value means that
// it is not possible to win the bear game by starting with n bears.
// Examples:
// bear(250) is true (as shown above)
// bear(42) is true
// bear(84) is true
// bear(53) is false
// bear(41) is false
Hint: To test whether n is even, use the expression ((n % 2) == 0).
Here is my solution but unfortinately it always returns false. I guess it is not following the whole alternative paths but have no idea why. Btw, i'm very new with the VB. Thanks in advance.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MsgBox(bear(Int(TextBox1.Text)))
End Sub
Public Function bear(bc As Integer) As Boolean
Dim way1, way2, way3 As Integer
If bc = 42 Then
Return True
ElseIf bc < 42 Then
Return False
ElseIf (bc Mod 2 = 0) Or (bc Mod 3 = 0) Or (bc Mod 4 = 0) Or (bc Mod 5 = 0) Then
If (bc Mod 2 = 0) Then
way1 = bear(bc / 2)
End If
If (bc Mod 3 = 0) Or (bc Mod 4 = 0) Then
way2 = bear((bc Mod 10) * ((bc Mod 100) / 10))
End If
If (bc Mod 5 = 0) Then
way3 = bear(bc - 42)
End If
If (way1 Or way2 Or way3) Then
Return True
Else
Return False
End If
Else
Return False
End If
End Function
(upon further reflection, I can now see that the only problem is the line referenced below..)
.. hang on, it looks like you can do this by just changing one line. In the MOD 3 or 4 case, change this line:
way2 = bear((bc Mod 10) * ((bc Mod 100) / 10))
to these:
dim gb as Integer
gb = (bc Mod 10) * ((bc Mod 100) / 10)
If gb <= 0 then Return False
way2 = bear(bc - gb)
The most obvious thing is that you're checking bears(bearsToTake) instead of bears(bearsLeft-bearsToTake). I think you might be returning false prematurely as well, but I haven't checked, so don't quote me on that.
A solution in Python, for posterity. You don't necessarily need an extra "counter" value like the other answers suggest, but it is often good practice to use one. (I know you're not using Python, but it almost looks like psuedocode, and is thus I've found it easier to grok.)
This solution is almost identical to yours -- it just fixes the parameter from the taken bears to the total bears! bears bears bears.
>>> def checkBears(n):
... if n == 42:
... return True
... elif n < 42:
... return False
... else:
... if not n % 2 and checkBears(n/2):
... return True
... if (not n % 3 or not n % 4) and checkBears(n - n % 10 * (n%100)/10):
... return True
... if not n % 5 and checkBears(n - 42):
... return True
... return False
...
>>> checkBears(250)
True
>>> checkBears(53)
False
>>> checkBears(42)
True
>>> checkBears(84)
True
i was studying some projects and i started The ColorPicker Project. i could not understand the LongToRgb Function >>> which as follow :
**
Private Function LongToRGB(lColor As Long) As String
Dim iRed As Long, iGreen As Long, iBlue As Long
iRed = lColor Mod 256
iGreen = ((lColor And &HFF00) / 256&) Mod 256&
iBlue = (lColor And &HFF0000) / 65536
LongToRGB = Format$(iRed, "000") & ", " & Format$(iGreen, "000") & ", " & Format$(iBlue, "000")
End Function
**
i want someone to explain to me in plain English ...
Mod is the modulo operation, which is ofter referred as % too.
It takes the remainder of the integer division between the two values. In your situation it's useful to get the meaningful part of component color (red, green, blue) from a long that contains all of them packed.
Eg:
1234 Mod 100 = 34
Mod Operator (% in C#)
Basically, it returns the remainder of a division operation. For example, 13 Mod 4 = 1, because 13 / 4 = 3 w/ a remainder of 1. It's important to understand how the long value is created to understand why the function does what it does.
All the colors (Red, Green, Blue) are represented in the amounts of 0-255. For example, imagine the following value: R(8), G(3), B(1).
To understand why the function does what it does, let's look at a scenario where number values range from 0-9 (10 values) instead of 0-255 (256 values). How would you represent a single value that you can reverse engineer the values from? You can not simply add the values together (8 + 3 + 1 = 12) because it would be impossible to reverse engineer the original values. Instead, you must multiply values by the base. The base depends on the value range... in the example it is 10 because there are 10 values. The position is a zero-based index. Red's position is 0, Green's position is 1, Blue's position is 2.
Value * (Base^Position))
Red(8) = (8 * 10^0) = 8 * 1 = 8
Green(3) = (3 * 10^1) = 3 * 10 = 30
Blue(1) = (1 * 10^2) = 1 * 100 = 100
8 + 30 + 100 = 138. And 138 can easily be reverse engineered (in fact, just by looking at it!). Mathematically reverse engineering it is done as so:
(CombinedValue / (Base^Position)) % Base = OriginalValue.
(138 / (10^0)) % 10 = (138 / 1) % 10 = 138 % 10 = 8 (Red)
(138 / (10^1)) % 10 = (138 / 10) % 10 = 13 (decimal is truncated) % 10 = 3 (Green)
(138 / (10^2)) % 10 = (138 / 100) %10 = 1 (decimal is truncated) % 10 = 1 (Blue)
The function does a few things:
It uselessly does a bitwise operator (lColor And &HFF00) and (lColor And &HFF0000) for some reason.
It simplifies the mathematics. There is no point in dividing by 1 for red (256^0 = 1), and there is no point to using the modulo operator to retrieve Green because X % 256 = X for all X where X < 256. Also, 256^2 is equal to 65536.
It uses the actual range that color values can be represented (0-255, 256 values).
You can actually use a simplified version of the function instead:
Private Function LongToRGB(lColor As Long) As String
Dim iRed As Long, iGreen As Long, iBlue As Long
iRed = lColor Mod 256
iGreen = (lColor / 256) Mod 256
iBlue = lColor / 65536
LongToRGB = Format$(iRed, "000") & ", " & Format$(iGreen, "000") & ", " & Format$(iBlue, "000")
End Function
Note that the last method is simply a string formatting function and has nothing to do with the mathematics.