Ad
  • Custom User Avatar

    i does not need to iterate over all the whole numbers. 2 is the only even whole number that can be a prime. It's nearly twice as fast to first check if the input is divisible by 2 and is not 2, and then starting i off at 3 and iterating with i += 2.

    Also, the loop does not need to go until num/2. Once you get past the square root, there's no point in checking any longer, it has to be prime.

    Example runtimes with a large odd number input:
    iterating over all integers up to num/2: 0.007080078125ms
    interating over odd numbers up to Math.sqrt(num): 0.002685546875ms

  • Default User Avatar

    I just saw that, do you mean it does not pass this example test from the description because I added it Test.assertEquals(findEvenIndex([20,10,-80,10,10,15,35]),0, "The array was: [20,10,-80,10,10,15,35] \n");
    and that test fails with this solution?

    ah just saw other people said the same.

  • Custom User Avatar