|
Last change
on this file since 2058e5c was 2058e5c, checked in by istevanoska <ilinastevanoska@…>, 6 months ago |
|
Working / before login
|
-
Property mode
set to
100644
|
|
File size:
869 bytes
|
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | Given the ratio a : b : c = 2 : 3 : 4
|
|---|
| 3 | What is c, given a = 40?
|
|---|
| 4 |
|
|---|
| 5 | A general ratio chain is a_1 : a_2 : a_3 : ... : a_n = r_1 : r2 : r_3 : ... : r_n.
|
|---|
| 6 | Now each term can be expressed as a_i = r_i * x for some unknown proportional constant x.
|
|---|
| 7 | If a_k is known it follows that x = a_k / r_k. Substituting x into the first equation yields
|
|---|
| 8 | a_i = r_i / r_k * a_k.
|
|---|
| 9 |
|
|---|
| 10 | Given an array r and a given value a_k, the following function calculates all a_i:
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | function calculateRatios(r, a_k, k) {
|
|---|
| 14 | const x = Fraction(a_k).div(r[k]);
|
|---|
| 15 | return r.map(r_i => x.mul(r_i));
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | // Example usage:
|
|---|
| 19 | const r = [2, 3, 4]; // Ratio array representing a : b : c = 2 : 3 : 4
|
|---|
| 20 | const a_k = 40; // Given value of a (corresponding to r[0])
|
|---|
| 21 | const k = 0; // Index of the known value (a corresponds to r[0])
|
|---|
| 22 |
|
|---|
| 23 | const result = calculateRatios(r, a_k, k);
|
|---|
| 24 | console.log(result); // Output: [40, 60, 80]
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.