Waiter

  • + 0 comments
    1. First understand what are factors, e.g., 4 and 6 are factors of 24.
    2. Secondly, understand that prime numbers only have 2 factors : 1 and itself. And 24 has 1,2,3,4,6,8,12,24 as factors. Hence it's not a prime number.
    3. Thirdly, understand that to identify whether a number is a prime number from a programming stand point, we need to loop from 1 to 24 to see how many factors we are getting. This way is called brute force, but it takes a very long time.
    4. If we take a closer look, we notice a pattern, 2 * 12 is 24, 3 * 8 is 24, 4 * 6 is 24, and vice versa, 6 * 4 is 24, 8 * 3 is 24, 12 * 2 is 24. And square root of 24 is between these two sets of factors. 1, 2, 3, 4, 4.8989, 6, 8, 12, 24
    5. So instead of looping all the way to 24, we can just loop up to square root of 24