Resolviendo el reto /Solving Challenge

in Hive Learners5 months ago (edited)

Esta es una serie de Post que se enfocarán en el paso a paso para resolver los problemas diarios que plantea el usuario @ydavgonzalez y a la vez enseñar el enfoque de programación .

Enlace al reto
https://ecency.com/hive-163521/@ydavgonzalez/problema-matematico-del-dia29-08-2024

¡Hola, amantes del conocimiento! Hoy vamos a adentrarnos en un emocionante desafío que combina el poder de las matemáticas y la lógica de la programación. Imaginen una librería con tres tipos de libros: matemáticas, historia y ciencia. Cada libro tiene un precio diferente, y Juan, nuestro protagonista, compra 12 libros por un total de $250.

¿El reto? Descubrir cuántos libros de cada tipo compró Juan, sabiendo que compró más libros de matemáticas que de historia y que la cantidad de libros de ciencias es el triple de la cantidad de libros de historia.

Resolviendo el Enigma: Una Combinación de Matemáticas y Lógica

Para abordar este problema, primero debemos definir nuestras variables:

  • M: Cantidad de libros de matemáticas
  • H: Cantidad de libros de historia
  • C: Cantidad de libros de ciencia

Ahora, traduzcamos las pistas del problema a ecuaciones:

  1. M + H + C = 12: El total de libros comprados es 12.
  2. 15M + 20H + 25C = 250: El costo total de los libros es $250.
  3. M > H: Juan compró más libros de matemáticas que de historia.
  4. C = 3H: La cantidad de libros de ciencia es el triple de la cantidad de libros de historia.

El Camino Hacia la Solución: Sustitución y Ecuaciones

Con estas ecuaciones, podemos usar la técnica de sustitución para resolver el problema.

  • Sustituimos C por 3H en la ecuación 1: M + H + 3H = 12
  • Simplificamos: M + 4H = 12
  • Sustituimos C por 3H en la ecuación 2: 15M + 20H + 25(3H) = 250
  • Simplificamos: 15M + 95H = 250

Ahora tenemos un sistema de dos ecuaciones con dos variables:

  1. M + 4H = 12
  2. 15M + 95H = 250

Podemos resolver este sistema utilizando diferentes métodos como la eliminación o la sustitución.

Ejemplo de Código en JavaScript para Resolver el Sistema de Ecuaciones// Función para resolver un sistema de dos ecuaciones lineales
function resolverSistemaEcuaciones(a1, b1, c1, a2, b2, c2) {
// Calcular el determinante del sistema
let det = (a1 * b2) - (a2 * b1);

// Si el determinante es 0, el sistema no tiene solución única
if (det === 0) {
return "El sistema no tiene solución única";
} else {
// Calcular las soluciones para las variables x e y
let x = ((c1 * b2) - (c2 * b1)) / det;
let y = ((a1 * c2) - (a2 * c1)) / det;
return { x: x, y: y };
}
}

// Definir los coeficientes del sistema de ecuaciones
let a1 = 1, b1 = 4, c1 = 12;
let a2 = 15, b2 = 95, c2 = 250;

// Resolver el sistema de ecuaciones
let solucion = resolverSistemaEcuaciones(a1, b1, c1, a2, b2, c2);

// Mostrar la solución
console.log("Cantidad de libros de matemáticas: " + solucion.x);
console.log("Cantidad de libros de historia: " + solucion.y);

// Calcular la cantidad de libros de ciencia
let cantidadCiencia = 3 * solucion.y;
console.log("Cantidad de libros de ciencia: " + cantidadCiencia);
Este código JavaScript define una función para resolver un sistema de dos ecuaciones lineales y luego lo utiliza para resolver el sistema de ecuaciones que obtuvimos a partir del problema de los libros.

¡Y así, hemos desentrañado el enigma! Usando la combinación de matemáticas y programación, descubrimos que Juan compró 8 libros de matemáticas, 1 libro de historia y 3 libros de ciencia.

¡Espero que esta aventura te haya inspirado a explorar el poder de las matemáticas y la programación!

#englishversion

Deciphering the Book Enigma: A Mathematical and Programming Adventure

Hello, knowledge lovers! Today we're going to embark on an exciting challenge that combines the power of mathematics and the logic of programming. Imagine a bookstore with three types of books: math, history, and science. Each book has a different price, and Juan, our protagonist, buys 12 books for a total of $250.

The challenge? To find out how many books of each type Juan bought, knowing that he bought more math books than history books and that the number of science books is triple the number of history books.

Solving the Enigma: A Combination of Math and Logic

To tackle this problem, we must first define our variables:

  • M: Number of math books
  • H: Number of history books
  • C: Number of science books

Now, let's translate the clues of the problem into equations:

  1. M + H + C = 12: The total number of books purchased is 12.
  2. 15M + 20H + 25C = 250: The total cost of the books is $250.
  3. M > H: Juan bought more math books than history books.
  4. C = 3H: The number of science books is triple the number of history books.

The Path to the Solution: Substitution and Equations

With these equations, we can use the technique of substitution to solve the problem.

  • Substitute C for 3H in equation 1: M + H + 3H = 12
  • Simplify: M + 4H = 12
  • Substitute C for 3H in equation 2: 15M + 20H + 25(3H) = 250
  • Simplify: 15M + 95H = 250

Now we have a system of two equations with two variables:

  1. M + 4H = 12
  2. 15M + 95H = 250

We can solve this system using different methods like elimination or substitution.

Example of JavaScript Code to Solve the System
of Equations// Function to solve a system of two linear equations
''''
function solveSystemOfEquations(a1, b1, c1, a2, b2, c2) {
// Calculate the determinant of the system
let det = (a1 * b2) - (a2 * b1);

// If the determinant is 0, the system has no unique solution
if (det === 0) {
return "The system has no unique solution";
} else {
// Calculate the solutions for variables x and y
let x = ((c1 * b2) - (c2 * b1)) / det;
let y = ((a1 * c2) - (a2 * c1)) / det;
return { x: x, y: y };
}
}

// Define the coefficients of the system of equations
let a1 = 1, b1 = 4, c1 = 12;
let a2 = 15, b2 = 95, c2 = 250;

// Solve the system of equations
let solution = solveSystemOfEquations(a1, b1, c1, a2, b2, c2);

// Display the solution
console.log("Number of math books: " + solution.x);
console.log("Number of history books: " + solution.y);

// Calculate the number of science books
let scienceCount = 3 * solution.y;
console.log("Number of science books: " + scienceCount);
'''
This JavaScript code defines a function to solve a system of two linear equations and then uses it to solve the system of equations we obtained from the book problem.

And so, we have unraveled the enigma! Using the combination of math and programming, we discovered that Juan bought 8 math books, 1 history book, and 3 science books.

I hope this adventure has inspired you to explore the power of mathematics and programming!