Four-digit Armstrong numbers#8642

Four-digit Armstrong numbers (Editorial)

Let's iterate through the numbers from to . For each number i = \overline{xyzu}~(a ≤ i ≤ b) extract the digits of thousands , hundreds , tens , and ones . If the number is an Armstrong number (i = x^4 + y^4 + z^4 + u^4), print it.

Algorithm implementation

Read the input data.

scanf("%d %d", &a, &b);

Iterate through the numbers from to .

for (i = a; i <= b; i++)
{

Extract the digits of the number i = \overline{xyzu}.

  x = a / 1000;
  y = i / 100 % 10;
  z = i / 10 % 10;
  u = i % 10;

If the number is an Armstrong number (i = x^4 + y^4 + z^4 + u^4), print it.

  if (x * x * x * x + y * y * y * y + z * z * z * z + 
      u * u * u * u == i) printf("%d ", i);
}

printf("\n");

Python implementation

Read the input data.

a, b = map(int, input().split())

Iterate through the numbers from to .

for i in range(a, b + 1):

Extract the digits of the number i = \overline{xyzu}.

  x = i // 1000
  y = (i // 100) % 10
  z = (i // 10) % 10
  u = i % 10

If the number is an Armstrong number (i = x^4 + y^4 + z^4 + u^4), print it.

  if x**4 + y**4 + z**4 + u**4 == i:
    print(i, end=" ")

print()