This question already has answers here:
How to get the real cube root of a negative number in Python3?
(2 answers)
Closed 3 years ago.
doing wrong account while getting python root
>>>(-27)**(1/3) #must be -3
>>>(1.5000000000000004+2.598076211353316j)
do you have any idea?
This is the correct answer for the usual branch cut for log.
If you start from 27 and go to -27 along the circle |z|=27 in the complex plane, then the cubic root will go from 3 to 1.5+2.6i if you follow the upper half-plane and to 1.5-2.6i if you follow the lower one.
PS. If you are unfamiliar with branch cuts, you can start with pretty pictures.
If you unfamiliar with complex numbers, start with fun.
Related
This question already has answers here:
how to randomly select a neighbor patch that has a higher elevation in netlogo
(2 answers)
Closed 7 years ago.
I've been tasked with a question of "randomly selecting a neighbour patch that has a higher elevation." My code is found below.
I believe I am required to use the "any?" command to prevent getting the 'nobody' issue.
to move-up
let myelevPatch [elevation] of patch-here
let higherpatches neighbors with [elevation > myelevPatch]
move-to one-of higherpatches
end
I can't seem to get around this problem, if you could help me get around it, it is very much appreciated
Your code seems to be right other than the nobody case. Any? consumes a agentset. neighbors with returns an agentset.
to move-up
let myelev [elevation] of patch-here
let higherpatches neighbors with [elevation > myelev]
if any? higherpatches
[move-to one-of higherpatches]
end
Note the following is equivalent:
if count higherpatches > 0
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Starting in the top left corner of a 2×2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner.
How many such routes are there through a 20×20 grid?
In the 2x2 case you have 6 possible ways you say. (So I infer from that that you actually move from grid-point to grid-point rather than from cell to cell):
(r,r,d,d), (r,d,r,d), (r,d,d,r), (d,r,d,r), (d,d,r,r), (d,r,r,d).
Note that we always have 2 'd's and 2 'r's. And we always have 4 moves (_ , _, _, _).
Also note, that if you just place the 'r's at the 4 moves, then it is implicitly clear where the 'd's will go, i.e. if you put a 'r' at position 1 and 3 then you will have an 'd' at positions 2 and 4.
Hence you can think of it as: "how many ways are there to distribute 2 'd's to 4 possible positions?". You have to choose 2 elements out of 4.
This is the well known binomial coefficient (https://en.wikipedia.org/wiki/Binomial_coefficient) "n choose k". In your case "4 choose 2" (among the 4 choices pick 2) which happens to be 6.
So, now I leave it as an exercise to you: on a 20x20 grid what is your 'n' and what is your 'k'?
The other problem you might run into is how to actually compute the value. But I'm sure you'll figure it out. Just have a close look at the wikipedia page about the binomial coefficient, you might find something useful there.
This question already has answers here:
how to get nearest tenth of a double
(2 answers)
Closed 7 years ago.
Given a number of type double, say d:
How can d be rounded/extracted to it's "most suitable" power of 10?
Example:
0.123 => 0.1
1.234 => 1
12.34 => 10
[At this point, I have not decided which behaviour I want for for example 0.99 (i.e if it should be 0.1 or 0.01 - any solution will do for now.]
I am using this in Java programming, so either some standard library function or just a simple mathematical solution (for any language) will do. (I can think of naive solutions like dividing d by ten and look for the first non zero number, but it feels too ugly)
I am sorry if I am not using the correct terminology in the question, please edit if you can formulate it better.
Compute the base-10 logarithm, using a language of your choice, and round the number up or down according to personal taste.
In Java, you can use Math.log10.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How is floating point stored? When does it matter?
Using the built-in calculator on my Win7 x64 I get the number -8.1648465955514287168521180122928e-39 when calculation sqrt(4)-2.
I would expect the result to be 0.
There's some error with floating-point values, when you go to subtract them on occasion. You may get a representation that's 0 or really close to 0 (10^-39's pretty close).
For more information, check out Fractions in Binary on Wikipedia.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Algorithm to find minimum number of weighings required to find defective ball from a set of n balls
We have n coins. One of them is fake, which is heavier or lighter (we don't know). We have scales with 2 plates. How can we get the fake coin in p moves?
Can you give me a hand for writing such a program? No need a whole program, just ideas.
Thank you.
This is known as Balance puzzle. See Marcel Kołodziejczyk’s Two-pan balance and generalized counterfeit coin problem for a generalization of this problem.
I remember solving this for n=12 and 13, partly by hand and then with a program at the end. I don't know how I would solve it for a general n... but I know how I'd start - by considering small values of n and doing it by hand.
I suspect there are essentially patterns that can be used recursively for this... but you'll find them much easier to discover with pen and paper for small values (n=4 to 7, for example) than by coding.
Put coins on each side, the real ones will balance each other out, the fake will make the scale go either way. When the scales aren't balanced, one of the 2 you just put on is fake, try each against a real coin.
If the coins are objects you're handed, then you should be able to do that in a program quite easily.