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 =...

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Shelby

Well-Known Member
Licensed User
Longtime User
Reverse the order of all characters.
6*10-3
results=57? or 42? (not negative)
I'm just being silly without using parentheses.
 
Last edited:
Upvote 0

Farzam

Member
Maybe the OP means
Log(abs(e.Eval("3-10*6")))
?
Well, now, in this calculation, first multiplication is calculated and then subtraction...
The output is -57

I don't want the output to be a negative number
With this command, I do the operation with 2 numbers, now I want it to be 3 numbers

B4X:
Sub GenerateQuestion1(minValue As Int,maxValue As Int) As QuestionModel
    Dim operations() As String = Array As String("+", "-", "x", "÷")
    Dim operation As String = operations(Rnd(0, operations.Length))
    Dim a, b,answer As Int
    Select operation
        Case "+"
            a = Rnd(minValue, maxValue)
            b = Rnd(minValue, maxValue)
            answer = a + b
        Case "-"
            a = Rnd(minValue, maxValue)
            b = Rnd(minValue, a + 1)
            answer = a - b
        Case "x"
            a = Rnd(minValue, maxValue)
            b = Rnd(minValue, maxValue)
            answer = a * b
        Case "÷"
            b = Rnd(minValue, maxValue)
            answer = Rnd(minValue, maxValue)
            a = answer * b
    End Select
    
    L_Quzie.Text = a & " " & operation & " " & b & " = ?"

    AnsverNum=answer

    
End Sub
 
Upvote 0

emexes

Expert
Licensed User
I want to create a mathematical equation with 3 or 4 numbers...
For example: 3+5*2-10

Is this in the ballpark of what you're trying to create?
(creating numbers from 1 to 100 using exactly three numbers from 1 to 10)

Log output:
Waiting for debugger to connect...
Program started.
1 = ((1+1)-1)
2 = ((1+1)*1)
3 = ((1+1)+1)
4 = ((1+1)+2)
5 = ((1+1)+3)
6 = ((1+1)*3)
7 = ((1+1)+5)
8 = ((1+1)*4)
9 = ((1+1)+7)
10 = ((1+1)*5)
11 = ((1+1)+9)
12 = ((1+1)*6)
13 = (1+(2*6))
14 = ((1+1)*7)
15 = ((1+2)*5)
16 = ((1+1)*8)
17 = (1+(2*8))
18 = ((1+1)*9)
19 = (1+(2*9))
20 = ((1+1)*10)
21 = ((1+2)*7)
22 = ((1+10)*2)
23 = ((3*8)-1)
24 = ((1+2)*8)
25 = (1+(3*8))
26 = ((3*9)-1)
27 = ((1+2)*9)
28 = ((1+3)*7)
29 = ((3*10)-1)
30 = ((1+2)*10)
31 = (1+(3*10))
32 = ((1+3)*8)
33 = ((1+10)*3)
34 = ((5*7)-1)
35 = ((1+4)*7)
36 = ((1+3)*9)
37 = (1+(4*9))
38 = (2+(4*9))
39 = ((4*10)-1)
40 = ((1+3)*10)
41 = (1+(4*10))
42 = ((1+5)*7)
43 = (1+(6*7))
44 = ((1+10)*4)
45 = ((1+4)*9)
46 = (1+(5*9))
47 = ((6*8)-1)
48 = ((1+5)*8)
49 = ((5*10)-1)
50 = ((1+4)*10)
51 = (1+(5*10))
52 = (2+(5*10))
53 = ((6*9)-1)
54 = ((1+5)*9)
55 = ((1+10)*5)
56 = ((1+6)*8)
57 = (1+(7*8))
58 = ((6*10)-2)
59 = ((6*10)-1)
60 = ((1+5)*10)
61 = (1+(6*10))
62 = ((7*9)-1)
63 = ((1+6)*9)
64 = ((1+7)*8)
65 = (1+(8*8))
66 = ((1+10)*6)
67 = ((7*10)-3)
68 = ((7*10)-2)
69 = ((7*10)-1)
70 = ((1+6)*10)
71 = (1+(7*10))
72 = ((1+7)*9)
73 = (1+(8*9))
74 = (2+(8*9))
75 = ((3*5)*5)
76 = (4+(8*9))
77 = ((1+10)*7)
78 = ((8*10)-2)
79 = ((8*10)-1)
80 = ((1+7)*10)
81 = ((1+8)*9)
82 = (1+(9*9))
83 = (2+(9*9))
84 = ((2*6)*7)
85 = (4+(9*9))
86 = ((9*10)-4)
87 = ((9*10)-3)
88 = ((1+10)*8)
89 = ((9*10)-1)
90 = ((1+8)*10)
91 = (1+(9*10))
92 = (2+(9*10))
93 = (3+(9*10))
94 = (4+(9*10))
95 = (5*(9+10))
96 = ((2*6)*8)
97 = ((10*10)-3)
98 = ((2*7)*7)
99 = ((1+10)*9)
100 = ((1+9)*10)
Program terminated (StartMessageLoop was not called).
 
Upvote 0

emexes

Expert
Licensed User
Is this in the ballpark of what you're trying to create?
(numbers from 1 to 100 using exactly three numbers from 1 to 10)

Log output:
Waiting for debugger to connect...
Program started.
1 = ((1+1)-1)
2 = ((1+1)*1)
3 = ((1+1)+1)
4 = ((1+1)+2)
5 = ((1+1)+3)
6 = ((1+1)*3)
7 = ((1+1)+5)
8 = ((1+1)*4)
9 = ((1+1)+7)
10 = ((1+1)*5)
11 = ((1+1)+9)
12 = ((1+1)*6)
13 = (1+(2*6))
14 = ((1+1)*7)
15 = ((1+2)*5)
16 = ((1+1)*8)
17 = (1+(2*8))
18 = ((1+1)*9)
19 = (1+(2*9))
20 = ((1+1)*10)
21 = ((1+2)*7)
22 = ((1+10)*2)
23 = ((3*8)-1)
24 = ((1+2)*8)
25 = (1+(3*8))
26 = ((3*9)-1)
27 = ((1+2)*9)
28 = ((1+3)*7)
29 = ((3*10)-1)
30 = ((1+2)*10)
31 = (1+(3*10))
32 = ((1+3)*8)
33 = ((1+10)*3)
34 = ((5*7)-1)
35 = ((1+4)*7)
36 = ((1+3)*9)
37 = (1+(4*9))
38 = (2+(4*9))
39 = ((4*10)-1)
40 = ((1+3)*10)
41 = (1+(4*10))
42 = ((1+5)*7)
43 = (1+(6*7))
44 = ((1+10)*4)
45 = ((1+4)*9)
46 = (1+(5*9))
47 = ((6*8)-1)
48 = ((1+5)*8)
49 = ((5*10)-1)
50 = ((1+4)*10)
51 = (1+(5*10))
52 = (2+(5*10))
53 = ((6*9)-1)
54 = ((1+5)*9)
55 = ((1+10)*5)
56 = ((1+6)*8)
57 = (1+(7*8))
58 = ((6*10)-2)
59 = ((6*10)-1)
60 = ((1+5)*10)
61 = (1+(6*10))
62 = ((7*9)-1)
63 = ((1+6)*9)
64 = ((1+7)*8)
65 = (1+(8*8))
66 = ((1+10)*6)
67 = ((7*10)-3)
68 = ((7*10)-2)
69 = ((7*10)-1)
70 = ((1+6)*10)
71 = (1+(7*10))
72 = ((1+7)*9)
73 = (1+(8*9))
74 = (2+(8*9))
75 = ((3*5)*5)
76 = (4+(8*9))
77 = ((1+10)*7)
78 = ((8*10)-2)
79 = ((8*10)-1)
80 = ((1+7)*10)
81 = ((1+8)*9)
82 = (1+(9*9))
83 = (2+(9*9))
84 = ((2*6)*7)
85 = (4+(9*9))
86 = ((9*10)-4)
87 = ((9*10)-3)
88 = ((1+10)*8)
89 = ((9*10)-1)
90 = ((1+8)*10)
91 = (1+(9*10))
92 = (2+(9*10))
93 = (3+(9*10))
94 = (4+(9*10))
95 = (5*(9+10))
96 = ((2*6)*8)
97 = ((10*10)-3)
98 = ((2*7)*7)
99 = ((1+10)*9)
100 = ((1+9)*10)
Program terminated (StartMessageLoop was not called).
 
Upvote 0

emexes

Expert
Licensed User
I guess the OP is trying to ...

Don't you just love these open-ended questions?

Anyway, I've gotten as far as:

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. Although, to be fair, my code is pretty lax, aka: simple and programmer friendly.
 
Last edited:
Upvote 0

aeric

Expert
Licensed User
Longtime User
Here is my attempt. Of course here we don't like speculate.
B4X:
Sub Play
    Dim e As B4XEval
    e.Initialize(Me, "Eval")
    
    Dim q1 As String = GenerateExpression(1, 10)
    Log(q1)
    Dim q2 As String = ConvertExpressionToMath(q1)
    Log(q2)
    
    Dim answer As Int = NumberFormat(e.Eval(q1), 1, 0)
    Log($"Answer = ${answer}"$)
    
    Log("Convert to absolute value")
    Log($"New Answer = ${Abs(answer)}"$)
End Sub

Sub GenerateExpression (minValue As Int, maxValue As Int) As String
    Dim Operators(4) As String = Array As String("+", "-", "*", "/")
    Dim expression As String
    Dim NumofOperands As Int = Rnd(2, 5)
    Dim Operands(NumofOperands) As Int
    For i = 0 To NumofOperands - 1
        If i > 0 Then
            expression = expression & " " & Operators(Rnd(0, Operators.Length))
        End If
        Operands(i) = Rnd(minValue, maxValue + 1)
        'Log($"${i}=${Operands(i)}"$)
        expression = expression & " " & Operands(i)
    Next
    Return expression
