Einfach 0 gelöst 0% Erfolgsquote

Strong Number

Check whether a number equals the sum of the factorials of its digits.

Das lernst du

  • Split a number into its digits with % and /
  • Precompute small factorials once
  • Compare a computed total with the original number

Problem

Übersetzung anzeigen

A strong number is a number that equals the sum of the factorials of its digits. For example, 145 is strong because 1! + 4! + 5! = 1 + 24 + 120 = 145. Mathematicians call these numbers *factorions*; in base 10 there are only four of them, but your program should test the definition rather than a list.

Implement strong_number(n).

Use this definition: take each decimal digit d of n, add up d! for all of them (with 0! = 1), and compare the total with n. So 0 is not strong, because 0! = 1.

Input

n: The number to test.

Output

Return true if the sum of the factorials of the digits of n equals n, otherwise false.

Einschränkungen

0 ≤ n ≤ 10^9

Beispiele

Beispiel 1
Eingabe:
n = 145

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

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

Erwartete Ausgabe:
bool expected = true;

Erläuterung

1! + 4! + 5! = 1 + 24 + 120 = 145, which is the number itself, so the answer is true.

Beispiel 2
Eingabe:
n = 123

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

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

Erwartete Ausgabe:
bool expected = false;

Erläuterung

1! + 2! + 3! = 1 + 2 + 6 = 9, and 9 ≠ 123, so the answer is false.

So gehst du vor

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

Hinweis 1

Keep a copy of n, because you will change the working value while taking digits off.

Hinweis 2

Only the factorials of 0 to 9 are ever needed, so build them once.

Hinweis 3

Check what your loop does when n is 0: it has one digit.

Erwartete Komplexität

O(d) time for d digits, O(1) extra space

Referenzen

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

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

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