Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Find Centered Hexagonal Primes
# Python program for practical application of sqrt() function
# https://en.wikipedia.org/wiki/Centered_hexagonal_number
# Finding Centered hexagonal primes
import sys
# import math module
import math
# function to check if prime or not
def check(n):
if n == 1:
return False
# from 1 to sqrt(n)
for x in range(2, (int)(math.sqrt(n))+1):
if n % x == 0:
return False
return True
# number of tiles in a hex
'''
x x
x x x
x x
x x x
x x x x
x x x x x
x x x x
x x x
x x x x
x x x x x
x x x x x x
x x x x x x x
x x x x x x
x x x x x
x x x x
'''
def calculate_hex_size(hex_side_size):
hex_tiles_total = 0
max_row_count= 2 * hex_side_size - 1
row_count = hex_side_size
while row_count < max_row_count:
hex_tiles_total += (row_count * 2)
row_count += 1
hex_tiles_total += max_row_count
return hex_tiles_total
hex_size = input("Enter a hexagon side length: ")
print(calculate_hex_size(hex_size))
# check if total is prime
n = calculate_hex_size(hex_size)
if check(n):
print('Side: ' + str(hex_size), 'Total: ' + str(n), "prime (centered hexagonal)")
else:
print('Side: ' + str(hex_size), 'Total: ' + str(n), "not prime")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment