# Solve Mathematical Problems

## Overview

Use `DataInterpreter`

to solve Mathematical problems randomly selected from the level5 level of the Math dataset.

## Example

### Task

At a school, all 60 students play on at least one of three teams: Basketball, Soccer, and Mathletics. 8 students play all three sports, half the students play basketball, and the ratio of the size of the math team to the size of the basketball team to the size of the soccer team is $4:3:2$. How many students at the school play on exactly two teams?

### Code

```
import asyncio
from metagpt.roles.interpreter import `DataInterpreter`
async def main(requirement: str = ""):
interpreter = `DataInterpreter`(use_tools=False, goal=requirement)
await interpreter.run(requirement)
if __name__ == "__main__":
problem = "At a school, all 60 students play on at least one of three teams: Basketball, Soccer, and Mathletics. 8 students play all three sports, half the students play basketball, and the ratio of the size of the math team to the size of the basketball team to the size of the soccer team is $4:3:2$. How many students at the school play on exactly two teams?"
requirement = (
f"This is a math problem:{problem}. You can analyze and solve it step by step or use Python code to solve it."
)
asyncio.run(main(requirement))
```

```
import asyncio
from metagpt.roles.interpreter import `DataInterpreter`
async def main(requirement: str = ""):
interpreter = `DataInterpreter`(use_tools=False, goal=requirement)
await interpreter.run(requirement)
if __name__ == "__main__":
problem = "At a school, all 60 students play on at least one of three teams: Basketball, Soccer, and Mathletics. 8 students play all three sports, half the students play basketball, and the ratio of the size of the math team to the size of the basketball team to the size of the soccer team is $4:3:2$. How many students at the school play on exactly two teams?"
requirement = (
f"This is a math problem:{problem}. You can analyze and solve it step by step or use Python code to solve it."
)
asyncio.run(main(requirement))
```

### Execution Results

`DataInterpreter`

proposes the following solution steps:

```
[
{
"task_id": "1",
"dependent_task_ids": [],
"instruction": "Calculate the total number of students playing basketball."
},
{
"task_id": "2",
"dependent_task_ids": ["1"],
"instruction": "Determine the number of students on each team using the given ratios."
},
{
"task_id": "3",
"dependent_task_ids": ["1", "2"],
"instruction": "Use the inclusion-exclusion principle to find the number of students playing on exactly two teams."
}
]
```

```
[
{
"task_id": "1",
"dependent_task_ids": [],
"instruction": "Calculate the total number of students playing basketball."
},
{
"task_id": "2",
"dependent_task_ids": ["1"],
"instruction": "Determine the number of students on each team using the given ratios."
},
{
"task_id": "3",
"dependent_task_ids": ["1", "2"],
"instruction": "Use the inclusion-exclusion principle to find the number of students playing on exactly two teams."
}
]
```

Since half the students are on the basketball team, it is easy to calculate the total number of basketball team members; Then the total number of math team and soccer team can be calculated according to the proportion of the number of math team, basketball team and soccer team. Finally, the answer is obtained according to the exclusion principle. It can be seen that the steps planned by `DataInterpreter`

are very reasonable and in line with human thinking of solving mathematical problems.

`DataInterpreter`

writes the following code:

```
# ------------------------------task1------------------------------
total_students = 60
half_basketball = total_students / 2
half_basketball
# -------------------------------task2-----------------------------
basketball_team = 30
ratio_math_basketball_soccer = [4, 3, 2]
# Calculate the total ratio units
total_ratio_units = sum(ratio_math_basketball_soccer)
# Calculate the number of students per ratio unit
students_per_unit = basketball_team / ratio_math_basketball_soccer[1]
# Calculate the number of students on each team
math_team = students_per_unit * ratio_math_basketball_soccer[0]
soccer_team = students_per_unit * ratio_math_basketball_soccer[2]
(math_team, basketball_team, soccer_team)
# ------------------------------task3-------------------------------
# Given values from previous tasks
math_team = 40.0
basketball_team = 30
soccer_team = 20.0
students_all_three = 8
# Total number of students
total_students = 60
# Calculate the number of students playing in exactly two teams using the inclusion-exclusion principle
students_two_teams = (math_team + basketball_team + soccer_team) - total_students - (2 * students_all_three)
students_two_teams
```

```
# ------------------------------task1------------------------------
total_students = 60
half_basketball = total_students / 2
half_basketball
# -------------------------------task2-----------------------------
basketball_team = 30
ratio_math_basketball_soccer = [4, 3, 2]
# Calculate the total ratio units
total_ratio_units = sum(ratio_math_basketball_soccer)
# Calculate the number of students per ratio unit
students_per_unit = basketball_team / ratio_math_basketball_soccer[1]
# Calculate the number of students on each team
math_team = students_per_unit * ratio_math_basketball_soccer[0]
soccer_team = students_per_unit * ratio_math_basketball_soccer[2]
(math_team, basketball_team, soccer_team)
# ------------------------------task3-------------------------------
# Given values from previous tasks
math_team = 40.0
basketball_team = 30
soccer_team = 20.0
students_all_three = 8
# Total number of students
total_students = 60
# Calculate the number of students playing in exactly two teams using the inclusion-exclusion principle
students_two_teams = (math_team + basketball_team + soccer_team) - total_students - (2 * students_all_three)
students_two_teams
```

The code generated by `DataInterpreter`

follows the planned task exactly and runs successfully, eventually calculating the correct answer to be: **14**