Check if digit cube limit of an integer arrives at fixed point or a limit cycle | Java 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
import java . util . * ; class GFG { static final int limit = 1000000000 ; static int F ( int N ) { 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 ; } static int findDestination ( int N ) { HashSet < Integer > s = new HashSet < > ( ) ; 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 ) System . out . print ( "N cannot be negativeNEW_LINE"); else { int ans = findDestination ( N ) ; if ( ans > limit ) System . out . print ( "Limit exceededNEW_LINE"); 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 ) ; } } } public static void main ( String [ ] args ) { int N = 3 ; digitCubeLimit ( N ) ; } }