Skip to content

Commit 4bed7df

Browse files
Add palindrome-products exercise
1 parent 483fc2f commit 4bed7df

File tree

11 files changed

+1157
-0
lines changed

11 files changed

+1157
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,14 @@
588588
"prerequisites": [],
589589
"difficulty": 6
590590
},
591+
{
592+
"slug": "palindrome-products",
593+
"name": "Palindrome Products",
594+
"uuid": "14453347-8ecf-4e0a-88e6-f4632177251c",
595+
"practices": [],
596+
"prerequisites": [],
597+
"difficulty": 6
598+
},
591599
{
592600
"slug": "rail-fence-cipher",
593601
"name": "Rail Fence Cipher",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Instructions
2+
3+
Detect palindrome products in a given range.
4+
5+
A palindromic number is a number that remains the same when its digits are reversed.
6+
For example, `121` is a palindromic number but `112` is not.
7+
8+
Given a range of numbers, find the largest and smallest palindromes which
9+
are products of two numbers within that range.
10+
11+
Your solution should return the largest and smallest palindromes, along with the factors of each within the range.
12+
If the largest or smallest palindrome has more than one pair of factors within the range, then return all the pairs.
13+
14+
## Example 1
15+
16+
Given the range `[1, 9]` (both inclusive)...
17+
18+
And given the list of all possible products within this range:
19+
`[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]`
20+
21+
The palindrome products are all single digit numbers (in this case):
22+
`[1, 2, 3, 4, 5, 6, 7, 8, 9]`
23+
24+
The smallest palindrome product is `1`.
25+
Its factors are `(1, 1)`.
26+
The largest palindrome product is `9`.
27+
Its factors are `(1, 9)` and `(3, 3)`.
28+
29+
## Example 2
30+
31+
Given the range `[10, 99]` (both inclusive)...
32+
33+
The smallest palindrome product is `121`.
34+
Its factors are `(11, 11)`.
35+
The largest palindrome product is `9009`.
36+
Its factors are `(91, 99)`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"PalindromeProducts.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
14+
]
15+
},
16+
"blurb": "Detect palindrome products in a given range.",
17+
"source": "Problem 4 at Project Euler",
18+
"source_url": "https://projecteuler.net/problem=4"
19+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
unit PalindromeProducts;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
function smallest(const min, max : UInt64) : UInt64;
8+
function largest(const min, max : UInt64) : UInt64;
9+
10+
implementation
11+
12+
uses SysUtils;
13+
14+
function palindrome(number: UInt64) : Boolean;
15+
var
16+
digit : UInt64;
17+
reversed : UInt64;
18+
begin
19+
if (number mod 10) = 0 then
20+
begin
21+
result := (number = 0);
22+
exit;
23+
end;
24+
25+
reversed := 0;
26+
while number <> 0 do
27+
begin
28+
digit := number mod 10;
29+
number := number div 10;
30+
31+
if number = reversed then
32+
begin
33+
result := true;
34+
exit;
35+
end;
36+
37+
reversed := reversed * 10 + digit;
38+
if number = reversed then
39+
begin
40+
result := true;
41+
exit;
42+
end;
43+
end;
44+
45+
result := false;
46+
end;
47+
48+
function smallest(const min, max : UInt64) : UInt64;
49+
var
50+
first : UInt64;
51+
second : UInt64;
52+
product : UInt64;
53+
begin
54+
if min > max then
55+
raise ENotImplemented.Create('min must be <= max');
56+
57+
result := min * max + 1;
58+
for first := min to max do
59+
for second := first to max do
60+
begin
61+
product := first * second;
62+
if (result > product) and palindrome(product) then
63+
result := product;
64+
end;
65+
66+
if result = min * max + 1 then
67+
raise ENotImplemented.Create('no solution');
68+
end;
69+
70+
function largest(const min, max : UInt64) : UInt64;
71+
var
72+
first : UInt64;
73+
second : UInt64;
74+
product : UInt64;
75+
begin
76+
if min > max then
77+
raise ENotImplemented.Create('min must be <= max');
78+
79+
result := 0;
80+
for first := min to max do
81+
for second := first to max do
82+
begin
83+
product := first * second;
84+
if (result < product) and palindrome(product) then
85+
result := product;
86+
end;
87+
88+
if result = 0 then
89+
raise ENotImplemented.Create('no solution');
90+
end;
91+
92+
end.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[5cff78fe-cf02-459d-85c2-ce584679f887]
13+
description = "find the smallest palindrome from single digit factors"
14+
15+
[0853f82c-5fc4-44ae-be38-fadb2cced92d]
16+
description = "find the largest palindrome from single digit factors"
17+
18+
[66c3b496-bdec-4103-9129-3fcb5a9063e1]
19+
description = "find the smallest palindrome from double digit factors"
20+
21+
[a10682ae-530a-4e56-b89d-69664feafe53]
22+
description = "find the largest palindrome from double digit factors"
23+
24+
[cecb5a35-46d1-4666-9719-fa2c3af7499d]
25+
description = "find the smallest palindrome from triple digit factors"
26+
27+
[edab43e1-c35f-4ea3-8c55-2f31dddd92e5]
28+
description = "find the largest palindrome from triple digit factors"
29+
30+
[4f802b5a-9d74-4026-a70f-b53ff9234e4e]
31+
description = "find the smallest palindrome from four digit factors"
32+
33+
[787525e0-a5f9-40f3-8cb2-23b52cf5d0be]
34+
description = "find the largest palindrome from four digit factors"
35+
36+
[58fb1d63-fddb-4409-ab84-a7a8e58d9ea0]
37+
description = "empty result for smallest if no palindrome in the range"
38+
39+
[9de9e9da-f1d9-49a5-8bfc-3d322efbdd02]
40+
description = "empty result for largest if no palindrome in the range"
41+
42+
[12e73aac-d7ee-4877-b8aa-2aa3dcdb9f8a]
43+
description = "error result for smallest if min is more than max"
44+
45+
[eeeb5bff-3f47-4b1e-892f-05829277bd74]
46+
description = "error result for largest if min is more than max"
47+
48+
[16481711-26c4-42e0-9180-e2e4e8b29c23]
49+
description = "smallest product does not use the smallest factor"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SHELL = /bin/bash
2+
MAKEFLAGS += --no-print-directory
3+
DESTDIR = build
4+
EXECUTABLE = $(DESTDIR)/test
5+
COMMAND = fpc -l- -v0 -g -gl -Sa -Cr -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"
6+
7+
.ONESHELL:
8+
9+
test:
10+
@mkdir -p "./$(DESTDIR)"
11+
@cp -r ./lib "./$(DESTDIR)"
12+
@$(COMMAND) && ./$(EXECUTABLE) $(test)
13+
14+
clean:
15+
@rm -fr "./$(DESTDIR)"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
unit PalindromeProducts;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
function smallest(const min, max : UInt64) : UInt64;
8+
function largest(const min, max : UInt64) : UInt64;
9+
10+
implementation
11+
12+
uses SysUtils;
13+
14+
function smallest(const min, max : UInt64) : UInt64;
15+
begin
16+
17+
raise ENotImplemented.Create('Please implement your solution.'); result := min * max;
18+
19+
end;
20+
21+
function largest(const min, max : UInt64) : UInt64;
22+
begin
23+
24+
raise ENotImplemented.Create('Please implement your solution.'); result := min * max;
25+
26+
end;
27+
28+
end.

0 commit comments

Comments
 (0)