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


#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


#2

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++){}

#3

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


#4

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.


#5

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)


#6

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).


#7

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


#8

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


#9

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.