Torna alle sfide
Facile 0 risolte 0% di accettazione

Armstrong Number

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

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

0 ≤ n ≤ 10¹⁸

Esempi

Esempio 1
Input:
n = 153

Output atteso:
expected = True
Input:
long long n = 153;

Output atteso:
bool expected = true;
Input:
long n = 153;

Output atteso:
bool expected = true;

Spiegazione

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

Esempio 2
Input:
n = 10

Output atteso:
expected = False
Input:
long long n = 10;

Output atteso:
bool expected = false;
Input:
long n = 10;

Output atteso:
bool expected = false;

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Count how many digits the number has before summing powers.

Suggerimento 2

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

Suggerimento 3

Remember that 0 is a special one-digit case.

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.