Skip to content

Instantly share code, notes, and snippets.

@JamesJinPark
Last active August 29, 2015 14:06
Show Gist options
  • Save JamesJinPark/3a99a383884b355aa773 to your computer and use it in GitHub Desktop.
Save JamesJinPark/3a99a383884b355aa773 to your computer and use it in GitHub Desktop.
Figurate_Number
#Monica Ionescu and James Park
#Figurate Numbers
#This program tests for characteristics of positive integers (tests for figurate numbers)
import math
#This function compiles all other functions in this program, establishes a range of numbers, \
#and tests the numbers for each specific characteristic.
def main():
limit = 100
for n in range(1, limit + 1):
print n, \
"Prime," if is_prime(n) else "Composite,", \
"Triangular," if is_triangular(n) else "Not Triangular,", \
"Tetrahedral," if is_tetrahedral(n) else "Not Tetrahedral,", \
"Square," if is_square(n) else "Not Square,", \
"Square Pyramidal," if is_square_pyramidal(n) else "Not Square Pyramidal,", \
"Pentagonal," if is_pentagonal(n) else "Not Pentagonal,", \
"Prime Oblong," if is_prime_oblong(n) else "Not Prime Oblong,", \
"Pointy" if is_pointy(n) else "Not Pointy"
#This function tests whether a number is prime.
def is_prime(n):
if n == 1:
return False
for x in range(2, n):
if n % x == 0:
return False
return True
#This function tests whether a number is triangular.
def is_triangular(n):
test_number = 8 * n + 1
root = math.sqrt(test_number)
return int(root)**2 == test_number
#This function tests whether a number is a tetrahedral number.
def is_tetrahedral(n):
test_number = 6.0 * n
cube_root = test_number**(1.0 / 3.0)
cube_root_floor = int(cube_root)
return cube_root_floor * (cube_root_floor + 1) * (cube_root_floor + 2) == test_number
#This function tests whether a number is a square number.
def is_square(n):
root = math.sqrt(n)
return int(root)**2 == n
#This function test whether a number square pyramidal.
def is_square_pyramidal(n):
test_number = 3.0 * n
cube_root = int(test_number**(1.0 / 3.0))
return cube_root * (cube_root + 1) * (2 * cube_root + 1) == test_number * 2
#This function tests whether a number is pentagonal.
def is_pentagonal(n):
test_number = (2.0 / 3.0) * n
square_root = int(test_number ** (1.0 / 2.0))
return (square_root + 1) * (3 * (square_root + 1) - 1) == test_number * 3
#This function uses the is_prime function to test whether a number is prime oblong.
def is_prime_oblong(n):
for x in range (2, n):
quotient = n / x
if n % x == 0 and x != quotient and is_prime(x) and is_prime(quotient):
return True
return False
#The next two functions test whether a number is pointy.
# generate_tetrahedral generates tetrahedral numbers, which are then used in is_pointy to check if a number is pointy.
def generate_tetrahedral(n):
return (1.0 / 6.0) * ( n * (n + 1) * (n + 2))
def is_pointy(n):
x = 1
tetrahedral = generate_tetrahedral(x)
while tetrahedral < n:
difference = n - tetrahedral
if is_tetrahedral(difference) and difference != tetrahedral:
return True
x = x + 1
tetrahedral = generate_tetrahedral(x)
return False
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment