Facile 0 résolus 0 % de réussite

Neon Number

Check whether the digit sum of a number's square equals the number.

Ce que vous allez apprendre

  • Square a number without overflow
  • Sum the digits of the square
  • Compare the result with the input

Problème

Afficher la traduction

A neon number is a number whose square has a digit sum equal to the number itself. For example, 9² = 81 and 8 + 1 = 9, so 9 is a neon number.

Implement neon_number(n).

n² can reach 10^18, which fits in a signed 64-bit integer.

Input

n: The number to test.

Output

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

Contraintes

0 ≤ n ≤ 10^9

Exemples

Exemple 1
Entrée :
n = 9

Sortie attendue :
expected = True
Entrée :
long long n = 9;

Sortie attendue :
bool expected = true;
Entrée :
long n = 9;

Sortie attendue :
bool expected = true;

Explication

9² = 81 and 8 + 1 = 9, so the answer is true.

Exemple 2
Entrée :
n = 12

Sortie attendue :
expected = False
Entrée :
long long n = 12;

Sortie attendue :
bool expected = false;
Entrée :
long n = 12;

Sortie attendue :
bool expected = false;

Explication

12² = 144 and 1 + 4 + 4 = 9, which is not 12, so the answer is false.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Compute n × n in a 64-bit integer.

Indice 2

Add up the digits of the square with % 10 and / 10.

Indice 3

The digit sum of a square below 10^18 is at most 162, so large n can never be neon.

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.