Can someone explain to me what this program does?

Asked

Viewed 66 times

-4

public class Break {

public static void main(String[] args) {

    long i = System.currentTimeMillis();

    for(int count=1 ; count <=1000000 ; count++){
        if((count % 17 == 0) && (count % 19 == 0)){
            System.out.println(count);
   
            break;
        }
    }

    System.out.println("Tempo de execução, em milisegundos: "+ (System.currentTimeMillis() -i));
 
   }

}

3 answers

2

the % operator returns the rest of the division of its arguments, if the rest of the Count division by 17 and 19 is zero, then it is divisible by the two. I believe this is the functionality of the program

1

It prints on the screen the total run time in milliseconds needed to find a number between 1 and 1000 that is divisible by 17 and 19 ... As both are cousins and easy to notice that the number will be 17*19! The - ${i} notation refers to variable i - when the program enters the specified If block, that is when it is divisible by 17 and 19!

0

The program prints a number and the time used by the processor to find that number, between 1 and 1,000,000, which returns an integer in the division by 17 and 19 at the same time.

It is curious because by dividing this number by 17, it results in 19; by dividing this same number by 19, it results in 17.

for(let i = 1; i < 1000000; i++) {
  if ((i % 17 == 0) && (i % 19 == 0)) {
    console.log(`${i} é divisível por 17 e 19 ao mesmo tempo`);
    console.log(`${i} / 17 = ${i/17}`);
    console.log(`${i} / 19 = ${i/19}`);
    break;
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.