math

How do I round a set of numbers while ensuring the total adds to 1

Hi, I'm looking for a way to round a set of numbers to the nearest rational number while still preserving the total of the set. I want to distribute the total value of '1' amongst a variable number of fields without allowing irrational numbers. So say I want to distribute the total across three fields. I don't want each value to be 0....

What is a ray intersection interval?

In the context of ray/box intersection, what exactly is "a valid intersection interval"? I've been searching through different tutorials, but it seems they mostly seem to expect this as a priori knowledge. ...

Can some one help solving this recurrence relation?

T(n) = 2T(n/2) + 0(1) T(n) = T(sqrt(n)) + 0(1) first one I use substitution method for n, logn, etc, all gave me wrong answers. Recurrence trees: I dont know if I can apply as the root will be a constant Can some one help? T ...

Understanding "randomness"

I cant get my head around this, which is more random? rand() OR rand() * rand() I´m finding it a real brain teaser, could you help me out? Thanks in advance! EDIT: Intuitively I know that the Mathematical answer will be that they are equally random but I can't help but think that if you "run the random number algorithm" twice wh...

Algorithm to select a set of numbers to reach a minimum total

Given A set of numbers n[1], n[2], n[3], .... n[x] And a number M I would like to find the best combination of n[a] + n[b] + n[c] + ... + n[?] >= M The combination should reach the minimum required to reach or go beyond M with no other combination giving a better result. Will be doing this in PHP so usage of PHP libraries is ok. I...

vb.net mantissa and exponent calculation from double

Hi there. Can anyone offer any advice on how to get the mantissa and exponent from a double in VB.net? I know I can do a string parse and some conversion to ints but I wondered if anyone had a mathematical equivalent formula that would allow me to do this? Many thanks ...

Is there any PHP or JavaScript math expression formatters (which adds brackets) ?

Google Calculator formats math expressions this way: 2+2/2 ---> 2 + (2 / 2) 2+2/2*PI ---> 2 + ((2 / 2) * PI) In others words - it adds brackets. Is there any similar PHP or JavaScript solutions to do the same thing? ...

Recalculate center math question

Want to recalculate x and y in a centered layout relative to the current viewers resolution. I have two numbers a set of coordinates x and y. x=140 y=80 x and y was recorded in a resolution sessionWidth, sessionHeight sessionWidth = 1024 sessionHeight = 400 Want to recalculate x and y so that they are relative to the viewers resol...

Fast factorial function in JavaScript

Looking for a really fast implementation of factorial function in JavaScript. Any suggests? ...

How to add coins from different currencies

We are teaching a class where we need to teach about currency. We would like our students to add and demonstrate using different currencies across the world. (e.g. Country=US, How much does 3 nickels + 2 pennies + 3 dimes) (e.g. Country=UK, ...) (e.g. Country=JAPAN, ...) (e.g. Country=CHINA, ....) (e.g. Country=AUSTRALIA, ...) Is ther...

algorithm to pick set of winners using different weights

Hello, I'm attempting to design an algorithm that does the following. Input: I've a set of keys (total n) that are mapped to set of properties. The properties contain the weight for each property and the value for the property. Output: Identify a set of keys that are qualified (total k) based on the set of properties and their ...

Visualizing a geometric problem with mathematica

I am trying to figure out a way to move two points, X and Y, independently of one another along the edges of an equilateral triangle with vertices A, B, and C. There are also some collision rules that need to be taken into account: (1) If X is at a vertex, say vertex A, then Y cannot be on A or on the edges adjacent to it. i.e., Y ca...

Ray transformation for ray-triangle intersection

In openGL, I have a 3D model I'm performing a ray-triangle intersection on, using the code explained in the paper "Fast, Minimum Storage Ray/Triangle Intersection" ( http://jgt.akpeters.com/papers/MollerTrumbore97/ ). My cursor position is unprojected into world space using the following code: bool SCamera::unproject(Vector3 input, Ve...

Find all possible row-wise sums in a 2D array

Ideally I'm looking for a c# solution, but any help on the algorithm will do. I have a 2-dimension array (x,y). The max columns (max x) varies between 2 and 10 but can be determined before the array is actually populated. Max rows (y) is fixed at 5, but each column can have a varying number of values, something like: 1 2 3 4 5 6 7.....

Jquery/Javascript Math: Why are these two 1 line math problems not giving the same answer?

Shouldn't these two math problems give the same answer? Brackets/parenthesis are done first, right? so it should add them all, then divide it by 2, then subtract 10. The second answer below is the one giving me the correct value that I need, the other one gives a value that's a long ways off. var pleft = $(this).offset().left + ($(t...

probability of picking maximum red balls

Given n red balls and m blue balls and some containers, how would you distribute those balls among the containers such that the probability of picking a red ball is maximized, assuming that the user randomly chooses a container and then randomly picks a ball from that? Please can anyone help me calculate this ...

What does abs do in Python?

I don't understand how the following code works: #CodingBat.com Warmup 7 #Given an int n, return True if it is within 10 of 100 or 200. def near_hundred(n): return ((abs(100 - n) <= 10) or (abs(200 - n) <= 10)) What does "abs()" do? And how does this work to solve the problem above? ...

Numerical precision in simple financial computations

Hi everyone I did a course at university that explained how (amongst other things) to order your mathematical execution to maximize precision and reduce the risk of rounding errors in a finite precision environment. We are working on a financial system with your usual interest calculations and such. Can somebody please share/remind me ...

How to calculate current position on a great circle path

Given a starting point (origLat, origLon), ending point (destLat, destlon), and a % of trip completed. How do I calculate the current position (curLat, curLon)? ...

Two's complement proof

Is it possible to prove by induction that the two's complement of any string of 0's will always result in 0, for all sequences of length n? I'm trying to do this using the value formula, i.e. value = -a_n-1 x 2^(n-1) + summation{i=0 to n} (a_i x 2^i), where n = number of bits in string ...