(n >= 3 ) && (n <= 99)
OR
n `elem` [3..99]
Which one is faster and why?
The speed of light as it travels through air and space is much faster than that of sound; it travels at 300 million meters per second or 273,400 miles per hour. Visible light can also travel through other things besides through air and through space.
Light travels through empty space at 186,000 miles per second. The electricity which flows through the wires in your homes and appliances travels much slower: only about 1/100 th the speed of light.
No, sound travels much slower than the speed of light. The speed of sound in air is about 340 metres per second. Whereas, the light will travel through the vacuum at a speed of 300 million metres per second. The speed of light and sound varies in different mediums.
Light travels faster than sound even in water. If you are asking why sound is slower when it is in air than water, and why light is faster in air than in water, here is why: Light waves are electromagnetic transversal waves.
The first one is faster
(n >= 3) && (n <= 99)
it is doing 3 operations
n >= 3
n <= 99
and
Where as the elem is looking up the item in the array, so is doing upto (99 - 3) * 2 operations.
index = 0
isFound = false
array[] = { 3, 4, 5, 6, ... 98, 99 }
while isFound == false
isFound = (n == array[index++])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With