Simple demonstration of Time complexity

Photo by Jan Huber on Unsplash

Simple demonstration of Time complexity

Factors of a number

Β·

2 min read

Time Complexity!!!

Hey, I am Ishidaa, a computer science undergraduate.

We, the computer science people must learn fundamentals and apply in every possible new way. Everyone can grasp an idea, fundamentals, sketch, if made easy. So, I made an Simple demonstration on Time complexity.

So..... what is time complexity? Let's put it simple,

Time complexity - Time taken by a program to process a given input.

With this in mind let's see two comparisons.

number = 978456912;

Snippet 1:

for (i = 2; i < number; i++)
        {
            if (number%i==0)
            {
                System.out.println(i);
                count++;
            }
        }

Snippet 2:

for (i = 2; i <= number/2; i++)
        {
            if (number%i==0)
            {
                System.out.println(i);
                count++;
            }
        }

Could you figure the differences between the snippets above? Yeah, the condition part in loop.

But lemme tell this before you all conclude with the differences, those two provide the same output for the same input.

But the time taken to provide those outputs differ.

Look at the output below:

Snippet 1:

image.png

Snippet 2:

image.png

The time differences between those two execution is around 6.4s but the same results.

Logic to find factors of number is simple...

Highest factor for a number is always less than number/2(excluding the number).

Coming back to Time complexity,

the sample input we gave is very small compared to real life input data.

Little context over what I said,

nine hundred seventy-eight million four hundred fifty-six thousand nine hundred twelve

This is our input integer we gave which is almost equal to 1 billion. If we consider the tech giants, their user counts are greater than 1 billion and those will store and retrieve data quickly.

Eg., when you create a google account the first step is to enter your desirable Gmail username and when you type it either it's gonna show "Username taken" or "Username available". That's the two outputs we see in most cases.

image.png Credits: Google

If you think, google has more than 2 billion user accounts and to let you know your username availability within seconds is amazing. The querying, data structures and time complexity plays a critical role in that process.

If the time taken to know your username availability was longer, we all might lose patience.

What you must remember:

  • Always make efficient solutions.

  • Do not use unwanted data.

Take a look at my Github repoπŸ‘‡πŸ‘‡

Source code

If you're all the way down here, thanks for your time.

Lemme know what I should create next.✌✌

P.S: I haven't used any data structures in this cause I wanna keep this Simple.

Β