```
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

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

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.

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

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

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.