How can I randomly generate division elements a / b = c, where c has only one decimal number?

This is rather a basic math / programming issue. I need to generate division a / b = c. I will give the user a and b, and he should answer c, but c can only have one decimal number. For example, c = 5.2 is fine, but 5.23 is too much to ask because users are children. So I need an algorithm that can generate random division of this kind from all possible combinations, where a must be less than x, b must be less than y, and c can have only one decimal number. In addition, a and b must be integers.

I am looking for a more elegant solution than trying numbers and checking them until the right combinations are created. It is also for web content, so I would prefer to do it with javascript, but it can be done in php if necessary. ¿Does anyone know how I can do this? I'm not so good at math.

Thank you in advance.

+5
source share
5 answers

Try to create Chow int/10.
Then generate B:

  • just int if (10 * C) % 10 == 0
  • int * 2, if (10 * C) % 5 == 0
  • int * 5, if (10 * C) % 2 == 0
  • int * 10 else

Then A = B * Cthis is int

pseudo code:

tenC = rnd();
if(tenC % 10 == 0) B = rnd();
elseif(tenC % 5 == 0) B = rnd() * 2;
elseif(tenC % 2 == 0) B = rnd() * 5;
else B = rnd() * 10;
C = tenC / 10.0;
A = tenC * B / 10;

where rnd () generates an integer that you like

+2
source

A * 10, B.

        int[] primes = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 };
        List<int> factors = new List<int>();
        int x = 1000;
        int y = 650;
        Random rand = new Random();
        int a = rand.Next(x);
        int _a = a * 10;
        int currentPrime = 0;

        //find all factors of a * 10
        while (_a > 1)
        {
            while (_a % primes[currentPrime] == 0)
            {
                factors.Add(primes[currentPrime]);
                _a /= primes[currentPrime];
            }
            currentPrime++;
        }

        int b = 1;
        foreach(int factor in factors)
        {
            if (b * factor > y) break;
            if (rand.Next(2) == 0)
            {
                b *= factor;
            }
        }
+2
// Declare variables
var a, b, c, f = 1,
    highestCommonFactor = function(x, y) {
        var i;
        for (i = Math.min(x, y); i > 1; i--) {
            if (x / i == Math.round(x / i) && y / i == Math.round(y / i)) {
                return i;
            }
        }
        return false;
    };

// Pick two random numbers, b and c
// b should be a fairly small integer, i'll choose 5 as an upper limit
// c should be larger than 10 and not end with a 0
b = Math.floor((Math.random() * 5) + 1);
do {
    c = Math.floor(Math.random() * 100);
} while (c.toString().slice(-1) == "0");

// Multiply b by 10
b *= 10;

// Get a value for a
a = (c * b) / 10;

// Divide c by 10
c /= 10;

// Optional - Cancel a and b down to lowest possible integers
/*
while (f !== false) {
    a /= f;
    b /= f;
    f = highestCommonFactor(a, b);
}
//*/

, . a b, 10, - (b 10, ). , , , c 1 , int / 10.

Fiddle

+1
function findEquation($maxA, $maxB){
    $x = 2;
    $e = NULL;

    do {
        $a = rand(1, $maxA);
        $b = rand(1, $maxB);
        $e = "$a / $b";
        $y = explode(".", $a/$b);
        if(count($y) > 1){
            $x = strlen($y[1]);
        }else {
            $x = 0;
        }
    } while($x > 1);

    return $e;
}

echo findEquation(100,100);

, , < 2.

JavaScript:

function findEquation(maxA, maxB){
    var x = 2;
    var e = '';
    var a,b,c,d;

    do {
        a = Math.floor(Math.random() * maxA+1);
        b = Math.floor(Math.random() * maxB+1);
        e = a + '/' + b;
        c = (a/b).toString().split(".");
        if(c.length > 1){
            x = c[1].length;
        }else {
            x = 0;
        }
    } while( x > 1 );

    return e;
}

alert(findEquation(100,100));
0

http://jsfiddle.net/eX7fM/2/

var upper = getRandomInt(2, 100);

var lower;
var answer = 0.99; // sentinel

while (!numberHasOneOrNoDecimals(answer)) {
    lower = getRandomInt(1, upper);
    answer = upper / lower;
}

It’s still connected with some cycles, but it’s a little more elegant than pure brute force, and it’s much easier to understand how the non-cylindrical approach will be used (simple factorization and all this jazz is higher than my mathematical skill).

0
source

All Articles