Skip to content

Commit

Permalink
Added number.is_prime() method
Browse files Browse the repository at this point in the history
  • Loading branch information
bharadwajyarlagadda committed Oct 29, 2016
1 parent 51c31ad commit d4c5471
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Changelog
- Add ``is_multiple_of()``.
- Add ``is_number()``.
- Add ``is_odd()``.
- Add ``is_prime()``.
- Add ``is_string()``.
- Add ``last()``.
- Add ``random_()``.
Expand Down
9 changes: 9 additions & 0 deletions docs/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ Numbers
>>> _.is_odd(7)
True
>>> _.is_prime(5)
True
>>> _.is_prime(7)
True
>>> _.is_prime(4)
False
>>> _.is_prime(727021)
True
>>> result = _.random_(5, 6)
>>> assert 5 <= result <= 6
>>> result = _.random_(5)
Expand Down
1 change: 1 addition & 0 deletions sugar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
is_even,
is_multiple_of,
is_odd,
is_prime,
random_
)

Expand Down
2 changes: 2 additions & 0 deletions sugar/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
if PY3:
string_types = (str,)
number_types = (int, float, Decimal)
_range = range
else:
string_types = (str, unicode)
number_types = (int, long, float, Decimal)
_range = xrange
4 changes: 3 additions & 1 deletion sugar/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import copy

from ._compat import _range

import sugar as _


Expand Down Expand Up @@ -153,7 +155,7 @@ def construct(var, callback):
.. versionadded:: 0.1.0
"""
return [callback(i) for i in range(0, var)]
return [callback(i) for i in _range(0, var)]


def count(array, value):
Expand Down
39 changes: 38 additions & 1 deletion sugar/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

from __future__ import absolute_import

import sys
import random
import math

from ._compat import _range


def is_even(num):
"""Returns True if :attr:`num` is even.
Expand Down Expand Up @@ -75,6 +76,42 @@ def is_odd(num):
return num % 2 != 0


def is_prime(num):
"""Returns True if the give :attr:`num` is a prime number.
Args:
num (int/float): Number passed in by the user.
Returns:
bool: True if the given :attr:`num` is a prime number else False
Example:
>>> is_prime(5)
True
>>> is_prime(7)
True
>>> is_prime(4)
False
>>> is_prime(727021)
True
.. versionadded:: TODO
"""
count = 0

for i in _range(1, (math.ceil(math.sqrt(num)) + 1)):
if num % i == 0:
count += 1

if count <= 1:
result = True
else:
result = False

return result


def random_(n1=None, n2=None):
"""Returns a random integer/float from :attr:`n1` to :attr:`n2` (both
inclusive)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ def test_is_odd(num, expected):
assert _.is_odd(num) == expected


@parametrize('num,expected', [
(13, True),
(727021, True),
(10, False),
(21, False),
(37, True)
])
def test_is_prime(num, expected):
assert _.is_prime(num) == expected


@parametrize('args', [
(0, 5),
(2, 5),
Expand Down

0 comments on commit d4c5471

Please sign in to comment.