📜  快乐号码

📅  最后修改于: 2021-04-27 19:38:18             🧑  作者: Mango

如果一个数字在一系列步骤后都为1,则将其称为“快乐”,其中,每个步数都将用其数字的平方和代替,即,如果我们以“快乐数字”开头并继续用数字的平方和代替,则得出1。
例子 :

Input: n = 19
Output: True
19 is Happy Number,
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
As we reached to 1, 19 is a Happy Number.

Input: n = 20
Output: False

当数字按其顺序循环(即按顺序触摸已被触摸的数字)时,它不是快乐数字。因此,要检查一个数字是否满意,我们可以保留一个值,如果再次出现相同的数字,则将结果标记为不满意。可以将上述方法的一个简单函数编写如下:

CPP
//  method return true if n is Happy Number
//  numSquareSum method is given in below detailed code snippet
int isHappyNumber(int n)
{
    set st;
    while (1)
    {
        n = numSquareSum(n);
        if (n == 1)
            return true;
        if (st.find(n) != st.end())
            return false;
        st.insert(n);
    }
}


C++
//    C/C++ program to check a number is a Happy
//  number or not
#include 
using namespace std;
 
// Utility method to return sum of square of
// digit of n
int numSquareSum(int n)
{
    int squareSum = 0;
    while (n)
    {
        squareSum += (n % 10) * (n % 10);
        n /= 10;
    }
    return squareSum;
}
 
//    method return true if n is Happy number
bool isHappynumber(int n)
{
    int slow, fast;
 
    //    initialize slow and fast by n
    slow = fast = n;
    do
    {
        //    move slow number by one iteration
        slow = numSquareSum(slow);
 
        //    move fast number by two iteration
        fast = numSquareSum(numSquareSum(fast));
 
    }
    while (slow != fast);
 
    //    if both number meet at 1, then return true
    return (slow == 1);
}
 
//    Driver code to test above methods
int main()
{
    int n = 13;
    if (isHappynumber(n))
        cout << n << " is a Happy number\n";
    else
        cout << n << " is not a Happy number\n";
}


Java
//  Java program to check a number is a Happy
//  number or not
 
class GFG {
  
// Utility method to return sum of square of
// digit of n
static int numSquareSum(int n)
{
    int squareSum = 0;
    while (n!= 0)
    {
        squareSum += (n % 10) * (n % 10);
        n /= 10;
    }
    return squareSum;
}
  
//  method return true if n is Happy number
static boolean isHappynumber(int n)
{
    int slow, fast;
  
    //  initialize slow and fast by n
    slow = fast = n;
    do
    {
        //  move slow number
        // by one iteration
        slow = numSquareSum(slow);
  
        //  move fast number
        // by two iteration
        fast = numSquareSum(numSquareSum(fast));
  
    }
    while (slow != fast);
  
    //  if both number meet at 1,
    // then return true
    return (slow == 1);
}
  
//  Driver code to test above methods
public static void main(String[] args)
{
    int n = 13;
    if (isHappynumber(n))
        System.out.println(n +
        " is a Happy number");
    else
        System.out.println(n +
        " is not a Happy number");
}
}


Python3
# Python3 program to check a number
# is a Happy number or not
 
# Utility method to return
# sum of square of digit of n
def numSquareSum(n):
    squareSum = 0;
    while(n):
        squareSum += (n % 10) * (n % 10);
        n = int(n / 10);
    return squareSum;
 
# method return true if
# n is Happy number
def isHappynumber(n):
 
    # initialize slow
    # and fast by n
    slow = n;
    fast = n;
    while(True):
         
        # move slow number
        # by one iteration
        slow = numSquareSum(slow);
 
        # move fast number
        # by two iteration
        fast = numSquareSum(numSquareSum(fast));
        if(slow != fast):
            continue;
        else:
            break;
 
    # if both number meet at 1,
    # then return true
    return (slow == 1);
 
# Driver Code
n = 13;
if (isHappynumber(n)):
    print(n , "is a Happy number");
