通过以下过程定义快乐数n。以n开头,将其替换为数字的平方和,然后重复该过程,直到n等于1,或者它在不包含1的循环中无休止地循环。 ,而那些不以1结尾的数字是不满意的数字。
前几个快乐数字是1,7,10,13,19,23,28,31,32,44,49,68,70,79,82,86,91,94,97,100
例子:
Input : 23
Output : Yes
Explanation :
First Iteration:
22 + 32 = 4 + 9 = 13
Second Iteration:
12 + 32 = 1 + 9 = 10
Third Iteration:
12 + 02 = 1 + 0 = 1
Since we reach 1, 23 is Happy.
想法很简单,我们重复做数字平方和。在执行此操作时,我们使用哈希跟踪访问的号码。如果达到1,则返回true。否则,如果我们访问了一个号码,则返回false。
C++
// CPP program to check if a number
// is happy number
#include
using namespace std;
// Returns sum of squares of digits
// of a number n. For example for n = 12
// it returns 1 + 4 = 5
int sumDigitSquare(int n)
{
int sq = 0;
while (n) {
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}
// Returns true if n is Happy number
// else returns false.
bool isHappy(int n)
{
// A set to store numbers during
// repeated square sum process
set s;
s.insert(n);
// Keep replacing n with sum of
// squares of digits until we either
// reach 1 or we endup in a cycle
while (1) {
// Number is Happy if we reach 1
if (n == 1)
return true;
// Replace n with sum of squares
// of digits
n = sumDigitSquare(n);
// If n is already visited, a cycle
// is formed, means not Happy
if (s.find(n) != s.end())
return false;
// Mark n as visited
s.insert(n);
}
return false;
}
// Driver code
int main()
{
int n = 23;
if (isHappy(n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
Java
// Java program to check if a number
// is happy number
import java.util.*;
class GFG
{
// Returns sum of squares of digits
// of a number n. For example for n = 12
// it returns 1 + 4 = 5
static int sumDigitSquare(int n)
{
int sq = 0;
while (n > 0)
{
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}
// Returns true if n is Happy number
// else returns false.
static boolean isHappy(int n)
{
// A set to store numbers during
// repeated square sum process
HashSet s = new HashSet();
s.add(n);
// Keep replacing n with sum of
// squares of digits until we either
// reach 1 or we endup in a cycle
while (true)
{
// Number is Happy if we reach 1
if (n == 1)
return true;
// Replace n with sum of squares
// of digits
n = sumDigitSquare(n);
// If n is already visited, a cycle
// is formed, means not Happy
if ((s.contains(n) && n != (int)s.toArray()[ s.size()-1 ] ))
return false;
// Mark n as visited
s.add(n);
}
}
// Driver code
public static void main(String[] args)
{
int n = 23;
if (isHappy(n))
System.out.println("Yes");
else
System.out.println("No");
}
}
/* This code contributed by PrinciRaj1992 */
Python 3
# python program to check if a number
# is happy number
# Returns sum of squares of digits
# of a number n. For example for n = 12
# it returns 1 + 4 = 5
def sumDigitSquare( n):
sq = 0;
while (n!=0):
digit = n % 10
sq += digit * digit
n = n // 10
return sq;
# Returns true if n is Happy number
# else returns false.
def isHappy(n):
# A set to store numbers during
# repeated square sum process
s=set()
s.add(n)
# Keep replacing n with sum of
# squares of digits until we either
# reach 1 or we endup in a cycle
while (True):
# Number is Happy if we reach 1
if (n == 1):
return True;
# Replace n with sum of squares
# of digits
n = sumDigitSquare(n)
# If n is already visited, a cycle
# is formed, means not Happy
if n in s:
return False
# Mark n as visited
s.add(n)
return false;
# Driver code
n = 4
if (isHappy(n)):
print("Yes")
else:
print("No")
C#
// C# program to check if a number
// is happy number
using System;
using System.Collections.Generic;
class GFG
{
// Returns sum of squares of digits
// of a number n. For example for n = 12
// it returns 1 + 4 = 5
static int sumDigitSquare(int n)
{
int sq = 0;
while (n > 0)
{
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}
// Returns true if n is Happy number
// else returns false.
static bool isHappy(int n)
{
// A set to store numbers during
// repeated square sum process
HashSet s = new HashSet();
s.Add(n);
// Keep replacing n with sum of
// squares of digits until we either
// reach 1 or we endup in a cycle
while (true)
{
// Number is Happy if we reach 1
if (n == 1)
return true;
// Replace n with sum of squares
// of digits
n = sumDigitSquare(n);
// If n is already visited, a cycle
// is formed, means not Happy
if (s.Contains(n))
return false;
// Mark n as visited
s.Add(n);
}
}
// Driver code
public static void Main()
{
int n = 23;
if (isHappy(n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code contributed by Rajput-Ji
C++
// A space optimized CPP program to check
// if a number is happy number
#include
using namespace std;
// Returns sum of squares of digits
// of a number n. For example for n = 12
// it returns 1 + 4 = 5
int sumDigitSquare(int n)
{
int sq = 0;
while (n) {
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}
// Returns true if n is Happy number
// else returns false.
bool isHappy(int n)
{
// Keep replacing n with sum of
// squares of digits until we either
// reach 1 or we end up in a cycle
while (1) {
// Number is Happy if we reach 1
if (n == 1)
return true;
// Replace n with sum of squares
// of digits
n = sumDigitSquare(n);
// Number is not Happy if we reach 4
if (n == 4)
return false;
}
return false;
}
// Driver code
int main()
{
int n = 23;
if (isHappy(n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
Java
// A space optimized Java
// program to check if a
// number is happy number
import java.io.*;
class GFG {
// Returns sum of squares of
// digits of a number n. For
// example for n = 12 it
// returns 1 + 4 = 5
static int sumDigitSquare(int n)
{
int sq = 0;
while (n != 0)
{
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}
// Returns true if n is Happy
// number else returns false.
static boolean isHappy(int n)
{
// Keep replacing n with
// sum of squares of digits
// until we either reach 1
// or we end up in a cycle
while (true)
{
// Number is Happy if
// we reach 1
if (n == 1)
return true;
// Replace n with sum
// of squares of digits
n = sumDigitSquare(n);
// Number is not Happy
// if we reach 4
if (n == 4)
return false;
}
}
// Driver code
public static void main(String args[])
{
int n = 23;
if (isHappy(n))
System.out.println("Yes");
else
System.out.println("No" );
}
}
/*This code is contributed by Nikita tiwari.*/
Python3
# A space optimized Python3 program to
# check if a number is happy number
# Returns sum of squares of
# digits of a number n. For
# example for n = 12 it
# returns 1 + 4 = 5
def sumDigitSquare(n) :
sq = 0
while (n) :
digit = n % 10
sq = sq + digit * digit
n = n // 10
return sq
# Returns true if n
# is Happy number else
# returns false.
def isHappy(n) :
# Keep replacing n with
# sum of squares of digits
# until we either reach 1
# or we end up in a cycle
while (1) :
# Number is Happy if
# we reach 1
if (n == 1) :
return True
# Replace n with sum of
# squares of digits
n = sumDigitSquare(n)
# Number is not Happy
# if we reach 4
if (n == 4) :
return False
return False
# Driver code
n = 23
if (isHappy(n)) :
print("Yes")
else :
print("No")
# This code is contributed
# by Nikita tiwari.
C#
// A space optimized C#
// program to check if a
// number is happy number
using System;
class GFG
{
// Returns sum of squares of
// digits of a number n. For
// example for n = 12 it
// returns 1 + 4 = 5
static int sumDigitSquare(int n)
{
int sq = 0;
while (n != 0)
{
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}
// Returns true if n is Happy
// number else returns false.
static bool isHappy(int n)
{
// Keep replacing n with
// sum of squares of digits
// until we either reach 1
// or we end up in a cycle
while (true)
{
// Number is Happy if
// we reach 1
if (n == 1)
return true;
// Replace n with sum
// of squares of digits
n = sumDigitSquare(n);
// Number is not Happy
// if we reach 4
if (n == 4)
return false;
}
}
// Driver code
static public void Main ()
{
int n = 23;
if (isHappy(n))
Console.WriteLine("Yes");
else
Console.WriteLine("No" );
}
}
// This code is contributed by ajit
PHP
输出:
Yes
一个重要的发现是,循环始终包含4 。因此,我们不需要跟踪所有数字。我们可以简单地检查4。
C++
// A space optimized CPP program to check
// if a number is happy number
#include
using namespace std;
// Returns sum of squares of digits
// of a number n. For example for n = 12
// it returns 1 + 4 = 5
int sumDigitSquare(int n)
{
int sq = 0;
while (n) {
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}
// Returns true if n is Happy number
// else returns false.
bool isHappy(int n)
{
// Keep replacing n with sum of
// squares of digits until we either
// reach 1 or we end up in a cycle
while (1) {
// Number is Happy if we reach 1
if (n == 1)
return true;
// Replace n with sum of squares
// of digits
n = sumDigitSquare(n);
// Number is not Happy if we reach 4
if (n == 4)
return false;
}
return false;
}
// Driver code
int main()
{
int n = 23;
if (isHappy(n))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
Java
// A space optimized Java
// program to check if a
// number is happy number
import java.io.*;
class GFG {
// Returns sum of squares of
// digits of a number n. For
// example for n = 12 it
// returns 1 + 4 = 5
static int sumDigitSquare(int n)
{
int sq = 0;
while (n != 0)
{
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}
// Returns true if n is Happy
// number else returns false.
static boolean isHappy(int n)
{
// Keep replacing n with
// sum of squares of digits
// until we either reach 1
// or we end up in a cycle
while (true)
{
// Number is Happy if
// we reach 1
if (n == 1)
return true;
// Replace n with sum
// of squares of digits
n = sumDigitSquare(n);
// Number is not Happy
// if we reach 4
if (n == 4)
return false;
}
}
// Driver code
public static void main(String args[])
{
int n = 23;
if (isHappy(n))
System.out.println("Yes");
else
System.out.println("No" );
}
}
/*This code is contributed by Nikita tiwari.*/
Python3
# A space optimized Python3 program to
# check if a number is happy number
# Returns sum of squares of
# digits of a number n. For
# example for n = 12 it
# returns 1 + 4 = 5
def sumDigitSquare(n) :
sq = 0
while (n) :
digit = n % 10
sq = sq + digit * digit
n = n // 10
return sq
# Returns true if n
# is Happy number else
# returns false.
def isHappy(n) :
# Keep replacing n with
# sum of squares of digits
# until we either reach 1
# or we end up in a cycle
while (1) :
# Number is Happy if
# we reach 1
if (n == 1) :
return True
# Replace n with sum of
# squares of digits
n = sumDigitSquare(n)
# Number is not Happy
# if we reach 4
if (n == 4) :
return False
return False
# Driver code
n = 23
if (isHappy(n)) :
print("Yes")
else :
print("No")
# This code is contributed
# by Nikita tiwari.
C#
// A space optimized C#
// program to check if a
// number is happy number
using System;
class GFG
{
// Returns sum of squares of
// digits of a number n. For
// example for n = 12 it
// returns 1 + 4 = 5
static int sumDigitSquare(int n)
{
int sq = 0;
while (n != 0)
{
int digit = n % 10;
sq += digit * digit;
n = n / 10;
}
return sq;
}
// Returns true if n is Happy
// number else returns false.
static bool isHappy(int n)
{
// Keep replacing n with
// sum of squares of digits
// until we either reach 1
// or we end up in a cycle
while (true)
{
// Number is Happy if
// we reach 1
if (n == 1)
return true;
// Replace n with sum
// of squares of digits
n = sumDigitSquare(n);
// Number is not Happy
// if we reach 4
if (n == 4)
return false;
}
}
// Driver code
static public void Main ()
{
int n = 23;
if (isHappy(n))
Console.WriteLine("Yes");
else
Console.WriteLine("No" );
}
}
// This code is contributed by ajit
的PHP
输出:
Yes