Facile 0 risolte 0% di accettazione

Neon Number

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

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

0 ≤ n ≤ 10^9

Esempi

Esempio 1
Input:
n = 9

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

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

Output atteso:
bool expected = true;

Spiegazione

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

Esempio 2
Input:
n = 12

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

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

Output atteso:
bool expected = false;

Spiegazione

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

Come affrontarlo

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

Suggerimento 1

Compute n × n in a 64-bit integer.

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

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

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

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