Happy Number TIme Limitation issue


  • -1
    D
    This post is deleted!

  • 0
    F

    the error means the program runs forever unless stopped.
    the code above should have this behavior for n a not happy number, for example 4, where it will iterate endlessly in the

    while(sum!=1){  /// for n = 4
        sum = 0; 
          while(n>=10){ 
            r = n%10; 
            sum=sum + (r^2);
            n = n/10;
         }
         
    Sum+= n^2; // here sum == 16 and n =0 after the 1st iteration
               // after the second iteration sum = 0 and n = 0 and the while will never finish
    }

  • 0

    "HOw can i make better?"

    For starters, you could format it so it becomes readable.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.