-1

My assignment is to print all the happy numbers from 10 until the program prints 3 consecutive numbers. Those 3 numbers should be 1880 1881 and 1882. i wrote while(num2<1883) just for the loop to stop. i should compare the 3 last "sums" and stop the loop when they are consecutive.

do {
        num = num2;
        while (num > 0 || sum > 9) {
            if (num == 0) {
                num = sum;
                sum = 0;
            }
            sum += Math.pow(num % 10, 2);
            num /= 10;
        }
        if (sum == 1) {
            counter++;
            System.out.println(counter + ") " + num2 + " is a happy number :-)");
        }
        num2++;
        sum = 0;
    } while (num2 < 1883);//<---?????
idopinhas
  • 1
  • 2
  • Possible duplicate of [How do I exit a while loop in Java?](https://stackoverflow.com/questions/7951690/how-do-i-exit-a-while-loop-in-java) – Tim Apr 06 '18 at 14:27
  • Just as an aside: Are you sure you are supposed to check for `num2 < 1883` explicitly? The assignment reads as if you might be required to actually compare the numbers you output. – Cecilya Apr 06 '18 at 14:30
  • I don't think you do – Tim Apr 06 '18 at 14:30
  • Doesn't your loop currently stop when `num2` reaches 1883? In reality though, I would think you would have a counter of the most recent steak of consecutive happy numbers and exit when that count reaches 3. It's currently written assuming the answer. – lurker Apr 06 '18 at 14:31
  • i mean i know how to exit from loops. but in this assiment i need to exit when the 3 last "sums" were consecutive numbers. and i dont know how can i compare the last "sum" to the 2 "sums" before – idopinhas Apr 06 '18 at 14:35

2 Answers2

1

You should have a List that contains consecutive found numbers. And loop until the length of this list is 3. Inside the loop check if your candidate is an happy number. If it is add it to the list. If it isn't clear the list since you haven't reached a consecutive happy number. After exiting the loop the List contains the three consecutive happy numbers found.

List<Integer> foundNumbers = new ArrayList<Integer>();
int candidate = 10;
while(foundNumbers.length < 3) {
    if(isHappyNumber(candidate)) {
        foundNumbers.add(candidate);
        System.out.println(candidate + " is a happy number");
    } else {
        foundNumbers.clear();
    }
    candidate ++;
}
//Print the list members
vincrichaud
  • 2,218
  • 17
  • 34
  • 1
    "*`List foundNumbers = new ArrayList();`*" - Are you using some experimental JDK? Generic primitives are only supported through [project valhalla](http://openjdk.java.net/projects/valhalla/). – Turing85 Apr 06 '18 at 14:40
1

The general format is:

int lastHappyNumber = 0;
int currentHappyNumber = 0;
int thirdLastSum = 0;
int secondLastSum = 0;
int firstLastSum = 0;

do {
    currentHappyNumber = getNextHappyNumber();     // <--- fill in with your logic
    thirdLastSum = secondLastSum;
    secondLastSum = firstLastSum;
    firstLastSum = lastHappyNumber + currentHappyNumber;
    lastHappyNumber = currentHappyNumber;
} 
while ((thirdLastSum + 1) == secondLastSum && (secondLastSum + 1) == firstLastSum);

Since this is an assignment, I will leave computing the happy numbers to you, but the idea is that every iteration, you shift down the last 3 sums and then compare them. Once you find a valid solution (where the last three sums were consecutive integers), the value of lastHappyNumber holds the happy number that solves the problem. lastHappyNumber = currentHappyNumber ensures that during the next iteration, the last happy number will be stored and allow for a sum computation to be performed.

Justin Albano
  • 3,809
  • 2
  • 24
  • 51