How to perform division in Go

The operands of the binary operation 3 / 10 are untyped constants. The specification says this about binary operations with untyped constants if the operands of a binary operation are different kinds of untyped constants, the operation and, for non-boolean operations, the result use the kind that appears later in this list: integer, rune, floating-point, … Read more

Designing function f(f(n)) == -n

You didn’t say what kind of language they expected… Here’s a static solution (Haskell). It’s basically messing with the 2 most significant bits: f :: Int -> Int f x | (testBit x 30 /= testBit x 31) = negate $ complementBit x 30 | otherwise = complementBit x 30 It’s much easier in a … Read more

Flipping a quaternion from right to left handed coordinates

I don’t think any of these answers is correct. Andres is correct that quaternions don’t have handedness (*). Handedness (or what I’ll call “axis conventions”) is a property that humans apply; it’s how we map our concepts of “forward, right, up” to the X, Y, Z axes. These things are true: Pure-rotation matrices (orthogonal, determinant … Read more

The possible number of binary search trees that can be created with N keys is given by the Nth catalan number. Why?

Since there are four proofs in the wikipedia article you referenced, it seems you aren’t looking for a mathematical explanation for the correspondence between the Catalan numbers and the permutations of a binary tree. So instead, here are two ways to try and intuitively visualise how the Catalan sequence (1, 2, 5, 14, 42, …) … Read more

Calculate angle between two Latitude/Longitude points

using this referance to calculate Angle: private double angleFromCoordinate(double lat1, double long1, double lat2, double long2) { double dLon = (long2 – long1); double y = Math.sin(dLon) * Math.cos(lat2); double x = Math.cos(lat1) * Math.sin(lat2) – Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon); double brng = Math.atan2(y, x); brng = Math.toDegrees(brng); brng = (brng + 360) % … Read more