Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,14 @@
"prerequisites": [],
"difficulty": 8
},
{
"slug": "palindrome-products",
"name": "Palindrome Products",
"uuid": "ddbf1590-ce3d-4ab5-94c5-08ecbee79764",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "zebra-puzzle",
"name": "Zebra Puzzle",
Expand Down
36 changes: 36 additions & 0 deletions exercises/practice/palindrome-products/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Instructions

Detect palindrome products in a given range.

A palindromic number is a number that remains the same when its digits are reversed.
For example, `121` is a palindromic number but `112` is not.

Given a range of numbers, find the largest and smallest palindromes which
are products of two numbers within that range.

Your solution should return the largest and smallest palindromes, along with the factors of each within the range.
If the largest or smallest palindrome has more than one pair of factors within the range, then return all the pairs.

## Example 1

Given the range `[1, 9]` (both inclusive)...

And given the list of all possible products within this range:
`[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 15, 21, 24, 27, 20, 28, 32, 36, 25, 30, 35, 40, 45, 42, 48, 54, 49, 56, 63, 64, 72, 81]`

The palindrome products are all single digit numbers (in this case):
`[1, 2, 3, 4, 5, 6, 7, 8, 9]`

The smallest palindrome product is `1`.
Its factors are `(1, 1)`.
The largest palindrome product is `9`.
Its factors are `(1, 9)` and `(3, 3)`.

## Example 2

Given the range `[10, 99]` (both inclusive)...

The smallest palindrome product is `121`.
Its factors are `(11, 11)`.
The largest palindrome product is `9009`.
Its factors are `(91, 99)`.
19 changes: 19 additions & 0 deletions exercises/practice/palindrome-products/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"palindrome-products.el"
],
"test": [
"palindrome-products-test.el"
],
"example": [
".meta/example.el"
]
},
"blurb": "Detect palindrome products in a given range.",
"source": "Problem 4 at Project Euler",
"source_url": "https://projecteuler.net/problem=4"
}
74 changes: 74 additions & 0 deletions exercises/practice/palindrome-products/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
;;; palindrome-products.el --- Palindrome Products (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl-lib)


(defun is-palindrome (n)
(if (= 0 (% n 10))
(= 0 n)
(let ((acc 0)
(digit 0))
(cl-loop while (< acc n)
do (setq digit (% n 10))
do (setq n (/ n 10))

when (= acc n)
do (cl-return t)

do (setq acc (+ (* 10 acc) digit))

when (= acc n)
do (cl-return t)

finally return nil))))


(defun smallest (min-factor max-factor)
(when (< max-factor min-factor)
(error "min must be <= max"))

(let ((value nil)
(factors nil))
(cl-loop for first from min-factor to max-factor
do (cl-loop for second from first to max-factor
for product = (* first second)

do (if (and value (< value product)) (cl-return))

when (is-palindrome product)
do (cond ((or (null factors) (< product value))
(setq value product)
(setq factors `((,first ,second))))
((= product value)
(push `(,first ,second) factors)))))
(list :value value :factors (reverse factors))))


(defun largest (min-factor max-factor)
(when (< max-factor min-factor)
(error "min must be <= max"))

(let ((value nil)
(factors nil))
(cl-loop for second from max-factor downto min-factor
do (cl-loop for first from second downto min-factor
for product = (* first second)

do (if (and value (< product value)) (cl-return))

when (is-palindrome product)
do (cond ((or (null factors) (< value product))
(setq value product)
(setq factors `((,first ,second))))
((= product value)
(push `(,first ,second) factors)))))
(list :value value :factors (reverse factors))))


