# Math - Inequalities - Challenge

In this lecture we learnt how to manipulate inequalities.

For your challenge, our designer wants to know how many times the player can refuel and still afford an upgrade at the end of the level.

We know that:

• Pickups are worth 5 coins each
• There are 30 pickups in a level
• Refueling costs 10 coins

For a hint, we can express our problem as:
(30 x 5) - (10n) >= 100

So we need to rearrange and solve for n.

Summary

float pickup = 30 * 5;
float refuel = 10;
float n;

n <= (upgrade - pickup) / -refuel;

n <= 5

1 Like

(30 * 5) - 10n >= 100

150 - 10n - 100 >= 0
150 - 100 >= 10n
50/10 >= n
n <= 5

1 Like

With the equation (30*5) - (10n) >= 100

30*5 = 150 so 150-10n >= 100

-150 + 150 - 10n = 100-150 so -10n >= -50

-10n/-10 >= -50/-10 so n <= -50/-10

or

n <= 5

1 Like

So I got no more than 5 refuels and it was pretty easy solving it, however I think the hardest part of this, you actually did for us, namely expressing the whole thing as an inequality. As soon as you explained the problem, I sort of calculated it intuitively but I wasnât thinking iun terms of inequalities.

I think it would be good to learn how to express these problems formally in math language for when more complex problems that are not so cognitively intuitive come along.

My take away from this lesson is that we expressed the >= sign as âhas got to be less or equal toâ, in other words we formulate maths statements to require something rather than to discover (in this case at least)

3 Likes

@olidadda, one of the hardest parts of maths is converting your word problem into math terms.
Hopefully me talking through the problem helps you get a better sense of the thought process that goes into getting from one to the other.

Rather that just drop you in at the deep end I do like to give you a helping hand where I can in these early stages of the course, but that help does start to turn into smaller and smaller hints as we move through the course.

And itâs great to hear you got an intuitive feel for the answer before actually solving it.
Thatâs a good skill to develop and will help you a great deal in the long run.

In your last paragraph though, >= is âgreater or equal toâ (I think you meant <=).
Youâre general thought process seems sound though. Inequalities deal with ranges of possible answers, so they come up a lot in programming - especially for things like if-statements.

2 Likes

Writing it out in full to get into the practice of making sure I have a verbal understanding of what Iâm doing:

The player needs to be able to afford an upgrade at the end of the level. Upgrades cost 100 coins. Refueling in the level costs 10 coins. The player can pick up 30 coins in the level, for a total of 150 coins. How many times can the player refuel before they can no longer afford an upgrade?

Let n = number of refuels.

(30 [coins] x 5 [coin value]) - 10n[refuels] >= 100 [upgrade cost]

150 - 10n >= 100

150 >= 100 + 10n

50 >= 10n

5 >= n

n <= 5 : The player can refuel a maximum of 5 times before they can no longer afford an upgrade.

2 Likes

I like the problem example of have fuel for a space ship.

c = m - (b* t)

and having `c` a range from `0 .. m(100)`

I find the evaluation code a bit odd.

if ( 0 â€ c & c â€ m(100) ) âŠ has fuel ?

Because `c` can also be 0, no fuel. I would expect `0 < c` âŠ
As a developer I would use

if ( 0 < c ) âŠ has fuel ?

I think I got it rightâŠ I hope

1 Like

@FedPete,
It really depends on the problem youâre trying to solve.

If you just want to keep the current fuel within the range of what the tank can hold then `if(0 â€ currentFuel && currentFuel â€ maxFuel)` would make sense.
You donât want to continue burning fuel once thereâs none left and you donât want pickups to increase the value beyond the maximum value.

If you were checking whether the ship actually has enough fuel to fly then `if(currentFuel > 0)` would be the right way to go. If thereâs no fuel then you canât fly and the maximum fuel limit doesnât really factor into the equation, since that should be managed by other code.

So both options have their uses, and if youâre writing a fuel management script then youâd probably use both in different places.

2 Likes

Looks good to me @KhaledAhmedYounes. Nice work!

1 Like

(30â5)â(10n)â„100
150 â10nâ„100
â10nâ„â50
nâ€5

1 Like

n <= 5

1 Like

(30 * 5) - (10n) >= 100
(150) - (10n) - 100 >= 0
50 >= 10n
50/10 >= n
n <= 5

1 Like

So I got
n<=5.
30 *5 = 150 coins in total
150 - 10n >= 100 / -150
-10n >= 100 - 150 / : -10
n <= -50 \ 10
n <= 5

I got

n <= 5
Because
(30 * 5) - (10n) >= 100
150 - 10n >= 100
-10n >= 100-150
-10n >= -50
n <= -50/-10
n <= 5

1 Like

I got n <= 5

My workings:

( 30 * 5 ) - ( 10 * n ) >= 100
150 - 10n >= 100
-10n >= -50 (Taken 150 from both sides)
n <= -50/-10 (Divide both sides by -10 and flip sign)
Therefore: n <= 5

Hopefully that is all right!

1 Like

So lets try this:

(30 x 5) - (10n) >= 100
150 - 10n >=100
-10n >= 100 - 150
-10n >= -50
n <= 5

Answer: Player can refuel 5 or fewer times.

1 Like

Oh this is fun! Here we go!
Initial formula: (30 x 5) - (10n) >= 100

150 - (10 * x) â„ 100 || - 150
-(10 * x) â„ -50 || * -1
10 * x â€ 50 || Ă· 10
x â€ 5

I hope this is correct!

1 Like

Starting Equation: 150-10n >= 100

-10n >= 100-150
-10n >=-50
n <= -10/-50
n <= 5

1 Like