Databases 4 min read

Solve the 5‑Digit Reverse‑Multiplication Puzzle with SQL Queries

A father turned his daughter's third‑grade math challenge—finding a five‑digit number that becomes its reverse when multiplied by four—into a collaborative coding exercise, showcasing three distinct Oracle SQL solutions that generate and test digit permutations to reveal the unique answer.

ITPUB
ITPUB
ITPUB
Solve the 5‑Digit Reverse‑Multiplication Puzzle with SQL Queries

A father encountered his third‑grade daughter's math problem: find a five‑digit number ABCDE such that ABCDE * 4 = EDCBA, with all digits distinct. Curious about solving it with code, he posted the challenge and invited the community to provide SQL solutions.

Solution by qingyun

This approach builds all permutations of five digits (0‑9) using a hierarchical query, filters out repetitions, constructs the number, and checks the multiplication condition.

with a as (select level-1 as x from dual connect by level <= 10)
select a.x*10000 + b.x*1000 + c.x*100 + d.x*10 + e.x as y
from a a, a b, a c, a d, a e
where a.x > 0
  and (a.x*10000 + b.x*1000 + c.x*100 + d.x*10 + e.x) * 4 =
      e.x*10000 + d.x*1000 + c.x*100 + b.x*10 + a.x
  and a.x <> b.x and a.x <> c.x and b.x <> c.x
  and a.x <> d.x and b.x <> d.x and c.x <> d.x
  and a.x <> e.x and b.x <> e.x and c.x <> e.x and d.x <> e.x;

Solution by regonly1

This method generates numbers of up to five digits, creates their reversed form, and filters those that satisfy the 4× relationship.

select * from (
  select level lvl,
         to_number(replace(sys_connect_by_path(l, ','), ',')) num,
         to_number(reverse(replace(sys_connect_by_path(l, ','), ','))) rnum
  from (select level-1 l from dual connect by level <= 10)
  connect by nocycle prior l <> l and level <= 5)
where num >= 10000
  and num < 100000
  and num * 4 = rnum;

Solution by hudingchen

A concise query that generates candidate numbers directly and checks the reverse‑multiply condition.

SELECT m.v
FROM (SELECT LEVEL*4 v FROM DUAL CONNECT BY LEVEL <= 99999/4) m
WHERE m.v >= 40000
  AND m.v = REVERSE(TO_CHAR(m.v)) * 4;

The query returns the unique solution 87912, confirming that 87912 * 4 = 21978, which is the reverse of the original number.

These examples illustrate how a seemingly pure‑math puzzle can be tackled with relational‑database techniques, turning a classroom exercise into an engaging coding challenge for developers.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SQLqueryNumber theoryMath PuzzleDigit Permutation
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.