Reputation: 4267
My problem is mainly a mathematic logic problem, more than a code problem.
I have an android app where I generate a division.
I have to generate a division which divisor and dividend are generated random from a specific range of number (let's say 10-1000) and which result is a number without dot.
For example:
A(10-1000) : B (10-1000) = C (number without dot)
Does anybody know how to reach my goal. Math is not a friend of mine.
Thank you
Upvotes: 0
Views: 158
Reputation: 15693
If you want the result of a division to be an integer, then you need to start with a multiplication. If A / B = C, then A = B * C. Start by picking two random integers ("no dot") B and C. Find A by multiplying then together. Ask "What is A / B?" and the answer will be the integer C.
Some pseudocode:
B <- 10 + rand(991) // B in [10..1000]
C <- 10 + rand(991) // C in [10..1000]
A <- B * C
display("What is " + A + " / " + B + "?")
read(answer)
if (answer == C)
display ("Correct. Well done.")
else
display("Wrong answer. The correct answer was " + C)
end if
Upvotes: 1
Reputation: 110
The "math" logic here (example values):
if((2 % 2.5) > 0) aka check if 2 is evenly divisible by 2.5
As long as the example above is false the randomly generated values wont return an integer or "a number without a dot".
Random random = new Random();
int lowerbound = 10;
long counter = 0;
while(true){
double a = random.nextInt(10000-lowerbound)+lowerbound+1;//a number between 10 and 10000
double b = random.nextInt(10000-lowerbound)+lowerbound+1;//a number between 10 and 10000
double result = (a/b);
if( (result % Math.round(result)) == 0){
System.out.println(a + "/" + b + "=" + result);
int ires = (int) Math.round(result);
System.out.println(ires);
break;
}
counter++;
}
Upvotes: 0