[Solved] Align the cube’s nearest face to the camera [closed]

Without getting into a bunch of math, here is a strategy. Identify the face nearest to the camera under some criteria. Two possible criteria for determining the closest face are: a. Finding the closest face based on the Euclidian distance between the face centroid and the camera’s centroid. b. Determine which face normal vector is … Read more

[Solved] Calculate price based on interval [closed]

function calculatePrice($numberOfUnits) { // Initialise price $price = 0; // Prices: amount for category => price for category // Starts with largest category $prices = array(150 => 1.5, 100 => 2, 50 => 2.5, 0 => 3); // Loop over price categories foreach($prices as $categoryAmount => $categoryPrice) { // Calculate the numbers that fall into … Read more

[Solved] Factorial Code issue

The clever authors of the second snippet are rolling their own method of dealing with a very large number such as 50!. Is rather like a multiprecision library and factorial algorithm rolled into one. The first snippet has no such consideration. Even 21! will overflow a signed 64 bit integral type, with undefined results. solved … Read more

[Solved] How to convert decimal to binary?

Just like you divide this 18 by 2 repeatedly to form a decimal representation for it, you need to do the reverse to convert the decimal part of the number to binary. You need to multiply that decimal portion of the number by 2 repeatedly until it gives a standalone digit. The result(product) of the … Read more

[Solved] Why is the output Negative? [closed]

Many notations use “^” as a power operator, but in PHP (and other C-based languages) that is actually the XOR operator. You need to use this ‘pow’ function, there is no power operator. In your code $keone = $vone ^ 2; Should be $keone = pow($vone,2); Rest of your code is fine. That pow function … Read more

[Solved] Jensen-Bregman LogDet divergence [closed]

As I understand it the function (of two symmetric positive definite matrices) is JL(X,Y) = log( det( (X+Y)/2)) – log( det( X*Y))/2 = log( det( (X+Y)/2)) – (log(det(X)) + log(det(Y)))/2 I reckon the way to do this is to compute the cholesky factorisations of X, Y and (X+Y)/2, that is find lower triangular matrices L,M,N … Read more

[Solved] polygon coordinates

Imagine a circle of radius r. It is like a regular polygon with an infinite number of sides. Trigonometry tells us: x = r * cos(a); y = r * sin(a); We know there are 360 degrees or 2pi radians in a circle. So to draw it we would start with angle = 0, calculate … Read more

[Solved] How to do modulo calculation? [closed]

Modulo arithemtic means that you should use corresponding remainder, not the value itself. In your case: 5 + 25 (mod 26) == 30 % 26 == 4 // <- “%” is taking remainder after dividing on 26 Whatever operation you do in mod 26, the answer will be in 0..25 range. Sample code (C#): int … Read more

[Solved] Why do round() and math.ceil() give different values for negative numbers in Python 3? [duplicate]

The functions execute different mathematical operations: round() rounds a float value to the nearest integer. If either of the two integers on either side are equal distance apart, the even number is picked. Here, the nearest integer is always -6; for -5.5 the nearest even integer is -6, not -5. math.ceil() returns the smallest integer … Read more

[Solved] ValueError: math domain error

Your traceback indicates you are passing a negative number to the math.sqrt() function: >>> from math import sqrt >>> sqrt(4.5 – 5.0) Traceback (most recent call last): File “<stdin>”, line 1, in <module> ValueError: math domain error >>> sqrt(-1.0) Traceback (most recent call last): File “<stdin>”, line 1, in <module> ValueError: math domain error Don’t … Read more

[Solved] Getting the prime numbers [closed]

I guess I found the solution. At least, I found the answer I need. Here is my answer import java.math.BigInteger; public class Problem7 { public static void main(String[]args) { int i = 0; int counter = 0; while(true) { if(i>6) { break; } if(i>1) { String str = String.valueOf(i); if (new BigInteger(str).isProbablePrime(i/2)) { System.out.println(str); counter++; … Read more