For definition of BMI, healthy would be 18.5 to 25.
First, let’s create a function to calculate the BMI:
float imc(float peso, float altura) {
return peso / (altura * altura);
}
We could also create a function that says whether the BMI is healthy or not (but we won’t even need it here):
int imc_saudavel(float imc) {
return imc >= 18.5 && imc <= 25;
}
If so-and-so has a low BMI, how much weight he needs to gain to get at 18.5?
Let’s solve the equation:
imc(peso_atual + peso_a_ganhar, altura) = 18,5
(peso_atual + peso_a_ganhar) / (altura * altura) = 18,5
peso_atual + peso_a_ganhar = 18,5 * altura * altura
peso_a_ganhar = 18,5 * altura * altura - peso_atual
And if it’s above 25, how much he has to lose?
imc(peso_atual - peso_a_perder, altura) = 25
(peso_atual - peso_a_perder) / (altura * altura) = 25
peso_atual - peso_a_perder = 25 * altura * altura
-peso_a_perder = 25 * altura * altura - peso_atual
whereas peso_a_perder = -peso_a_ganhar
, then:
peso_a_ganhar = 25 * altura * altura - peso_atual
In both cases, we have this:
peso_a_ganhar = imc_ideal * altura * altura - peso_atual
So we can do a function that says how much weight a person should gain or lose:
float peso_a_variar(float peso, float altura) {
float valorImc = imc(peso, altura);
if (valorImc >= 18.5 && valorImc <= 25) return 0; // A pessoa já tem o IMC saudável.
float fator = valorImc < 18.5 ? 18.5 : 25;
return fator * altura * altura - peso;
}
Note the inequalities in these if
s and the use of ternary operator. The rest is based on equations.
I can eliminate that if
?
If the person already has an ideal weight, we could eliminate the if
by using as fator
, own valorImc
. Since in that case:
valorImc = peso / (altura * altura)
Then the return
would produce this:
(peso / (altura * altura)) * altura * altura - peso
Note that peso
is divided by altura * altura
and then multiplied by that same term. These two operations cancel out, so that this results in:
peso - peso
Which is obviously always zero. So, if the person already has the ideal weight, we can use the valorImc
in place of fator
. This makes the code a little simpler:
float peso_a_variar(float peso, float altura) {
float valorImc = imc(peso, altura);
float fator = valorImc < 18.5 ? 18.5 : valorImc > 25 ? 25 : valorImc;
return fator * altura * altura - peso;
}
I can eliminate these ternary operators?
The function of these ternary operators is to ensure that the factor is within the healthy range of 18.5 through 25. Therefore, you can replace them by using the fminf
and the fmaxf
(don’t forget the #include <math.h>
):
float peso_a_variar(float peso, float altura) {
float valorImc = imc(peso, altura);
float fator = fmaxf(18.5, fminf(valorImc, 25.0));
return fator * altura * altura - peso;
}
If you don’t want to use functions that are not already ready in the standard library as well:
float peso_fulano = ...;
float altura_fulano = ...;
float imc_fulano = peso_fulano / (altura_fulano * altura_fulano);
float fator_fulano = fmaxf(18.5, fminf(imc_fulano, 25.0));
float peso_a_ganhar_fulano = fator_fulano * altura_fulano * altura_fulano - peso_fulano;
And you have as a result a code that uses only very basic knowledge in C.
Time normally will not be so absurdly distinct. Mathematical calculations of primitive types are performed in constant time. When trying to do something more efficient, one usually tries to decrease the execution complexity of the algorithm; for example, to sort
o(n log n)
is much more efficient thano(n^2)
, so it doesn’t make much sense to question how to brush bits to make quadratic ordering more efficient– Jefferson Quesado
They recently asked a question about performance between an algorithm
o(n^2)
and anothero(n log n)
. I shared my results and the methodologies used. Look at the difference between the performance ofselection sort
to themerge (3)
. This is the power to decrease the complexity of the algorithm that solves the problem– Jefferson Quesado