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
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
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
Cookies are required to use this site. You must accept them to continue using the site. Learn more…