Next if?

Im really bad at the modulos can someone help me?

What level are you at in Mathematics? Have you had any practice with long division or have you depended upon a calculator?

Depended on the calculator and multiple choice… Can you help me with the question now?

Can you show us what you have, or what part of the instructions and/or lesson text you are unclear on?

I have attached a link which leads the problem. Here it is again.

Its asking to target the odd numbers using modulus.

An odd number is any number that cannot be evenly divided by 2. There will always be a remainder.

Put another way, an odd number is any number that when we repeatedly subtract 2 will never reach zero, but skip over it. 3, 1, -1, -3, …

Either way, we get 1 if we don’t go into the negative.

A modulo is the remainder when we divide. On your calculator it is the decimal fraction when the modulus is 1, else the decimal fraction times the modulus.

Put another way, consider,

23.to_f / 11 = 2.09090909090909
11 * 0.09090909090909 = 0.99999999999999

That’s close enough to 1 to be 1, but illustrates the tiny errors in floating point arithmetic. We also know that repeating sequences in a decimal can be written as a fraction (a rational number) so with a little bit of pencil and paper work this can be simplified and solved.

Let x = 0.090909..., so that
100 * x = 9 + x
100 * x - x = 9
99 * x = 9
x = 9 / 99 = 1 / 11

That means that

 23   2 * 11    1
 -- = ------ + ---
 11     11      11

Multiplying everything by 11 gives,

23 = 22 + 1

Now consider,

Dividend = divisor * quotient + Remainder

D = dq + R  =>  D - dq = R

In modulo division, R is the modulo, d is the modulus.

23 / 11 = 2 with remainder 1
23 - 2 * 11 = 1
23 - 22     = 1

23 % 11     = 1

So can you help me with the challenge?

Did you figure out how to check if a number is odd or even?

Most of the code is there already. All you need to do is add one line above the print statement. How should that line read? Go over the instructions and study the example. Your line of code will be similar, but not the same.

i = 20
loop do
  i -= 1
  print "#{i}"
  break if i < 1
1 Like

Very in depth and straight forward, bookmarked this for later reference, thank you!

1 Like

thank you very much i got it. =)

1 Like

hello! i realy donpt know how to do it, i know what is a odd number but, how can it put in my code a odd number,
break if i and what?

Did you read the lesson text? I gives a pretty straight forward example that the instuctions ask us to follow. There is only one slight change we need to make to our line of code.

Bear in mind that when i % 2 == 0, i is even, when it is != 0, i is odd.

thanks a lot mtf!
you open my mind, i was stucked…

1 Like

2 posts were split to a new topic: Next if problem

A post was split to a new topic: Trouble with next if problem