Created
February 5, 2019 02:18
-
-
Save nbuechler/0665a2b77df8c4039225c9048ba55455 to your computer and use it in GitHub Desktop.
Find Centered Hexagonal Primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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