Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I came across this in some brain teaser question bank.
What should be the answer of this?
( (3+1) / 3) * 6
I thought it is 7 but the correct answer is 8.
That can happen only when it is (3+1) * (6/3). Since there are parenthesis given in the expression how can I get 8? I tried to double check and the answer is correct.
Can somebody help me in understanding this?
Working with integers:
((3 + 1) / 3) * 6
= (4 / 3) * 6
= 1 * 6
= 6
Working with floats:
((3 + 1) / 3) * 6
= (4 / 3) * 6
= (4 / 3) * (6 / 1)
As a fraction:
= (6 * 4) / (3 * 1)
= 24 / 3
= 8
This is just simple arithmetic - nothing to do with programming, nor the difference between infix, prefix and postfix notations:
((3+1) / 3) * 6 = (4 / 3) * 6
= 1.333... * 6
= 8
This brain teaser question bank is clearly expecting you to use the usual rules of mathematics - it's not a C brain teaser question bank after all. Even if you were using integer division instead (where 4 / 3 would give 1), the answer would be 6.
See the result on Wolfram Alpha.
By the sound of it, this is basic arithmetic and has nothing whatsoever to do with programming:
(3 + 1) / 3 * 6 =
4 / 3 * 6 =
(4 * 6) / 3 =
24 / 3 =
8
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
My code:
import Pkg
Pkg.add("PyPlot")
using PyPlot
e = 0.0
eta = 1e - 4
wmin = -2.0
wmax = 2.0
Nw = 1000
w =range(wmin, wmax, Nw)
g = 1 ./ (w .- e .+ eta * im)
t = 1.0
g2 = g .^2
Gsemi = (1 ./ (g*2*t^2)) .* (1 .- sqrt(1 .- 4*t^2*g .^2)) //Error line
Ginf = Gsemi ./ (1 .- Gsemi .^ 2 * t^2)
xlabel(L"Energy $\omega $", fontsize=20)
ylabel("Density of states", fontsize=20)
axis([-2,2,0,1.4])
plot(w, (-1.0/pi)*imag(Ginf), linewidth=3.0)
The error message:
How can I fix this? sorry my english is bad, thank for anwers
This particular error happens because you cannot take the square root of a vector, since it does not make sense mathematically.
But you can take the square root of each element separately. To do that add a dot to the sqrt function call:
sqrt.(1 .- 4*t^2*g .^2)
But you should really add more dots on that line to save some temporary allocations. You can do that with the #. macro instead of dotting everything by hand, like this:
Gsemi = #. (1 / (g*2*t^2) * (1 - sqrt(1 - 4*t^2*g^2)))
Now, all operations will be element-wise.
Also, make sure to put your code inside a function.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
I used PEMDAS on 3 - 1 * 8 + 2 * 3.
Steps:
1) 1 * 8 = 8
2) 2 * 3 = 6
3) 8 + 6 = 14
4) 3 - 14 = -11
Multiply all the terms, then add and finally subtract but I get -11 as the result.
But when I googled it, it said 1. Where did I go wrong?
By Following the BODMAS rule -
3 - 1 * 8 + 2 * 3
So According to BODMAS Rule-
B → Brackets first (parentheses)
O → Of (orders i.e. Powers and Square Roots, Cube Roots, etc.)
DM → Division and Multiplication (start from left to right)
AS → Addition and Subtraction (start from left to right)
So our equation ->
3 - (1 * 8) + (2 * 3)
3-8+6
-5+6
1
After multiply all the terms,please follow the order of operations.Right steps:
Steps: 1) 1 * 8 = 8 2) 2 * 3 = 6 3)3-8=-5 4)-5+6=1
The order of operations is, as you said it PEMDAS (or BEDMAS whatever you like), but it is also left-to-right. And on top of that: multiplication and division are treated as the same order and so are addition and subtraction.
So your first two steps were right.
3 - (1 * 8) + (2 * 3)
3 - 8 + 6
Now here is where the left-to-right ordering takes place.
((3 - 8) + 6)
(-5 + 6)
1
To make it easier, you can remember that x - y is really just x + (-y). Then the order of subtraction and addition doesn't matter at all.
You are not following the order of operations correctly. You are correct in doing the multiplication first. However, you also must always go from left to right when performing operations of the same type (addition and subtraction or multiplication and division). By using parenthesis, we can make this clearer.
3 - (1*8) + (2*3) => 3 - 8 + 6 => -5 + 6 => 1
Your mistake is in step 3. The - gets applied to 8 so step 3 is: -8 + 6 = -2
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Can someone help with the following MATH question.
Keep in mind the operator precedence.
x = 27 * 24 \ 4 / 2 – 4 + 2
Arithmetic and logical operators are evaluated in the following order of precedence:
Exponentiation (^)
Negation (-)
Multiplication and division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
What is the result?
x = ((27 * 24) \ (4 / 2)) - 4 + 2
= (648 \ 2) - 4 + 2
= 324 - 4 + 2
= 322
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have downloaded a MetaTrader MQL4 language .mq4 source-code file from here and I think there is a divide by zero error contained in the file. The relevant section is:
// Calculate sums for the least-squares method
n = ( Taps - 1 ) / 2;
sx2 = ( 2*n + 1 ) / 3.0;
sx3 = n * ( n + 1 ) / 2.0;
sx4 = sx2 * ( 3*n*n + 3*n - 1 ) / 5.0;
sx5 = sx3 * ( 2*n*n + 2*n - 1) / 3.0;
sx6 = sx2 * ( 3*n*n*n*( n + 2 ) - 3*n + 1 ) / 7.0;
den = sx6 * sx4 / sx5 - sx5; // <---------------------------- a DIV!0 error here?
This demo-code case:
Am I correct in my assumption that there is an error in the code,and if so,perhaps someone could point out what the correction should be?
General computation cases:
What is the industry best-practice / what practical software-design measures ought be used as a life-jacket protection for DIV!0 incident(s)?
A division by zero will occur if sx5 is zero. To find what would cause sx5 to be zero, solve sx5=0 for n.
0 = sx5
0 = sx3 * (2*n*n + 2*n - 1) / 3.0
0 = (n*(n + 1) / 2.0) * (2*n*n + 2*n - 1) / 3.0
...
0 = 2*n^4 + 4*n^3 + n^2 - n
One possible solution to that equation is n=0, so a division by zero error will occur if Taps is 1. I don't know if the equation has other solutions.
Update: Added math.
den = sx6*sx4 / sx5 - sx5; // error here?
sx5 - sx5 is presumably always equal to zero, so yes a divide by zero error will occur here.
Edit: Nevermind, this is incorrect
I don't think so, first the multiplication is done, sx6*sx4, then that is divided by sx5, then sx5 is subtracted from the final result.
Imagine I have this list, that is divided by 3
1
2
3
4
5
6
7
8
9
Now, I have 9 items, grouped in 3 sections.
My question is how know in which section is 6 (ie: 6 belong to section 2, 2 to section 1, 9 to section 3)
Hmmm...... section = ((item-1) / 3) + 1
section = ceiling (n / 3)
For example,
ceiling (4 / 3) = ceiling ( 1.33 ) = 2
For a list of items divided into sections of size n, the section s of an item i is given by:
s = (i + (n-1)) / n,
where the / is integer division.
So, for your example, item 6 gives (6 + (3-1))/3 = (6+2)/3 = 8/3 = 2.
This applies to many other things as well - I encountered it as "How many nodes do I need to request on a cluster with n CPUs per node?"
I'm not totally sure what you're asking, but give this a try:
floor((itemNumber - 1)/numberOfGroups) + 1