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