Check if digit cube limit of an integer arrives at fixed point or a limit cycle | 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
let limit = 1000000000 ; function F ( N ) { let str = ( N ) . toString ( ) ; let sum = 0 ; for ( let i = 0 ; i < str . length ; i ++ ) { let val = ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; sum += val * val * val ; } return sum ; } function findDestination ( N ) { let s = new Set ( ) ; let prev = N , next = 0 ; s . add ( N ) ; while ( N <= limit ) { next = F ( N ) ; if ( s . has ( next ) ) { return next ; } prev = next ; s . add ( prev ) ; N = next ; } return next ; } function digitCubeLimit ( N ) { if ( N < 0 ) document . write ( " " ) ; else { let ans = findDestination ( N ) ; if ( ans > limit ) document . write ( " " ) ; else if ( ans == F ( ans ) ) { document . write ( N ) ; document . write ( " " + " " ) ; document . write ( ans ) ; } else { document . write ( N ) ; document . write ( " " + " " ) ; document . write ( ans ) ; } } } let N = 3 ; digitCubeLimit ( N ) ;