What is the meaning? "for i in 2..n-1"

 def prime(n)
 puts "That's not an integer." unless n.is_a? Integer
  is_prime = true
  for i in 2..n-1
    if n % i == 0
      is_prime = false
    end
  end

I DON’T UNDERSTAND WHAT “FOR I IN 2…N-1” WHICH IS LINE 4 OF ABOVE CODE

for i in 2..n-1 describes iteration over a range from 2 to 1 less than n.

Compare this to Python:

for i in range(2, n-1):

or JavaScript:

for (var i = 2; i < n; i++){}
1 Like

Could you explain why the n-1 is there in more detail? And then the following line (if n % i == 0) and why that also is needed

In Ruby a range described with two dots includes to the top value…

n = 10
puts (2..n).to_a     # [2, 3, 4, 5, 6, 7, 8, 9, 10]

puts(2..n-1).to_a    # [2, 3, 4, 5, 6, 7, 8, 9]

however, Ruby can generate a short range the same way Python does…

puts(2...n).to_a     # [2, 3, 4, 5, 6, 7, 8, 9]

In Python 2,

print range(2, n)    # [2, 3, 4, 5, 6, 7, 8, 9]

That’s a test of divisibility using modulo division which outputs the remainder. When it is zero, the number is divisible, so not Prime.

2 Likes

Okay I got that thank you! But maybe I’m not understanding what the “for i in 2…n-1” statement is doing for the code, if you could explain that? (particularly what the for is doing to i)

for has a built in iterator which iterates the range, each in turn starting from lowest and going to the highest.

for i in 2...n

end

i is the block variable used to hold each value being iterated. It will be 2, then 3, then … n.

What the code above represents is a brute force test for Primeness of the number represented by n. I say brute force because it covers the entire range up to but not including n. (3 / 3 == 1) In truth, a number cannot be divided by any number greater than half its value. (5 / 3 == 1 with 2 remainder). The fact that there is a remainder means 5 is Prime.

This would be just as valid a test…

for i in 2..((n+1)/2).to_i

end

and if we look at this even further, we can conclude that since all numbers can be factored to the primes that they are derived from, then technically the square-root of n plus 1 for good measure is the highest our range needs to extend.

sqrt(5).to_i + 1 == 3

Read up on for and its many uses for iterating over iterable objects such as strings, arrays, hashes, etc. Learn when it is appropriate to drop the last value of a range (such as when iterating strings and arrays, which are zero-indexed) and when to keep it (such as printing a list of numbers from m to n, inclusive).

2 Likes

Awesome didn’t know for had a built in iterator so that helps a lot, thank you so much! I really appreciate how in depth you go

1 Like

This may be a long shot but do you possibly tutor code? I’m fine with individual topics but combining all that knowledge and putting together larger labs is a big struggle for me so if you have any recommendations or resources that you think would help I’d love to know

Not real world, code, no. I’m not qualified beyond the basics since I don’t work in any industries and have no schooling in the discipline. I can guide a learner only so far, then they surpass me, which I don’t mind. These past few years I’ve devoted myself to working with beginners to help them with a leg up. Once their juices start flowing they no longer need me. That’s why I chose CC as my place to be.

1 Like

Hey Roy,

Is there a way to debug this method in the same way you can debug JS in the web dev tools by sticking debugger;
I want to see the values of i and n as the iteration takes place

simplest way to see the values is to either assign them to an array, or log them as you go,

console(i, n)

right inside the loop.

yeah fine. I’ll log them thanks. I might use Pry later on

The best debugging tool is between your shoulders. Work with the console, study the errors, revise code so the errors go away and your get the result you want. If you only become an expert at running the machine that debugs the code, how much code will you learn? How well will be familiar with the error messages and what they mean, and usually point out directly?

Not to wax philosophical, a beginner should work with the least amount of tools and the simplest, at that. Text editor, browser, console, and the documentation close at hand.

Try everything, and see what happens. Make your code, then break it, and find ways to have it keep itself from breaking. The canvas is all ours. Splash a little paint and interpret the outcome.