Check if digit cube limit of an integer arrives at fixed point or a limit cycle

Given an integer N, the task is to check if the Digit Cube limit of an integer arrives at a fixed point or in a limit cycle.

A Digit Cube Limit is a number which repeatedly arrives at a point if its value is computed as the sum of cubes of its digits, i.e. they have the following properties:

  • Arrive at a fixed point if it is an Armstrong number.
  • Arrive at a limit cycle if it is repeating in a cycle.

Examples:

Input: N = 3
Output: Reached to fixed point 153
Explanation:
F(3) = 3 * 3 * 3 = 27
F(27) = 2*2*2 + 7*7*7 = 351
F(351) = 3*3*3 + 5*5*5 + 1*1*1 = 153
F(153) = 1*1*1 + 5*5*5 + 3*3*3 = 153
Since a fixed point(= 153) was obtained, which is an Armstrong number of order 3. Below is the illustration:

Input: N = 4
Output: Arrived at cycle
Explanation:
F(4) = 4 * 4 * 4 = 64
F(64) = 6 * 6 * 6 + 4 * 4 * 4 = 280
F(280) = 2 * 2 * 2 + 8 * 8 * 8 + 0 * 0 * 0 = 520
F(520) = 5 * 5 * 5 + 2 * 2 * 2 + 0*0*0 = 133
F(133) = 1*1*1 + 3*3*3 + 3*3*3 = 55
F(55) = 5*5*5 + 5*5*5 = 250
F(250) = 5*5*5 + 2*2*2 + 0*0*0 = 133
A cycle between
133 -> 55 -> 250 -> 133 is obtained.
Below is the illustration of the same:



Approach: Follow the steps below to solve the problem:

  • Create a hashMap to store the sum of the cube of digits of the number while iteration.
  • Iterate for the next values for the sum of cube of digits of a number and Check if the next sum of the cube of digits is already present in the hashMap or not.
    • If the number is already in the hash-map then, check if the number is an Armstrong number or not. If found to be true, then the number reaches a fixed point.
    • Otherwise, If the number is not Armstrong number then continue.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <algorithm>
#include <bits/stdc++.h>
  
// 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<long long> 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;
}

chevron_right


Output:

3 reaches to a fixed point: 153

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.