java - Manual code computing -
studying exams have been given lot of code manually compute. question throwing me loop.
public static int[] mystery(int x) { int[] result = new int[x / 2]; (int = 0; < x/2; i++) { result[i] = (i * i) % x; } homecoming result; } for:
mystery(5) mystery(10) what computed [0, 1] mystery(5) , [0, 1, 4, 4, 1] mystery(10); however, believe answers not correct.
your reply mystery(5) correct. array mystery(10) should contain [0,1,4,9,6], since you're calculating square numbers mod 10 in case.
in sentiment there no strategy result faster. it's experience in reading code.
the steps took particular reply basically
look @ argument (x=10) look argument appears , insert it go through code step step look @ first line -> int division -> array has size 5 look @ loop, insert argument , work concrete numbers ->for(int = 0; < 5; i++) => i goes 0 4 look @ look , decide, values changing , ones fixed -> result[i] = (i*i)%10; think, loop => ith square number mod 10 goes array @ index i so result should [0,1,4,9,6] java arrays loops
No comments:
Post a Comment