else:
    print(n , "is not a Happy number");
 
# This code is contributed by mits


C#
// C# program to check a number
// is a Happy number or not
using System;
class GFG {
 
// Utility method to return
// sum of square of digit of n
static int numSquareSum(int n)
{
    int squareSum = 0;
    while (n!= 0)
    {
        squareSum += (n % 10) *
                     (n % 10);
        n /= 10;
    }
    return squareSum;
}
 
// method return true if
// n is Happy number
static bool isHappynumber(int n)
{
    int slow, fast;
 
    // initialize slow and
    // fast by n
    slow = fast = n;
    do
    {
         
        // move slow number
        // by one iteration
        slow = numSquareSum(slow);
 
        // move fast number
        // by two iteration
        fast = numSquareSum(numSquareSum(fast));
 
    }
    while (slow != fast);
 
    // if both number meet at 1,
    // then return true
    return (slow == 1);
}
 
// Driver code
public static void Main()
{
    int n = 13;
    if (isHappynumber(n))
        Console.WriteLine(n +
        " is a Happy number");
    else
        Console.WriteLine(n +
        " is not a Happy number");
}
}
 
// This code is contributed by anuj_67.


PHP


Javascript


Java
//  This code is contributed by Vansh Sodhi.
//  Java program to check if a number is a Happy number or not.
 
class GFG {
 
     // method - returns true if the input is a happy
     //  number else returns false
     static boolean isHappynumber(int n) {
        if (n == 1 || n == 7)
            return true;
        int sum = n, x = n;
 
        // this loop executes till the sum of square of
        // digits obtained is not a single digit number
        while(sum > 9) {
            sum = 0;
 
            // this loop finds the sum of square of digits
            while (x > 0) {
                int d = x%10;
                sum += d*d;
                x/=10;
            }
            if (sum == 1)
                return true;
            x = sum;
        }
        if(sum == 7)
            return true;
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
       int n = 13;
       if (isHappynumber(n))
          System.out.println(n +
        " is a Happy number");
       else
          System.out.println(n +
        " is not a Happy number");
   }
}


我们可以在不使用额外空间的情况下解决此问题,并且该技术也可以用于其他一些类似的问题。如果我们将每个数字都视为一个节点,并用平方和的位数替换为一个链接,则此问题与在链接列表中找到一个循环相同:
因此,作为上述链接的建议解决方案,我们将使两个数字保持慢速和快速均从给定数字初始化,将慢速一次替换一个步骤,将快速一次替换两个步骤。如果他们在1点相遇,则给定的号码为“开心号码”,否则为“否”。

C++

//    C/C++ program to check a number is a Happy
//  number or not
#include 
using namespace std;
 
// Utility method to return sum of square of
// digit of n
int numSquareSum(int n)
{
    int squareSum = 0;
    while (n)
    {
        squareSum += (n % 10) * (n % 10);
        n /= 10;
    }
    return squareSum;
}
 
//    method return true if n is Happy number
bool isHappynumber(int n)
{
    int slow, fast;
 
    //    initialize slow and fast by n
    slow = fast = n;
    do
    {
        //    move slow number by one iteration
        slow = numSquareSum(slow);
 
        //    move fast number by two iteration
        fast = numSquareSum(numSquareSum(fast));
 
    }
    while (slow != fast);
 
    //    if both number meet at 1, then return true
    return (slow == 1);
}
 
//    Driver code to test above methods
int main()
{
    int n = 13;
    if (isHappynumber(n))
        cout << n << " is a Happy number\n";
    else
        cout << n << " is not a Happy number\n";
}

Java

//  Java program to check a number is a Happy
//  number or not
 
