Excel BI - Excel Challenge 891

excel-challenges
excel-formulas
πŸ”° Determine which numbers in the list can be expressed as the sum of two perfect squares of positive integers in at least two different ways.
Published

March 24, 2026

Illustration for Excel BI - Excel Challenge 891

Challenge Description

πŸ”° Determine which numbers in the list can be expressed as the sum of two perfect squares of positive integers in at least two different ways. Populate No if the number doesn’t meet this criterion.

Solutions

library(tidyverse)
library(readxl)

path <- "Excel/800-899/891/891 Sum of Two Perfect Squares.xlsx"
input <- read_excel(path, range = "A1:A15")
test <- read_excel(path, range = "B1:B15")

find_square_pairs <- function(n) {
  limit <- floor(sqrt(n))
  pairs <- expand.grid(i = 0:limit, j = 0:limit) %>%
    filter(i <= j, i^2 + j^2 == n) %>%
    arrange(i, j) %>%
    mutate(pair = paste(i, j, sep = "-")) %>%
    pull(pair)
  if (length(pairs) == 0) {
    return("")
  }
  paste(pairs, collapse = ", ")
}

results <- input %>%
  mutate(
    Pairs = map_chr(Number, find_square_pairs),
    Pairs = ifelse(Pairs == '', "No", Pairs)
  )

all.equal(results$Pairs, test$`Answer Expected`, check.attributes = FALSE)
# 3rd solution is not correct
  • Logic: Read the workbook ranges needed for the challenge; Derive the required intermediate columns; Apply the business rule conditions explicitly.
  • Strengths: The code maps the workbook rule into a compact, reproducible pipeline.
  • Areas for Improvement: The solution assumes the workbook layout and selected ranges remain stable, so any structural change in the sheet would require small adjustments.
  • Gem: The elegant part is how little code is needed once the correct intermediate representation is chosen.
import pandas as pd
import math
from itertools import product

path = "Excel/800-899/891/891 Sum of Two Perfect Squares.xlsx"

input = pd.read_excel(path, usecols="A", nrows=15)
test = pd.read_excel(path, usecols="B", nrows=15)


def find_square_pairs(n):
    limit = math.isqrt(n)
    pairs = [
        f"{i}-{j}"
        for i, j in product(range(limit + 1), repeat=2)
        if i <= j and i**2 + j**2 == n
    ]
    return ", ".join(pairs) if pairs else "No"

input["Pairs"] = input["Number"].apply(find_square_pairs)

print(input["Pairs"]==test["Answer Expected"])
# 3rd result is incorrect.

The Python version keeps the algorithm explicit, which helps when the challenge depends on a greedy or iterative rule.

Difficulty Level

Easy / Medium

The business rule is clear, though the workbook still needs a few transformation steps to reach the expected output.