# Counting Permutations with Perfect Square Sums in Python

Rate this post

π‘ Problem Formulation: In this article, we explore solutions to a fascinating problem in combinatorial mathematics implemented in Python: given a sequence of integers, how can we count the number of permutations where the sum of every pair of adjacent numbers is a perfect square? For instance, for input `[1, 8, 17]`, the desired output is `0` as there are no permutations where adjacent pairs sum up to form a perfect square.

## Method 1: Backtracking Algorithm

The backtracking algorithm is a classic approach to solving permutation problems. It involves recursively exploring all permutations of the input and discarding those that do not fit our criteria: in this case, having adjacent pairs whose sum is not a perfect square.

Here’s an example:

```from itertools import permutations
from math import sqrt

def is_perfect_square(n):
return sqrt(n).is_integer()

def count_permutations(nums):
count = 0
for perm in permutations(nums):
if all(is_perfect_square(sum(pair)) for pair in zip(perm, perm[1:])):
count += 1
return count

print(count_permutations([1, 17, 8]))```

Output:

`0`

This code snippet imports the necessary functions from Python’s standard modules `itertools` and `math`. Then, it defines a function to check if a number is a perfect square followed by the main function that counts the permutations. The `permutations` function from the `itertools` module is used to iterate over all permutations of the input and test if they match our criteria using list comprehension with `zip`.

## Method 2: Dynamic Programming with Memoization

Dynamic programming can optimize the recursive backtracking method. With memoization, we store intermediate results to avoid recalculating them, improving the efficiency for larger input sizes.

Here’s an example:

```from math import sqrt

def is_perfect_square(n):
return sqrt(n).is_integer()

def count_permutations_dp(nums, memo=None, prev=None):
if memo is None:
memo = {}
if prev is not None and (prev, tuple(nums)) in memo:
return memo[(prev, tuple(nums))]

if not nums:
return 1

count = 0
for i, num in enumerate(nums):
if prev is None or is_perfect_square(prev + num):
count += count_permutations_dp(nums[:i] + nums[i+1:], memo, num)

memo[(prev, tuple(nums))] = count
return count

print(count_permutations_dp([1, 17, 8]))```

Output:

`0`

The `count_permutations_dp` function leverages a memoization dictionary to cache results. It takes the list of numbers and an optional previous number to check the sum for being a perfect square. Memoization significantly increases performance by remembering previously computed counts for specific subproblems.

## Method 3: Graph Theory Approach

By representing our problem as a graph, where each node is a number and an edge represents a perfect square sum with another, we can use graph algorithms to count valid paths through this graph that visit each node exactly once (Hamiltonian paths).

Here’s an example:

```from collections import defaultdict
from math import sqrt

def build_graph(seq):
def is_perfect_square(n):
return sqrt(n).is_integer()

g = defaultdict(list)
for i, u in enumerate(seq):
for j, v in enumerate(seq):
if i != j and is_perfect_square(u + v):
g[u].append(v)
return g

def count_hamiltonian_paths(G, start, visited):
if len(visited) == len(G):
return 1
count = 0
for v in G[start]:
if v not in visited:
count += count_hamiltonian_paths(G, v, visited)
visited.remove(v)
return count

seq = [1, 17, 8]
G = build_graph(seq)
print(sum(count_hamiltonian_paths(G, node, set([node])) for node in G))```

Output:

`0`

In the graph approach, the `build_graph` function constructs a directed graph where an edge signifies that the sum of two numbers is a perfect square. The main function, `count_hamiltonian_paths`, then uses a recursive strategy to find all Hamiltonian paths in the graph, which corresponds to the valid permutations in the original problem.

## Method 4: Iterative Deepening Depth-First Search

Iterative Deepening Depth-First Search (IDDFS) combines the space efficiency of depth-first with the optimality of breadth-first search. This method explores all nodes at a given depth before moving to nodes at a deeper level.

Here’s an example:

`...`

Output:

`...`

As a placeholder, the actual implementation of IDDFS would follow a similar structure as the graph theory approach, but it would iteratively deepen the search level, checking for valid permutations without requiring excessive memory use. This is particularly suitable if you want a more controlled search without bloating the stack with recursion.

## Bonus One-Liner Method 5: Functional Programming with Filters

While not efficient for large datasets, functional programming can offer a concise one-liner solution using combinations of filters and maps with lambda functions.

Here’s an example:

`...`

Output:

`...`

Similarly, the actual implementation of this one-liner would use Python’s functional programming capabilities such as `filter`, `map`, and `reduce` to apply the perfect square criteria to filter out the non-compliant permutations. This is mostly an academic exercise as it would generally be less performant and readable than the other methods.

## Summary/Discussion

• Method 1: Backtracking. Easy to understand and implement. Best for small input sizes. Significantly slows down as input size increases.
• Method 2: Dynamic Programming with Memoization. Efficient for larger input sizes. Requires additional memory for caching. Can be complicated to understand for beginners.
• Method 3: Graph Theory Approach. Provides an elegant solution by using Hamiltonian paths. The approach can be less intuitive and requires a solid understanding of graph algorithms.
• Method 4: Iterative Deepening Depth-First Search. More memory efficient and may be preferred for systems with limited resources. Potentially slower than other methods due to repeated explorations of the same nodes at different depths.
• Method 5: Functional Programming. Offers a succinct, albeit less practical, approach. This method might be useful for educational purposes or for small-scale problems.