Endless loop detection

I wonder if it is possible to detect and / or stop an infinite loop with a basic knowledge of java.

I have a school task (which considers that our programming knowledge is at a basic level), where we need to take input (int) from the user, and then take the sum of the squares of the digits of this number (i.e. 123 → 1 ^ 2 + 2 ^ 2 + 3 ^ 2). Then this result should be put in the while loop, and it should repeat the same until it reaches 1 (i.e. 123 → 1 ^ 2 + 2 ^ 2 + 3 ^ 2 = 14 → 1 ^ 2 + 4 ^ 2 = 17 → 1 ^ 2 + 7 ^ 2 etc.)

If we get the number 1, we must print "the number is lucky!" and if it’s not, it will get stuck in an endless loop, and we should print “number is out of luck!”.

Now, what about how we expect us to print “no luck” if it gets stuck in an infinite loop?

Perhaps this is a poorly written and designed task / question, or is there a way of a basic level of knowledge to detect and stop an infinite loop?


Here is my code (without detecting an infinite loop):

import java.util.Scanner;

public class Vezba {

public static void main(String[] args) {
    boolean run = true;
    Scanner sc = new Scanner(System.in);
    int number = sc.nextInt();
    int digits;
    int sum=0;
    int k = 10;
    int j = 1;
    while(run){
        if(number<0){
            run=false;
        }
        int len = String.valueOf(number).length();
       /* Takes out each digit to make the first sum (probably redundant but please ignore)*/
        while(len>0){
            digits = number%k/j;
            j*=10;
            k*=10;
            len--;
            sum += digits*digits;
        }
       /* Repeats the process until sum is 1*/
        while(sum>1){
            int len2 = String.valueOf(sum).length();
            int pom = 1;
            int k1=10;
            while(len2>0){
                digits = sum%k1/pom;
                pom*=10;
                k1*=10;
                len2--;
                sum += digits*digits;
            }
        }
        System.out.println("Number is lucky!");
        run=false;
    }
}  
}

+4
source share
5 answers

In general, there is no solution to the Halting problem.

. ( ). Set. , , , .

( n 81 * n), , , , 1 , , , .

+2

, . , , , , , .

Set<Integer> previous = new HashSet<>();

// in you loop
int sum, number;
do {
   sum = 0;
   int len = String.valueOf(number).length();
   /* Takes out each digit to make the first sum (probably redundant but please ignore)*/
    while(len>0){
        digits = number%k/j;
        j*=10;
        k*=10;
        len--;
        sum += digits*digits;
    }
} while (sum > 1 && previous.add(sum))
if (sum > 1) // no lucky.

previous.add(sum) false .

+2

- -

0

- ? n- , 81 * n - 999999.. 81n < 100. 10 ^ n, n > 2, n- 10 ^ n. , , .

- . . , , .

A more efficient algorithm in time and space may be to save the value found at step m / 2 in the chain and compare it with that at step m. You can think of it as how to pull an elongated piece of string behind you when you explore the solution space. In the end (if it's a loop) you will meet the end of your line.

0
source

Perhaps you can use something like this:

n=in.nextInt ();
int t=n;
while (n!=0)
{
r=n% 10;
s=s+Math.pow (r, 2);
n/=10;
}
if (s==t)
Sopln (whatever is given);
else
Sopln (whatever is given);
0
source

All Articles