class GFG {
  
// Utility method to return sum of square of
// digit of n
static int numSquareSum(int n)
{
    int squareSum = 0;
    while (n!= 0)
    {
        squareSum += (n % 10) * (n % 10);
        n /= 10;
    }
    return squareSum;
}
  
//  method return true if n is Happy number
static boolean isHappynumber(int n)
{
    int slow, fast;
  
    //  initialize slow and fast by n
    slow = fast = n;
    do
    {
        //  move slow number
        // by one iteration
        slow = numSquareSum(slow);
  
        //  move fast number
        // by two iteration
        fast = numSquareSum(numSquareSum(fast));
  
    }
    while (slow != fast);
  
    //  if both number meet at 1,
    // then return true
    return (slow == 1);
}
  
//  Driver code to test above methods
public static void main(String[] args)
{
    int n = 13;
    if (isHappynumber(n))
        System.out.println(n +
        " is a Happy number");
    else
        System.out.println(n +
        " is not a Happy number");
}
}

Python3

# Python3 program to check a number
# is a Happy number or not
 
# Utility method to return
# sum of square of digit of n
def numSquareSum(n):
    squareSum = 0;
    while(n):
        squareSum += (n % 10) * (n % 10);
        n = int(n / 10);
    return squareSum;
 
# method return true if
# n is Happy number
def isHappynumber(n):
 
    # initialize slow
    # and fast by n
    slow = n;
    fast = n;
    while(True):
         
        # move slow number
        # by one iteration
        slow = numSquareSum(slow);
 
        # move fast number
        # by two iteration
        fast = numSquareSum(numSquareSum(fast));
        if(slow != fast):
            continue;
        else:
            break;
 
    # if both number meet at 1,
    # then return true
    return (slow == 1);
 
# Driver Code
n = 13;
if (isHappynumber(n)):
    print(n , "is a Happy number");
else:
    print(n , "is not a Happy number");
 
# This code is contributed by mits

C#

// C# program to check a number
// is a Happy number or not
using System;
class GFG {
 
// Utility method to return
// sum of square of digit of n
static int numSquareSum(int n)
{
    int squareSum = 0;
    while (n!= 0)
    {
        squareSum += (n % 10) *
                     (n % 10);
        n /= 10;
    }
    return squareSum;
}
 
// method return true if
// n is Happy number
static bool isHappynumber(int n)
{
    int slow, fast;
 
    // initialize slow and
    // fast by n
    slow = fast = n;
    do
    {
         
        // move slow number
        // by one iteration
        slow = numSquareSum(slow);
 
        // move fast number
        // by two iteration
        fast = numSquareSum(numSquareSum(fast));
 
    }
    while (slow != fast);
 
    // if both number meet at 1,
    // then return true
    return (slow == 1);
}
 
// Driver code
public static void Main()
{
    int n = 13;
    if (isHappynumber(n))
        Console.WriteLine(n +
        " is a Happy number");
    else
        Console.WriteLine(n +
        " is not a Happy number");
}
}
 
// This code is contributed by anuj_67.

的PHP


Java脚本


输出 :

13 is a Happy Number

无需额外空间即可解决此问题的另一种方法。
如果在任何步骤上获得的数字平方和为除1或7以外的一位数字,则数字不能为快乐数字。这是因为1和7是唯一的一位数字幸福数字。利用这些信息,我们可以开发一种方法,如下面的代码所示–

Java

//  This code is contributed by Vansh Sodhi.
//  Java program to check if a number is a Happy number or not.
 
class GFG {
 
     // method - returns true if the input is a happy
     //  number else returns false
     static boolean isHappynumber(int n) {
        if (n == 1 || n == 7)
            return true;
        int sum = n, x = n;
 
        // this loop executes till the sum of square of
        // digits obtained is not a single digit number
        while(sum > 9) {
            sum = 0;
 
            // this loop finds the sum of square of digits
            while (x > 0) {
                int d = x%10;
                sum += d*d;
                x/=10;
            }
            if (sum == 1)
                return true;
            x = sum;
        }
        if(sum == 7)
            return true;
        return false;
    }
 
    // Driver code
    public static void main(String[] args)
    {
       int n = 13;
       if (isHappynumber(n))
          System.out.println(n +
        " is a Happy number");
       else
          System.out.println(n +
        " is not a Happy number");
   }
}