← Back to challenges

Simple Numbers

PythonHardmathnumbersinterviewlogic

Instructions

Mubashir needs your help to find the Simple Numbers in a given range.

A number X, that has an N amount of digits (which we'll enumerate as d1, d2, ..., dN), is Simple if the following equation holds true:

X = d1^1 + d2^2 + ... + dN^N

Examples of Simple Numbers:

89 = 8^1 + 9^2

135 = 1^1 + 3^2 + 5^3

Create a function which returns a list of all the Simple Numbers that exist within a given range between a and b (both numbers are inclusive).

  1. Generate a list of the individual digits of the number.
  2. Filter the list so that only "simple numbers" are kept.
  3. To find out if a number is "simple":
    1. Take the digits of the number.
    2. For each digit, calculate digit ^ (index_of_the_digit + 1).
    3. Sum the results of the calculations above and compare with the original number. If they're equal, the number is "simple".

Examples

simple_numbers(1, 10) ➞ [1, 2, 3, 4, 5, 6, 7, 8, 9]

simple_numbers(1, 100) ➞ [1, 2, 3, 4, 5, 6, 7, 8, 9, 89]

simple_numbers(90, 100) ➞ []

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Combinatorial Exploration