‹ back to the board

Wednesday, October 14

Apprenticelogic & control flow

The Glamoured Arch of Fennowyn Hollow

Deep in Fennowyn Hollow stands a mossy arch of white stone. The fey of the Hollow have glamoured it, so it opens only for a traveller who speaks the right number aloud. Each dusk the warden-sprites carve fresh terms into the keystone, and the Thimble Court pays a bounty to any adventurer who can name the number that passes. Too many have guessed wrong and wandered the Hollow until dawn.

The terms are four non-negative integers: lo, hi, total and divisor. The number to be spoken must meet all three of these conditions:

  • it is an integer n with lo <= n <= hi (both bounds inclusive);
  • the sum of the decimal digits of n equals total;
  • n is evenly divisible by divisor.

Write archNumber(lo, hi, total, divisor), which returns the smallest integer meeting all three conditions. If no integer in the range meets them, return -1.

Constraints: 0 <= lo <= hi <= 100000, total >= 0, divisor >= 1. The digit sum of 0 is 0.

  • bounded search
  • Apprentice

examples

  1. case 1lo of 37, hi of 100, total of 9, divisor of 472 (45, 54 and 63 have digit sum 9 but are not divisible by 4; 36 would be, but it is below lo)
  2. case 2lo of 1, hi of 20, total of 10, divisor of 3-1 (only 19 has digit sum 10, and 19 is not divisible by 3)
  3. case 3lo of 0, hi of 0, total of 0, divisor of 50
reward
220 XP
signature
archNumber
not yet run

TypeScript, run as ES2022 — types are stripped, not checked. If a JS engine would run it, so will we.

loading the editor…
  • lo bound excludes an earlier match
  • first match inside range
  • small range with divisor 7
  • no number passes
  • zero is a valid answer
  • single-number range that passes
  • lo itself is the answer
  • hi itself is the answer
  • impossible digit sum
  • total zero without zero in range
The Glamoured Arch of Fennowyn Hollow · bitwick