(provide 'palindrome-products)
;;; palindrome-products.el ends here

49 changes: 49 additions & 0 deletions exercises/practice/palindrome-products/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[5cff78fe-cf02-459d-85c2-ce584679f887]
description = "find the smallest palindrome from single digit factors"

[0853f82c-5fc4-44ae-be38-fadb2cced92d]
description = "find the largest palindrome from single digit factors"

[66c3b496-bdec-4103-9129-3fcb5a9063e1]
description = "find the smallest palindrome from double digit factors"

[a10682ae-530a-4e56-b89d-69664feafe53]
description = "find the largest palindrome from double digit factors"

[cecb5a35-46d1-4666-9719-fa2c3af7499d]
description = "find the smallest palindrome from triple digit factors"

[edab43e1-c35f-4ea3-8c55-2f31dddd92e5]
description = "find the largest palindrome from triple digit factors"

[4f802b5a-9d74-4026-a70f-b53ff9234e4e]
description = "find the smallest palindrome from four digit factors"

[787525e0-a5f9-40f3-8cb2-23b52cf5d0be]
description = "find the largest palindrome from four digit factors"

[58fb1d63-fddb-4409-ab84-a7a8e58d9ea0]
description = "empty result for smallest if no palindrome in the range"

[9de9e9da-f1d9-49a5-8bfc-3d322efbdd02]
description = "empty result for largest if no palindrome in the range"

[12e73aac-d7ee-4877-b8aa-2aa3dcdb9f8a]
description = "error result for smallest if min is more than max"

[eeeb5bff-3f47-4b1e-892f-05829277bd74]
description = "error result for largest if min is more than max"

[16481711-26c4-42e0-9180-e2e4e8b29c23]
description = "smallest product does not use the smallest factor"
97 changes: 97 additions & 0 deletions exercises/practice/palindrome-products/palindrome-products-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
;;; palindrome-products-test.el --- Tests for Palindrome Products (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(load-file "palindrome-products.el")
(declare-function smallest "palindrome-products.el" (min-factor max-factor))
(declare-function largest "palindrome-products.el" (min-factor max-factor))


(ert-deftest find-the-smallest-palindrome-from-single-digit-factors ()
(should (equal '(:value 1
:factors ((1 1)))
(smallest 1 9))))


(ert-deftest find-the-largest-palindrome-from-single-digit-factors ()
(should (equal '(:value 9
:factors ((1 9) (3 3)))
(largest 1 9))))


(ert-deftest find-the-smallest-palindrome-from-double-digit-factors ()
(should (equal '(:value 121
:factors ((11 11)))
(smallest 10 99))))


(ert-deftest find-the-largest-palindrome-from-double-digit-factors ()
(should (equal '(:value 9009
:factors ((91 99)))
(largest 10 99))))


(ert-deftest find-the-smallest-palindrome-from-triple-digit-factors ()
(should (equal '(:value 10201
:factors ((101 101)))
(smallest 100 999))))


(ert-deftest find-the-largest-palindrome-from-triple-digit-factors ()
(should (equal '(:value 906609
:factors ((913 993)))
(largest 100 999))))


(ert-deftest find-the-smallest-palindrome-from-four-digit-factors ()
(should (equal '(:value 1002001
:factors ((1001 1001)))
(smallest 1000 9999))))


(ert-deftest find-the-largest-palindrome-from-four-digit-factors ()
(should (equal '(:value 99000099
:factors ((9901 9999)))
(largest 1000 9999))))


(ert-deftest empty-result-for-smallest-if-no-palindrome-in-the-range ()
(should (equal '(:value nil
:factors ())
(smallest 1002 1003))))


(ert-deftest empty-result-for-largest-if-no-palindrome-in-the-range ()
(should (equal '(:value nil
:factors ())
(largest 15 15))))


(ert-deftest error-result-for-smallest-if-min-is-more-than-max ()
(should-error (smallest 10000 1)))


(ert-deftest error-result-for-largest-if-min-is-more-than-max ()
(should-error (largest 2 1)))


(ert-deftest smallest-product-does-not-use-the-smallest-factor ()
(should (equal '(:value 10988901
:factors ((3297 3333)))
(smallest 3215 4000))))

(ert-deftest find-the-smallest-palindrome-from-six-digit-factors ()
(should (equal '(:value 50067176005
:factors ((223619 223895)))
(smallest 223617 244818))))

(ert-deftest find-the-largest-palindrome-from-six-digit-factors ()
(should (equal '(:value 59842824895
:factors ((244445 244811)))
(largest 223617 244818))))

(provide 'palindrome-products-test)
;;; palindrome-products-test.el ends here
17 changes: 17 additions & 0 deletions exercises/practice/palindrome-products/palindrome-products.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
;;; palindrome-products.el --- Palindrome Products (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun smallest (min-factor max-factor)
(error "Delete this S-Expression and write your own implementation"))

(defun largest (min-factor max-factor)
(error "Delete this S-Expression and write your own implementation"))


(provide 'palindrome-products)
;;; palindrome-products.el ends here