Package com; public class test {public static void main (string [] ARGs) {system. out. println (getsteps1 (); system. out. println (">>>>>>>>>>>>>>>>>>>>>>>>>>>>"); system. out. println (test. getsteps (); system. out. println (">>>>>>>>>>>>>>>>>>>>>>>>>>>> ");} public static int getsteps () {// use the minimum public multiple to reduce the number of traversal times. Int I = 1; int step = 2; Boolean maxstep = false; while (true) {system. Out. Print ("" + I + "); If (! Maxstep) {if (I % 2 = 1) {step = 2;} else {I + = step; continue;} if (I % 3 = 2) {step = 6;} else {I + = step; continue;} if (I % 4 = 3) {step = 12;} else {I + = step; continue;} if (I % 5 = 4) {step = 60; maxstep = true;} else {I + = step; continue ;} if (I % 6 = 5) {step = 60; maxstep = true;} else {I + = step; continue;} if (I % 7 = 0) {return I;} else {I + = step; Continue ;}} else {// Add 60if (I % 7 = 0) {return I ;} else {I + = step;} // else {I + = step; continue;} adds a continue compiler error. If (I % 6 = 5) {} else {I + = step; continue;} if (I % 5 = 4) {} else {I + = step; continue;} if (I % 4 = 3) {} else {I + = step; continue;} if (I % 3 = 2) {} else {I + = step; continue;} if (I % 2 = 1) {} else {I + = step; Continue ;}}}} public static int getsteps1 () {int I = 1; while (true) {if (I % 2 = 1) {} else {I ++; continue ;} if (I % 3 = 2) {} else {I ++; continue;} if (I % 4 = 3) {} else {I ++; continue ;} if (I % 5 = 4) {} else {I ++; continue;} if (I % 6 = 5) {} else {I ++; continue ;} if (I % 7 = 0) {return I;} else {I ++; Continue ;}}}}
The running result is:
119>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 1 3 5 11 23 35 47 59 119 119>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
We can see that when I use the minimum public multiple method, we only need to cycle 8 times to find the number of 119. All the later versions will meet the question requirements.