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
using System ; using System . Collections . Generic ; class GFG { static readonly int limit = 1000000000 ; static int F ( int N ) { 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 ; } static int findDestination ( int N ) { HashSet < int > s = new HashSet < int > ( ) ; int prev = N , next = 0 ; s . Add ( N ) ; while ( N <= limit ) { next = F ( N ) ; if ( s . Contains ( next ) ) { return next ; } prev = next ; s . Add ( prev ) ; N = next ; } return next ; } static void digitCubeLimit ( int N ) { if ( N < 0 ) Console . Write ( " N ▁ cannot ▁ be ▁ negative STRNEWLINE " ) ; else { int ans = findDestination ( N ) ; if ( ans > limit ) Console . Write ( " Limit ▁ exceeded STRNEWLINE " ) ; 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 ) ; } } } public static void Main ( String [ ] args ) { int N = 3 ; digitCubeLimit ( N ) ; } }