Einfach 0 gelöst 0% Erfolgsquote

Armstrong Number

Check whether a number equals the sum of its digits raised to the digit count.

Das lernst du

  • Count the digits of a number
  • Raise each digit to the digit count and add them up
  • Compare the result with the original number

Problem

Übersetzung anzeigen

A number with k digits is an Armstrong number if the sum of each digit raised to the power k equals the number. For example 153 = 1³ + 5³ + 3³.

Implement is_armstrong(n).

Input

n: A non-negative integer.

Output

Return true if n is an Armstrong number.

Einschränkungen

0 ≤ n ≤ 10¹⁸

Beispiele

Beispiel 1
Eingabe:
n = 153

Erwartete Ausgabe:
expected = True
Eingabe:
long long n = 153;

Erwartete Ausgabe:
bool expected = true;
Eingabe:
long n = 153;

Erwartete Ausgabe:
bool expected = true;

Erläuterung

1³ + 5³ + 3³ = 1 + 125 + 27 = 153.

Beispiel 2
Eingabe:
n = 10

Erwartete Ausgabe:
expected = False
Eingabe:
long long n = 10;

Erwartete Ausgabe:
bool expected = false;
Eingabe:
long n = 10;

Erwartete Ausgabe:
bool expected = false;

Erläuterung

1² + 0² = 1, which is not 10.

So gehst du vor

Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.

Hinweis 1

Count how many digits the number has before summing powers.

Hinweis 2

Compare the original number with the sum of each digit raised to that digit count.

Hinweis 3

Remember that 0 is a special one-digit case.

Erwartete Komplexität

O(k) time, O(1) extra space

Referenzen

Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.