0

*hi there here is my answer for this challenge :

Write an algorithm to determine if a number n is "happy".

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

my code works and outputs a true value only if the number is a happy one*


    public class Main {
    
        public static void main(String[] args) {
    
                    System.out.println(isHappy(12));
    
                    }
    
        public static boolean isHappy(int number) {
            
                while (number != 1 ) {
                    number = SumOfintegers(number); //to keep looping until It find sum=1
                 }
                 return true ; 
         }
    
        public static int SumOfintegers(int number){
               
                    int sum =0;
                    int news = 0;
                    while (number > 0) {
                        int num = number % 10;
                        number /= 10;
                        news = num * num;
                        sum = sum + news;
                    }
                    return sum;
               }
    }
  • please explain your problem more clearly. Also, this is not a place for discussing coding challenges :) – tsamridh86 Nov 14 '20 at 04:57
  • Have a look at Project Euler problem 92 for help. There are many websites giving advice on those problems. – rossum Nov 14 '20 at 09:03

1 Answers1

0

so I have solved this "happy number" problem once and this is my code using recursion with explanation:

public static void main(String[] args) {
    System.out.println(nextHappyNum(0));
}
//method to find next happy number
static int nextHappyNum(int num){
    int x =num+1;
    if(isHappy(x) == true){ //base case where the next number is happy number
        return x;
    } else{
        return nextHappyNum(num+1); //recursively call the method to check
    }
}

//method to check if number is happy or not
static boolean isHappy(int num){
    while(num != 1 && num != 4){
        num = calculateHappy(num); //this loop will call the checkHappy() method until it reach 1 or 4
    }
    if(num == 1){ //if result is 1 -> happy number
        return true;
    } else if(num == 4 ){ //final result is 4 -> not happy number
        return false;
    } else{
        return false;
    }
}

//method to calculate the sum of number digits
static int calculateHappy(int num){
    int sum = 0;
    int rem = 0;
    //create while loop to calculate the digit number, remainder of modulus 10 is the second digit
    //first digit is = the number/10 and the remainder of that int 
    while(num != 0){ 
        rem = num%10; //calculating the remainder 
        sum += (rem*rem); //calculate the square root of remainder and add it to the sum
        num = num/10; //divide the number by 10 - due to this data type is int, so the first digit will be taken
    }
    return sum;
}
Peter
  • 17
  • 3