End Sub

Sub ConvertExpressionToMath (Expression As String) As String
    Return Expression.Replace("*", "x").Replace("/", "÷") & " = ?"
End Sub

Output logs:
B4X:
 3 - 8 * 9 + 3
 3 - 8 x 9 + 3 = ?
Answer = -66
Convert to absolute value
New Answer = 66
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
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 = 20; Discards = 0
Equation = 9 - 8 + 9 = 10; Discards = 0
Equation = 7 - 2 + 4 = 9; Discards = 0
Equation = 5 + 4 - 3 = 6; Discards = 2
Equation = 9 * 5 * 5 = 225; Discards = 0
Equation = 8 + 2 + 7 = 17; Discards = 0
Equation = 7 + 3 + 4 = 14; Discards = 0
Equation = 3 * 9 * 5 = 135; Discards = 1
Equation = 3 * 1 + 8 = 11; Discards = 0
Equation = 7 * 2 - 1 = 13; Discards = 0

Of course, we have not introduced division here; that would be much more complicated, assuming we are looking for integer results.
 
Last edited:
Upvote 1
Solution

emexes

Expert
Licensed User
we have not introduced division here; that would be much more complicated

That's what I expected too, but turned out to not be that bad:

B4X:
Case 4:    'division
    If V(J) = 0 Then    'exclude division-by-zero
        OkFlag = False
    Else If V(I) Mod V(J) <> 0 Then    'exclude non-integer results
        OkFlag = False
    Else
        MM(II) = "(" & M(I) & "/" & M(J) & ")"    'the resultant math
        VV(II) = V(I) / V(J)    'and corresponding resultant value
        If VV(II) < 0 Then OkFlag = False    'exclude negative results
    End If
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
@aeric I changed your play sub a little bit
B4X:
Sub Play
    Dim e As B4XEval
    Dim q1 As String
    Dim answer As Double = -1
    e.Initialize(Me, "Eval")
    Do While answer < 0 Or (answer <> answer.As(Int))
        q1 = GenerateExpression(1, 10)
        answer = e.Eval(q1)
    Loop
    Log(q1)
    Log(NumberFormat(e.Eval(q1), 1, 0))
    Log($"Answer = ${answer.As(Int)}"$)
End Sub

It should produce only positive integer results
 
Last edited:
Upvote 0

Farzam

Member
Here is my attempt. Of course here we don't like speculate.
B4X:
Sub Play
    Dim e As B4XEval
    e.Initialize(Me, "Eval")
   
    Dim q1 As String = GenerateExpression(1, 10)
    Log(q1)
    Dim q2 As String = ConvertExpressionToMath(q1)
    Log(q2)
   
    Dim answer As Int = NumberFormat(e.Eval(q1), 1, 0)
    Log($"Answer = ${answer}"$)
   
    Log("Convert to absolute value")
    Log($"New Answer = ${Abs(answer)}"$)
End Sub

Sub GenerateExpression (minValue As Int, maxValue As Int) As String
    Dim Operators(4) As String = Array As String("+", "-", "*", "/")
    Dim expression As String
    Dim NumofOperands As Int = Rnd(2, 5)
    Dim Operands(NumofOperands) As Int
    For i = 0 To NumofOperands - 1
        If i > 0 Then
            expression = expression & " " & Operators(Rnd(0, Operators.Length))
        End If
        Operands(i) = Rnd(minValue, maxValue + 1)
        'Log($"${i}=${Operands(i)}"$)
        expression = expression & " " & Operands(i)
    Next
    Return expression
End Sub

Sub ConvertExpressionToMath (Expression As String) As String
    Return Expression.Replace("*", "x").Replace("/", "÷") & " = ?"
End Sub

Output logs:
B4X:
 3 - 8 * 9 + 3
 3 - 8 x 9 + 3 = ?
Answer = -66
Convert to absolute value
New Answer = 66
thank you brother
 
Upvote 0

Farzam

Member
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 . . .



Of course, we have not introduced division here; that would be much more complicated, assuming we are looking for integer results.

Thank you brother, your code was used
 
Upvote 0

Brian Dean

Well-Known Member
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 . . .

Equation = 7 x 2 - 6 = 8
Equation = 6 + 3 - 4 = 5
Equation = 4 x 7 + 8 = 36
Equation = 5 + 9 - 2 = 12
Equation = 2 x 5 + 8 ÷ 2 = 14
Equation = 9 + 2 + 9 x 7 = 74
Equation = 5 + 5 + 3 x 7 = 31
Equation = 7 + 2 x 4 = 15
Equation = 6 x 7 + 3 = 45
Equation = 4 + 5 + 5 = 14

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.
 
Upvote 0
Top