Simple python and need explanation - python-3.x

m = 0
for x in range (4,6):
for y in range (2,4):
m = m + x + y
print (m)
ANSWER: 28
not sure how this is? Excluding the last number in the range, I thought it should be 14. I add it up on paper and cannot understand what I am doing wrong.

That loop is equivalent to:
m = 4+2 + 4+3 + 5+2 + 5+3
And, that sum is 28.
(In the outer loop, x takes on the values 4 and 5. In the inner loop, y takes on values 2 and 3.)

Related

Calculate The Total Result of The Arithmetic Sequence With Big Number in Less Than 1 Second

How to Construct a Python 3 function sum(n) that takes a positive integer n as an input and perform the following computation:
sum(n)=5+10+⋯+5(n−1)+5n.
The value of 𝑛n is between 1 and 10^15. The timelimit for the computation is 1 second. To make your code efficient, try to use the explicit formula (closed form) of sum(n).
Test:
print(sum(1))
print(sum(2))
print(sum(3))
Result:
5
15
30
What I Have Tried:
def sum(n):
AK = 0
n += 1
for i in range(1,n):
P = 5 * i
AK += P
return AK
Unfortunately it takes more than 1 second to finish
as Hans Kesting said, the result is 5 times the sum of 1...n and so you can try this simple and easy piece of code. I haven't actually tried it but in practice, it should be less than one second
def sum(n):
return 5 * (n * (n + 1) // 2)

For Loop Counter in VBA increments by one extra

I have a simple loop counter which I expected to msg me 1,2,3...10. Instead I get 1,3,5,7,9and I can't figure out where this extra increment is coming from. I'm a bit rusty after layoffs so bear with me if this seems simple.
here is my code:
Dim x As Integer
For x = 1 To 10
MsgBox x
x = x + 1
Next x
It is typical that a for loop in most programming languages will automatically increment or iterate through something. In most languages, it's also common to provide a way to specify the increment amount. This is the case in VBA—see VBA For...Next documentation. For example:
For i = 1 to 20 Step 2
'do something
next i
Your code is incrementing x, and For is also incrementing x. There are times when you want to increment the counter within the loop if some special condition is met, but not on every loop.
If you want to manually increment, then you should use a while loop instead.
The Next x line, when referring to an integer (x) in a For loop, will increment x by 1
If you would like to increment by more than one, or increment in a negative direction you could use step at the end of the For line, which provides direction to the Next portion. Example:
For x = 10 to 0 step -1
msgbox x
next x
will result in 11 consecutive msgbox's displaying:
10
9
8
7
6
5
4
3
2
1
0
Depending on how you would like to control X you have different options:
Option 1:
Let the for-loop control everything and use "step".
In this case x will raise by y at each iteration. And you should not alter x within the loop.
Dim y As Integer
y = 1
For x = 1 To 10 step y
MsgBox x
Next x
Option 2:
If X depends on something happening within the loop and you don't want the for loop to alter x you could set step to 0.
For x = 1 To 10 step 0
MsgBox x
If x Mod 2 = 0 Then
x = x + 3
Else
x = x - 1
End If
Next x
Alternativ option
Having a for loop as in option 2 not altering the x would be bad practice. It is not wrong as the code is working just fine but a while loop would be a better solution.
Dim x As Integer
x = 1
Do While x < 10
MsgBox x
If x Mod 2 = 0 Then
x = x + 3
Else
x = x - 1
End If
Loop

Editing Excel Mod Formula

Could you please give me vba code that can solve this problem:
I want remainder in Mod Function can become equal to divisor.
Example: In normal situation Mod(132,12)=0 but I want when remainder is equal to divisor, last step of dividing that is dividing 12 on 12 doesn't do and remainder becomes 12.
Example
I wrote this code but it seems something is wrong. What's the problem?
Function XLMod(a, b)
XLMod = Int(a - (b * Int(a / b)))
If XLMod(a / 10, b) = 1 And XLMod(a, 10) = 2 Then
XLMod = b
End If
End Function
You need a special exception of the standard modulo function.
If the result of a normal division (a / b) would result in a number ending with 1 (e. g. 1, 31, 10001, 12341, ...), then you want it to return b.
Function XLMod(a, b)
XLMod = a Mod b
If XLMod = 0 And (a / b) Mod 10 = 1 Then XLMod = b
End Function

Table with 4 variable columns

I'm sure this is simple for all of you, but I'm new here. How do I create a formula or code that can output all of the potential scenarios for this type of array below? Basically, max is 60, min is 0, but I'm unsure how to make Excel spit out a table that represents this.
Solution:
Doing it with Excel formulas alone, while theoretically possible, is incredibly computationally demanding and crashed Excel when I was trying to do so. You can do this fairly easy with VBA though.
Create a VBA module, drop these two snippets in, press play, and wait for a few seconds to a minute while the code runs. The code is not the most efficient, but it is probably the simplest algorithm to understand.
Public Sub comb4()
Dim a, b, c, d, n, r, x As Integer
x = 60
a = x
Do While a >= n
b = x - a
Do While b >= n
c = x - a - b
Do While c >= n
d = x - a - b - c
Do While d >= n
If sumToZero(-x, a, b, c, d) Then
r = r + 1
Cells(r, 1).Value = a
Cells(r, 2).Value = b
Cells(r, 3).Value = c
Cells(r, 4).Value = d
End If
d = d - 1
Loop
c = c - 1
Loop
b = b - 1
Loop
a = a - 1
Loop
End Sub
Public Function sumToZero(ParamArray intNums())
For x = LBound(intNums) To UBound(intNums)
y = y + intNums(x)
Next x
If y = 0 Then sumToZero = True
End Function
Code explanation:
x is the max that you defined, while n is the min you defined.
r is a counter which helps us track what row to print to.
Since we always want to count down to 0 for each column's sub-permutations, each of our Do While loops will count from the theoretical maximum value down to 0.
The loop structure is nested so that each time we hit -1 in a column N we instead leave the loop, go into the loop one level higher, and decrease the value in column M by 1. The value in N is reset when we start the next instance of the loop for M.
Between each value change we need to check to see if the result is a valid solution. We pass the (negative) max and all 4 variables we are looping to a function which sums the values and returns true if the variables are equal to the max. When the function is true, we go to the next row in Excel and print the four values.

I want to solve a system of equations containing a*x

Is it possible to solve a system of three equations in Excel, that contain x*y??
Let's suppose that my unknowns are a,b,x
The equations are
a + b = 1
a * x - 20y = 0
10x * a - 20a + b = 0
Is there a way to express the multiplier that is one of my unknowns??
here you can take z=x*a then your equations become:
a + b = 1
z - 20y = 0
10z - 20a + b = 0
Now solve these equation using simple method given here. You get value of z and a. Now you can find value x. So finally you get value of a, b, z, y

Resources