Fácil 0 resueltos 0% de aceptación

Neon Number

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

Lo que aprenderás

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

Problema

Ver traducción

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.

Restricciones

0 ≤ n ≤ 10^9

Ejemplos

Ejemplo 1
Entrada:
n = 9

Salida esperada:
expected = True
Entrada:
long long n = 9;

Salida esperada:
bool expected = true;
Entrada:
long n = 9;

Salida esperada:
bool expected = true;

Explicación

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

Ejemplo 2
Entrada:
n = 12

Salida esperada:
expected = False
Entrada:
long long n = 12;

Salida esperada:
bool expected = false;
Entrada:
long n = 12;

Salida esperada:
bool expected = false;

Explicación

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

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

Compute n × n in a 64-bit integer.

Pista 2

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

Pista 3

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

Complejidad esperada

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

Referencias

Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.

Clasificación

Clasificación completa

La mejor solución aprobada de cada programador. Las más rápidas, ligeras y mejor valoradas van primero.

Aún no hay soluciones aprobadas. Resuélvelo y quédate el primer puesto.

Aprende de otros

Soluciones de la comunidad

Aún no hay soluciones publicadas. Resuelve este reto para compartir la primera.