Is_prime


#1


https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/6?curriculum_id=4f89dab3d788890003000096#

The code works perfect. :slight_smile:

I wonder, even if i write (x - 1) or just x when representing the range function both of them works perfect in either case.
To my knowledge,

lets suppose x = 10
range(2, x) will give (2,3,4,5,6,7,8,9)

and range (2, x - 1) will give (2,3,4,5,6,7,8)

then why does everyone mentions x - 1 when i try to go through all the solutions to cross check. Thanks for the help in advance. :slight_smile:


def is_prime(x):
    if x >= 2:
        for n in range(2, x - 1):
            if x % n == 0:
                return False
        else:
            return True
    else:
        return False


#2

The full range would make more sense on first look. However, when we think about it, we could stop anywhere after x / 2 plus 1 for good measure.

The largest number that can be divided into a number is no more than half of that number.


#3

Exactly! Thanks for the preciseness. :slight_smile:


#4

2 posts were split to a new topic: Don't understand why lines 6-7 aren't indented


#5