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 Limitis 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 = 3Output:Reached to fixed point 153Explanation:

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 = 4Output:Arrived at cycleExplanation:

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++

`// 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*

*filter_none*

**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.

## Recommended Posts:

- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Find a Fixed Point (Value equal to index) in a given array
- Count of N-digit numbers having digit XOR as single digit
- Rotation of a point about another point in C++
- Reflection of a point at 180 degree rotation of another point
- Check if frequency of each digit is less than the digit
- Find the minimum sum of distance to A and B from any integer point in a ring of size N
- Source to destination in 2-D path with fixed sized jumps
- Percentage increase in the volume of cuboid if length, breadth and height are increased by fixed percentages
- Check whether N can be a Perfect Cube after adding or subtracting K
- Check if number formed by joining two Numbers is Perfect Cube
- C Program to check whether a number is a Perfect Cube or not
- Minimum decrements to make integer A divisible by integer B
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Count of m digit integers that are divisible by an integer n
- Smallest integer with digit sum M and multiple of N
- Biggest integer which has maximum digit sum in range from 1 to n
- Smallest integer greater than n such that it consists of digit m exactly k times
- Maximize a number considering permutations with values smaller than limit
- Count 'd' digit positive integers with 0 as a digit

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.