Anagramic Squares

By replacing each of the letters in the word CARE with $1$, $2$, $9$, and $6$ respectively, we form a square number: $1296 = 36^2$. What is remarkable is that, by using the same digital substitutions, the anagram, RACE, also forms a square number: $9216 = 96^2$. We shall call CARE (and RACE) a square anagram word pair and specify further that leading zeroes are not permitted, neither may a different letter have the same digital value as another letter.
Using words.txt (right click and ‘Save Link/Target As…’), a 16K text file containing nearly two-thousand common English words, find all the square anagram word pairs (a palindromic word is NOT considered to be an anagram of itself).
What is the largest square number formed by any member of such a pair?
NOTE: All anagrams formed must be contained in the given text file.

This problem can’t be solved through traditional pen-and-paper mathematics, it requires a programming approach to test the combinations efficiently. Here’s a rough Python code:

“`python
import itertools
from collections import defaultdict
import math

# Read words from file and store them in a list
with open(‘words.txt’, ‘r’) as f:
words = [word.strip() for word in f.read().split(‘,’)]

# Create a dictionary where keys are sorted characters of each word and values are words made up of those characters
anagrams = defaultdict(list)
for word in words:
sorted_word = str(sorted(word))
anagrams[sorted_word].append(word)

# Filter out the sets with only 1 word (no anagrams)
anagram_sets = {k: v for k, v in anagrams.items() if len(v) > 1}

# Function to check if a number is square
def is_square(n):
if n == 1:
return True
low = 0
high = n // 2
root = high
while root * root != n:
root = (low + high) // 2
if low +1 >= high:
return False
if root * root > n:
high = root
else:
low = root
return True

# Function to generate all mappings for a given set of anagram words.
def generate_mappings(word):
chars = sorted(list(set(word)))
mappings = []
for perm in itertools.permutations(range(10), len(chars)):
if perm[0] == 0: continue
mapping = {chars[i]: str(perm[i]) for i in range(len(chars))}
mappings.append(mapping)
return mappings

max_square = 0
# Iterate through every set of anagram words
for word_set in anagram_sets.values():
# Generate all mappings for this set of words
mappings = generate_mappings(word_set[0])
for mapping in mappings:
# For each of the words in the set
for word in word_set:
# Map the characters in the word to digits according to the current mapping
number_as_str = ”.join([mapping[c] for c in word])
number = int(number_as_str)
# If the number is a square and greater than the current max_square, it becomes the new max square.
if is_square(number) and number > max_square:
max_square = number

print(max_square)
“`
This script looks for anagrams, generates a list of possible mappings of letters to digits, creates numbers using those mappings, checks if the numbers are squares, and finally checks if they would represent a new maximum.
Please, replace `’words.txt’` with the actual path to your file and make sure the file has the right format as described in the problem. The code will find all square anagram word pairs and report the largest square number.
In a real task, error handling, commentaries, and possibly optimization might be needed as well.

More Answers:
Amicable Chains
Su Doku
Large Non-Mersenne Prime

Error 403 The request cannot be completed because you have exceeded your quota. : quotaExceeded

Share:

Recent Posts