Programming Assignment: Homework 2 - Number Properties

###########################################################
### EXECUTE THIS CELL BEFORE YOU TO TEST YOUR SOLUTIONS ###
###########################################################

import unittest
from nose.tools import assert_equal, assert_true

Get Factors

def getFactors(x):
    """Returns a list of factors of the given number x.
    Basically, finds the numbers between 1 and the given integer that divide the number evenly.

    For example:
    - If we call getFactors(2), we'll get [1, 2] in return
    - If we call getFactors(12), we'll get [1, 2, 3, 4, 6, 12] in return
    """

    value = 1
    l=[]
    while value <= x :
        if x % value == 0 :
            l.append(value)
        value += 1
    return l

Is prime

def isPrime(x):
    """Returns whether or not the given number x is prime.

    A prime number is a natural number greater than 1 that cannot be formed
    by multiplying two smaller natural numbers.

    For example:
    - Calling isPrime(11) will return True
    - Calling isPrime(71) will return True
    - Calling isPrime(12) will return False
    - Calling isPrime(76) will return False
    """

    if x == 1:
        return False
    elif x > 1:
        for i in range(2,x):
            if (x % i) == 0:
                return False
                break
        else:
            return True
    else:
        return False

Is Composite

def isComposite(x):
    """Returns whether or not the given number x is composite.

    A composite number has more than 2 factors.
    A natural number greater than 1 that is not prime is called a composite number.
    Note, the number 1 is neither prime nor composite.

    For example:
    - Calling isComposite(9) will return True
    - Calling isComposite(22) will return True
    - Calling isComposite(3) will return False
    - Calling isComposite(41) will return False
    """

    n = 0
    for i in range(1, x+1):
        if x % i == 0:
            n += 1

    if n > 2:
        return True
    else:
        return False

Is perfect

def isPerfect(x):
    """Returns whether or not the given number x is perfect.

    A number is said to be perfect if it is equal to the sum of all its
    factors (for obvious reasons the list of factors being considered does
    not include the number itself).

    Example: 6 = 3 + 2 + 1, hence 6 is perfect.
    Example: 28 is another example since 1 + 2 + 4 + 7 + 14 is 28.
    Note, the number 1 is not a perfect number.
    """

    sum = 0

    for i in range(1, x):
        if x % i == 0:
            sum = sum + i

    if sum == x:
        return True
    else:
        return False

Is Abundant

def isAbundant(x):
    """Returns whether or not the given number x is abundant.

    A number is considered to be abundant if the sum of its factors
    (aside from the number) is greater than the number itself.

    Example: 12 is abundant since 1+2+3+4+6 = 16 > 12.
    However, a number like 15, where the sum of the factors.
    is 1 + 3 + 5 = 9 is not abundant.
    """

    s = 0
    for i in range(1, x):
        if x % i == 0:
            s += i
    if s > x:
        return True
    else:
        return False

Is Triangular

def isTriangular(x):
    """Returns whether or not a given number x is triangular.

    The triangular number Tn is a number that can be represented in the form of a triangular 
    grid of points where the first row contains a single element and each subsequent row contains 
    one more element than the previous one.

    We can just use the fact that the nth triangular number can be found by using a formula: Tn = n(n + 1) / 2.

    Example: 3 is triangular since 3 = 2(3) / 2
    3 --> 2nd position: (2 * 3 / 2)

    Example: 15 is triangular since 15 = 5(6) / 2
    15 --> 5th position: (5 * 6 / 2)
    """

    if (x < 0):
        return False
    sum, n = 0, 1
    while(sum <= x):

        sum = sum + n
        if (sum == x):
            return True
        n += 1

    return False

Is Narcissistic

def isNarcissistic(x):
    """Returns whether or not a given number is Narcissistic.

    A positive integer is called a narcissistic number if it
    is equal to the sum of its own digits each raised to the
    power of the number of digits.

    Example: 153 is narcissistic because 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153.
    Note that by this definition all single digit numbers are narcissistic.
    """

    sum = 0
    length = len(str(x))
    for i in str(x):
        sum = sum + int(i) ** length
    if (x == sum):
        return True
    else:
        return False

Did you find this article valuable?

Support Abhinavtej Reddy by becoming a sponsor. Any amount is appreciated!