Having looked at emirpimes in base 10 in my previous post, I thought it would be interesting to look at emirpimes in other bases. Of course an emirpimes in base 10 will not necessarily be an emirpimes in another base, even though it will remain a semiprime. I decided to get AI to write some code to investigate this matter. The tried Claude again but I didn't have enough credits and Grok refused to even consider it. So it was with some trepidation that I turned to Gemini to write and code but this time it came through. Here was my prompt:
Emirpimes are semiprimes that remain semiprimes when the order of their digits is reversed. Some very special emirpimes have prime factors that are emirps (primes that remain prime when their digits are reversed). 1469 is an example since 13 * 113 = 1469 and 31 * 311 = 9641. Write a program that will identify such emirpimes not only in both 10 but in other bases below 10 (9, 8 , 7, ..., 3, 2). The input should allows users to decide on the number base and to choose the range between which the emirpimes will be searched for. The default range will be between 1 and 40000 and the default base will be base 10. The output will be list of the emirpimes-semiprime that satisfy together will their factors in the selected number base. The base 10 equivalents of each emirpimes-semiprime should also be shown. The program will need to run in SageMathCell or a Jupyter notebook.
The range of numbers to be investigated was adjustable and so, having just turned 28000 day old, I decided to restrict the range to between 28000 and 40000. Here's what the program came up with for bases 9 to 2. It's all very well set out so that no explanation is required really.
BASE 9
![]() |
Table 1: permalink |
![]() |
Table 2: permalink |
![]() |
Table 3: permalink |
![]() |
Table 4: permalink |
![]() |
Table 6: permalink |
![]() |
Table 7: permalink |
Here is a Python program designed to run in a Jupyter Notebook or SageMathCell. It identifies the "special" emirpimes where the reversal property applies not just to the number itself, but distributes perfectly over its prime factors:$$Rev(p \times q) = Rev(p) \times Rev(q)$$
The Python Program
# ==========================================
# CONFIGURATION (User Inputs)
# ==========================================
# Change these values to search in different bases or ranges
SEARCH_BASE = 10 # Bases below 10 (e.g., 2, 3, ... 9, 10)
SEARCH_RANGE = (1, 40000) # Range to search [start, end]
# ==========================================
# CORE FUNCTIONS
# ==========================================
def to_base(n, b):
"""Converts an integer n to a list of digits in base b."""
if n == 0:
return [0]
digits = []
while n:
digits.append(int(n % b))
n //= b
return digits[::-1]
def from_base(digits, b):
"""Converts a list of digits in base b back to an integer."""
n = 0
for d in digits:
n = n * b + d
return n
def get_reversed_val(n, b):
"""Returns the integer value of n with its digits reversed in base b."""
digits = to_base(n, b)
return from_base(digits[::-1], b)
def is_prime(n):
"""Simple primality test."""
if n <= 1: return False
if n <= 3: return True
if n % 2 == 0 or n % 3 == 0: return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def get_semiprime_factors(n):
"""
Returns [p, q] if n is a semiprime (product of exactly 2 primes).
Returns None otherwise.
"""
factors = []
d = 2
temp = n
while d * d <= temp:
while temp % d == 0:
factors.append(d)
temp //= d
if len(factors) > 2: # Optimization: Not a semiprime if > 2 factors
return None
d += 1
if temp > 1:
factors.append(temp)
if len(factors) == 2:
return factors
return None
def format_in_base(n, b):
"""Returns string representation of n in base b."""
return "".join(str(d) for d in to_base(n, b))
# ==========================================
# MAIN EXECUTION
# ==========================================
print(f"Searching for special Emirpimes in Base {SEARCH_BASE} between {SEARCH_RANGE}...")
print(f"{'-'*105}")
print(f"{'Base 10':<10} | {'Base ' + str(SEARCH_BASE):<10} | {'Factors (Base ' + str(SEARCH_BASE) + ')':<20} |
{'Reversed (Base ' + str(SEARCH_BASE) + ')':<20} | {'Rev Factors (Base ' + str(SEARCH_BASE) + ')':<20}")print(f"{'-'*105}")
count = 0
start, end = SEARCH_RANGE
for n in range(start, end + 1):
# 1. Check if semiprime
factors = get_semiprime_factors(n)
if not factors:
continue
p, q = factors[0], factors[1]
# 2. Get reversed values in the target base
n_rev_val = get_reversed_val(n, SEARCH_BASE)
p_rev_val = get_reversed_val(p, SEARCH_BASE)
q_rev_val = get_reversed_val(q, SEARCH_BASE)
# 3. Check Condition 1: Factors must be 'Emirps' (Reversed factors must be prime)
if not (is_prime(p_rev_val) and is_prime(q_rev_val)):
continue
# 4. Check Condition 2: The "Special" Multiplication Property
# The reversed number must equal the product of the reversed factors
# i.e., Rev(n) == Rev(p) * Rev(q)
if n_rev_val == (p_rev_val * q_rev_val):
# Prepare output strings
n_str = format_in_base(n, SEARCH_BASE)
factors_str = f"{format_in_base(p, SEARCH_BASE)} x {format_in_base(q, SEARCH_BASE)}"
n_rev_str = format_in_base(n_rev_val, SEARCH_BASE)
rev_factors_str = f"{format_in_base(p_rev_val, SEARCH_BASE)} x {format_in_base(q_rev_val, SEARCH_BASE)}"
# Avoid showing palindromes if desired (optional, but 169 is usually accepted)
# Uncomment next line to strictly enforce n != reversed_n
# if n == n_rev_val: continue
print(f"{n:<10} | {n_str:<10} | {factors_str:<20} | {n_rev_str:<20} | {rev_factors_str:<20}")
count += 1
print(f"{'-'*105}")
print(f"Found {count} special emirpimes.")
How to use this
Copy and Paste: Paste the code into a cell in a Jupyter Notebook or
.SageMathCell Adjust Settings: Change
SEARCH_BASE(e.g., to 8 or 2) andSEARCH_RANGEat the top of the script.Run: Execute the cell.
Explanation of the Output
The program filters for the specific condition you noted:
This effectively finds multiplication operations that have no carries in the chosen base.
Base 10 Column: The number in standard decimal.
Base X: The number in your chosen base.
Factors: The prime factors displayed in the chosen base.
Reversed: The reversed number (also semiprime) in the chosen base.
Rev Factors: The factors of the reversed number, verifying they are indeed the reverse of the original factors.







No comments:
Post a Comment