📜  检查整数的数字立方体极限是否达到固定点或极限周期

📅  最后修改于: 2021-04-22 07:21:24             🧑  作者: Mango

给定整数N ,任务是检查整数的Digit Cube极限是否到达固定点或极限循环。

例子:

方法:请按照以下步骤解决问题:

  • 创建一个hashMap来存储迭代过程中数字的数字立方的总和。
  • 迭代一个数字的位的总和的下一个值,并检查hashMap中是否存在下一个的数字的总和。
    • 如果该数字已经在哈希图中,则检查该数字是否为阿姆斯特朗数字。如果发现为真,则数字达到固定点。
    • 否则,如果该号码不是Armstrong号码,则继续。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#include 
 
// Define the limit
#define limit 1000000000
 
using namespace std;
 
// Function to get the sum of cube
// of digits of a number
long long F(long long N)
{
    // Convert to string to get sum
    // of the cubes of its digits
    string str = to_string(N);
    long long sum = 0;
 
    for (long long i = 0;
         i < str.size(); i++) {
        long long val
            = int(str[i] - '0');
        sum += val * val * val;
    }
 
    // return sum
    return sum;
}
 
// Function to check if the number
// arrives at a fixed point or a cycle
long long findDestination(long long N)
{
    // Stores the values obtained
    set s;
 
    long long prev = N, next;
 
    // Insert N to set s
    s.insert(N);
 
    while (N <= limit) {
 
        // Get the next number using F(N)
        next = F(N);
 
        // Check if the next number is
        // repeated or not
        auto it = s.find(next);
        if (it != s.end()) {
            return next;
        }
 
        prev = next;
        s.insert(prev);
        N = next;
    }
    return next;
}
 
// Function to check if digit cube
// limit of an integer arrives at
// fixed point or in a limit cycle
void digitCubeLimit(long long N)
{
 
    // N is a non negative integer
    if (N < 0)
        cout << "N cannot be negative\n";
 
    else {
 
        // Function Call
        long long ans
            = findDestination(N);
 
        // If the value received is
        // greater than limit
        if (ans > limit)
            cout << "Limit exceeded\n";
 
        // If the value received is
        // an Armstrong number
        else if (ans == F(ans)) {
            cout << N;
            cout << " reaches to a"
                 << " fixed point: ";
            cout << ans;
        }
 
        else {
            cout << N;
            cout << " reaches to a"
                 << " limit cycle: ";
            cout << ans;
        }
    }
}
 
// Driver Code
int main()
{
    long long N = 3;
 
    // Function Call
    digitCubeLimit(N);
 
    return 0;
}


