Android Question generat maths question

Farzam

Member
hello
I want to make a math question by coding in the form of 3 numbers in math operations...
For example: 3-10*6 , the output should not be negative
Or: 10*2+5 , Priority is important .

But I don't want the result to be a negative number, or to observe the priority of the equation in the equations
 
Solution
Here is another suggestion :
B4X:
Private Sub generate As String
    Dim digit() As String = Array As String("1", "2", "3", "4", "5", "6", "7", "8", "9")
    Dim operator() As String = Array As String(" + ", " - ", " * ")
    Dim e As B4XEval
    e.Initialize(Me, "eval")
    Dim equation As String
    Dim result, discards As Int
    Do Until (result > 0)
        equation = digit(Rnd(0, 9)) & operator(Rnd(0, 3)) & digit(Rnd(0, 9)) & operator(Rnd(0, 3)) & digit(Rnd(0, 9))
        result = e.Eval(equation)
        If (result < 1) Then discards = discards + 1
    Loop
    Log("Equation = " & equation & " = " & result & ";  Discards = " & discards)
    Return equation
End Sub

. . . and some results . . .

Equation = 7 + 9 + 4 =...

aeric

Expert
Licensed User
Longtime User
Okay - I don't mind stealing other people's ideas, so encouraged by @Farzam's "Like" I used @emexes suggestion about division and @aeric's function to replace the operator signs and added a random fourth element to the equation and came up with the following . . .

B4X:
Private Sub generate As String
    Dim digit() As String = Array As String("2", "3", "4", "5", "6", "7", "8", "9")
    Dim operator() As String = Array As String(" + ", " - ", " * ", " / ")
    Dim e As B4XEval
    e.Initialize(Me, "eval")
    Dim equation As String
    Dim check As Float
    Dim result As Int
    Do Until (result > 0) And (result < 100)
        equation = digit(Rnd(0, 8)) & operator(Rnd(0, 3)) & digit(Rnd(0, 8)) & operator(Rnd(0, 3)) & digit(Rnd(0, 8))
        If (Rnd(1, 3) = 2) Then equation = equation & operator(Rnd(2, 4)) & digit(Rnd(0, 8))
        check = e.Eval(equation)                       ' Calculate float value
        result = Round(check)                          ' Convert to integer value
        If (Abs(result - check) > 0) Then result = 0   ' Discard if not an integer result
    Loop
    equation =  equation.Replace("*", "x").Replace("/", "÷")
    Log("Equation = " & equation & " = " & result)
    Return equation  
End Sub

Here are some sample results . . .



These results don't look too bad, but there are still snags. Note that I limit the result of the calculation to 99 - that seems a good idea. I allow only one division sign - allowing more than one can produce rather enigmatic equations which is not what we want here, I suspect. Also this simple code produces terms like "4 + 3 - 2 / 2" - that is division by oneself - more often than one might expect. So perhaps still more work to be done.
Just improve the code as you like.
 
Upvote 0

emexes

Expert
Licensed User
with two numbers 1..10 (and one operator) we can make all numbers up to 21
with three numbers 1..10 (and two operators) we can make all numbers up to 110
with four numbers 1..10 (and three operators) we can make all numbers up to 460

Still waiting for the next result. Seems to be exponential runtime.

with two numbers 1..10 (and one operator) we can make all numbers up to 21
with three numbers 1..10 (and two operators) we can make all numbers up to 110
with four numbers 1..10 (and three operators) we can make all numbers up to 460
with five numbers 1..10 (and four operators) we can make all numbers up to 1236
with six numbers 1..10 (and five operators) we can make all numbers up to 6822
with seven numbers 1..10 (and six operators) we can make all numbers up to 29625
with eight numbers 1..10 (and seven operators) we can make all numbers up to 104346
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Log($"Answer = ${answer.As(Int)}"$)
Previously, I got an answer 10.8xxxxxx and converted to Int = 10. It should be 11.

Take note that the result returned from e.Eval() is Double.

Guess what?
B4X:
Dim exp As String = "7 / 0"
Log(NumberFormat(e.Eval(exp), 1, 0))
It doesn't resulted an error.

Output Log:
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
I thought the OP wanted positive integer results, maybe I read the post wrong.
 
Upvote 0
Top