Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite loop detection

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

I got a school task(which considers our programming knowledge is on basic level) where we should take an input (int) from user and then take the sum of squares of the digits of that number (i.e. 123-->1^2+2^2+3^2). Then that result should be put in a while loop and it should repeat the same thing 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 number 1 we should print "number is lucky!" and if it isn't it will be stuck in an infinite loop and we should print "number is not lucky!".

Now whats bugging me is how do they expect us to print "number is not lucky" if it will be stuck in an infinite loop ?

Is it possibly the badly written and designed task/question or there actually is a basic-level-knowledge way to detect and stop an infinite loop?


Here is my code(w/o the infinite loop detection):

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;
    }
}  
}

like image 922
SadCoffeeBean Avatar asked Mar 06 '15 13:03

SadCoffeeBean


People also ask

Is it possible to detect an infinite loop?

While most infinite loops can be found by close inspection of the code, there is no general method to determine whether a given program will ever halt or will run forever; this is the undecidability of the halting problem.

What is an infinite loop explain?

An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a functional exit so that it repeats indefinitely. In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.

What is an infinite loop and how do you avoid it?

An infinite loop is a piece of code that keeps running forever as the terminating condition is never reached. An infinite loop can crash your program or browser and freeze your computer. To avoid such incidents it is important to be aware of infinite loops so that we can avoid them.

What is an example of an infinite loop?

An infinite loop occurs when a condition always evaluates to true. Usually, this is an error. For example, you might have a loop that decrements until it reaches 0.


1 Answers

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

However, in your specific case I can think of a way to detect an infinite loop. In each iteration you calculate a number (the sum of the squares of the digits or the previous number). You can put all those numbers in a Set. If in some iteration you calculate a number that is already in that Set, you know that you are stuck in an infinite loop.

Since the largest sum of squares is bound (for a number with n digits, the largest sum of squares of the digits is 81*n), there is a relatively small number of distinct values you are going to get in your iterations, so if you don't reach 1 and end with a success, you'll reach a value that's already appeared before and report a failure.

like image 95
Eran Avatar answered Nov 06 '22 14:11

Eran