Java
// Java program for the
// above approach
import java.util.*;
class GFG{
     
// Define the limit
static final int limit = 1000000000;
   
// Function to get the sum of cube
// of digits of a number
static int F(int N)
{
  // Convert to String to get sum
  // of the cubes of its digits
  String str = String.valueOf(N);
  int sum = 0;
 
  for (int i = 0;
           i < str.length(); i++)
  {
    int val = (int)(str.charAt(i) - '0');
    sum += val * val * val;
  }
 
  // return sum
  return sum;
}
 
// Function to check if the number
// arrives at a fixed point or a cycle
static int findDestination(int N)
{
  // Stores the values obtained
  HashSet s = new HashSet<>();
 
  int prev = N, next =0;
 
  // Insert N to set s
  s.add(N);
 
  while (N <= limit)
  {
    // Get the next number
    // using F(N)
    next = F(N);
 
    // Check if the next number is
    // repeated or not
    if (s.contains(next))
    {
      return next;
    }
 
    prev = next;
    s.add(prev);
    N = next;
  }
  return next;
}
 
// Function to check if digit cube
// limit of an integer arrives at
// fixed point or in a limit cycle
static void digitCubeLimit(int N)
{
  // N is a non negative integer
  if (N < 0)
    System.out.print("N cannot be negative\n");
  else
  {
    // Function Call
    int ans = findDestination(N);
 
    // If the value received is
    // greater than limit
    if (ans > limit)
      System.out.print("Limit exceeded\n");
 
    // If the value received is
    // an Armstrong number
    else if (ans == F(ans))
    {
      System.out.print(N);
      System.out.print(" reaches to a" +
                       " fixed point: ");
      System.out.print(ans);
    }
 
    else
    {
      System.out.print(N);
      System.out.print(" reaches to a" +
                       " limit cycle: ");
      System.out.print(ans);
    }
  }
}
 
// Driver Code
public static void main(String[] args)
{
  int N = 3;
 
  // Function Call
  digitCubeLimit(N);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
 
# Define the limit
LIMIT = 1000000000
 
# Function to get the sum of cube
# of digits of a number
def F(N: int) -> int:
     
    # Convert to string to get sum
    # of the cubes of its digits
    string = str(N)
    sum = 0
     
    for i in range(len(string)):
        val = int(ord(string[i]) - ord('0'))
        sum += val * val * val
 
    # Return sum
    return sum
 
# Function to check if the number
# arrives at a fixed point or a cycle
def findDestination(N: int) -> int:
     
    # Stores the values obtained
    s = set()
 
    prev = N
    next = 0
 
    # Insert N to set s
    s.add(N)
 
    while (N <= LIMIT):
 
        # Get the next number using F(N)
        next = F(N)
 
        # Check if the next number is
        # repeated or not
        if next in s:
            return next
 
        prev = next
        s.add(prev)
        N = next
 
    return next
 
# Function to check if digit cube
# limit of an integer arrives at
# fixed point or in a limit cycle
def digitCubeLimit(N: int) -> int:
     
    # N is a non negative integer
    if (N < 0):
        print("N cannot be negative")
 
    else:
 
        # Function Call
        ans = findDestination(N)
 
        # If the value received is
        # greater than limit
        if (ans > LIMIT):
            print("Limit exceeded")
 
        # If the value received is
        # an Armstrong number
        elif (ans == F(ans)):
            print("{} reaches to a fixed point: {}".format(
                N, ans))
        else:
            print("{} reaches to a limit cycle: {}".format(
                N, ans))
 
# Driver Code
if __name__ == "__main__":
     
    N = 3
 
    # Function Call
    digitCubeLimit(N)
 
# This code is contributed by sanjeev2552


C#
// C# program for the
// above approach
using System;
using System.Collections.Generic;
class GFG{
     
// Define the limit
static readonly int limit =
       1000000000;
   
// Function to get the sum
// of cube of digits of a
// number
static int F(int N)
{
  // Convert to String to get sum
  // of the cubes of its digits
  String str = String.Join("", N);
  int sum = 0;
 
  for (int i = 0;
           i < str.Length; i++)
  {
    int val = (int)(str[i] - '0');
    sum += val * val * val;
  }
 
  // return sum
  return sum;
}
 
// Function to check if the
// number arrives at a fixed
// point or a cycle
static int findDestination(int N)
{
  // Stores the values
  // obtained
  HashSet s =
          new HashSet();
 
  int prev = N, next = 0;
 
  // Insert N to set s
  s.Add(N);
 
  while (N <= limit)
  {
    // Get the next number
    // using F(N)
    next = F(N);
 
    // Check if the next
    // number is repeated
    // or not
    if (s.Contains(next))
    {
      return next;
    }
 
    prev = next;
    s.Add(prev);
    N = next;
  }
  return next;
}
 
// Function to check if digit cube
// limit of an integer arrives at
// fixed point or in a limit cycle
static void digitCubeLimit(int N)
{
  // N is a non negative integer
  if (N < 0)
    Console.Write("N cannot be negative\n");
  else
  {
    // Function Call
    int ans = findDestination(N);
 
    // If the value received is
    // greater than limit
    if (ans > limit)
      Console.Write("Limit exceeded\n");
 
    // If the value received is
    // an Armstrong number
    else if (ans == F(ans))
    {
      Console.Write(N);
      Console.Write(" reaches to a" +
                    " fixed point: ");
      Console.Write(ans);
    }
 
    else
    {
      Console.Write(N);
      Console.Write(" reaches to a" +
                    " limit cycle: ");
      Console.Write(ans);
    }
  }
}
 
// Driver Code
public static void Main(String[] args)
{
  int N = 3;
 
  // Function Call
  digitCubeLimit(N);
}
}
 
// This code is contributed by gauravrajput1


输出:
3 reaches to a fixed point: 153









时间复杂度: O(N)
辅助空间: O(N)