To efficiently count the number of factors of a number n, instead of checking all numbers from 1 to n (which takes O(n) time), we can iterate only up to √n and count factor pairs (i, n/i). For each i that divides n, if i equals n/i (perfect square case), we add 1 to the count; otherwise, we add 2. This optimization reduces time complexity from O(n) to O(√n), making it feasible to handle very large numbers like 10^18 that would otherwise take centuries to process.
Approfondir
Prérequis
- Pas de données disponibles.
Prochaines étapes
- Pas de données disponibles.
Approfondir
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSAAjouté :
Hello everyone. Welcome to the intermediate module of data structure and algorithm. I'm generally excited to begin this journey with all of you. As we move from the absolute basic to advanced problem solving, my goal is to keep this course highly interactive, structured and impactful. To achieve that, we will follow a simple powerful three-step framework every week. So step one is daily shorts and question. So to keep your momentum strong, I will be posting a daily conceptual question or a short video on YouTube and our community platform. This will this will help you to stay consistent and continuously engaged. Step two is Sunday live session. Every Sunday we will go live together. In these session we will solve top interview level problems and I will personally address all the doubts and question from the comments. Step three is the golden rule consistency and practice. Teaching is my responsibility, but practice is yours. If you truly want to master DS and crack top companies, you must stay consistent and actively practice everything we cover. Now, to support your learning, all lecture notes will be shared right after each class on our WhatsApp channel. In addition, I will also share my personal WhatsApp number with you. So, if you ever feel stuck or need a guidance regarding placement, you can reach out to me directly. Remember don't isolate yourself, ask question, support each other and let's build a strong engineering community together. At the end of the day, consistency is what we separate a beginner from a uh top engineer. So without wasting a single second, let's jump straight to into our very first lecture. Introduce introduction to problem solving part one.
So in introduction to problem solving we are covering these seven topics.
Count of factors, optimization for count, check if number is prime or not, sum of n natural number, definition of AP and GP, number of iteration and how to compare two aloses.
So first we will start with what is factors. I think everyone know what is factor.
So if I is a factor of n if I divides n completely. So we can say if n modulo i is is equals to zero then i is a factor of n.
So now I will give you one uh one small quiz.
Okay. First let's check this quiz.
Number of factors of the number 24. If you know the answer, please uh reply in the comment section.
So I will mean I will also write here. So the factors of 24 are 1a 2a 3a 4a 6a 8a 12a and 24. So total numbers are 1 2 3 4 5 6 7 8.
So the answer is factor of 24 are 1 2 3 4 6 8 12 and 24.
Now I will give one more quiz.
Number of factors of the number 10. If you know the answer please uh update in the comment section.
Yeah. So factor of 10 are 1a 2a 5a 10.
Okay.
So okay we will start from here. So you know we count we calculate the factor of 24 and 10 right? So if you check properly sorry we can say there are two observation.
Okay observation one is all factor start with one. So if you see 24 start with one. 24 factors start with one basically and 10 factor is also start from one. Correct?
And second observation is last factor always a number itself. So if you see 24 the last factor is always a 24 only and same for 10 last number is 10 correct and if you want to know how we calculate the factors we just simply check one is one we can divide one 24 with one or not same 2 3 4 and all. So here for 24 the count is 8 and for 10 the count is four.
Correct? And we we have a two observation. First vector always one and second uh last vector always is n. n means the number given number correct.
So this is our two observation basically. So to solve this what we can do we we need to just check all the numbers from 1 to n if it is divisible completely divisible or not completely divisible or not correct.
So I will write a function called count factors. Simple function. Here you can see uh we initialize C to count the factors.
And we start the for loop from i = 1 to uh= to n till n. And if n modulo i is equ= to0 it means i is a factor of n.
And we did plus c. Correct? And we are increasing the count and we at the end we are returning the count.
Okay. But one more thing. So here you can see our code will run from 1 to n times.
Correct?
So we can say this code will take n iteration. Means if if the input is 24 then it will take 24 iteration. If the input is 10 or maybe if the input is 100 then it will take 100 iteration.
Correct?
Okay. Now we will do one assumption.
Okay. We will do the an assumption.
Assumption is uh like this code will run on a server and my computer will take 10 to the power 8 iteration iteration per second. Okay. It means 10 is to the power 8 iteration means 1 second. It means we can say 1 iteration is equals to 1 upon 10 to the power 8 second. Correct? And at the end we can say n iteration is equals to n upon 10 ^ 8 second. Correct.
This is our obser observation. Okay.
Now I will uh take a input. Okay. we will say n is equ= to 10 ^ 9. Okay. So with this formula we have n upon 10 to the^ 8 second correct. So we will update uh the value of uh n with 10 to the^ 9 and if we divide we got 10. So it means it will take 10 second. Correct? Now we got input as 10 to ^ 18. Okay. What we get?
10 to the^ 18.
Now for 10 to the^ 18 if we divide with 10 to the power 8 we got output as 10 to the^ 10 and it will take something around 317 years.
So tell me would you able to see the output of the code? Maybe you your children maybe third or fourth generation will see the output right? if it will take 317 years.
So the uh we can say there is a problem with this code. Correct? Now I think you understood right what I want to say why it is taking 317 years or something. If you have any query please add in the comment I will check and I will reply you.
Now we will go with the different approach.
Okay, this is some different approach.
And uh here what happened?
We can say if I into J is equals to N. For example, we have a factor 24. We have an input as 24. And if we want to check the factor 1, 1 into 24 is equals to 24. It means one as I.
Okay, I will tell you in this way. For example, we got input as 24 and 1 into 24.
This one and this one means I and J is equals to N. So I and J both are factor of N. Correct? Correct.
So we can say J is equals to N upon I and I comma N upon I are the factor of N correct.
So we we got some observation like I and N upon I are the factors of N correct.
So we can say if I is a factor of N then N upon I is also a factor of N. Correct?
So let's take a same uh input 24.
I make one table here.
So here you can see I and here you can see n upon i and here we have the count as zero. Correct?
So here what you can see first we take the factor like for example we have a factor 1 and if we divide n by i means 24 by 1 we got 24 i is less than n upon i correct 1 is less than 24 so both are factors right 1 is also a factor and 24 is also a factor so we will to C + 2. C + 2. Why we are doing? Because 1 and 24 both are factor. Correct? Same we will do for 2 and uh 2 and 12, right?
Because 2 and 12 also a factor. Same 3 and 8 because 3 is if uh we divide 24 by 3, we got 8. So 3 and 8 both are the factors and 3 is less than 8. C is equals to 2. We will do correct. And same here four and six. Okay. So this one done, this one done and this one these two are also done. So now next input is 4 and six. Both are factors. So we can do C + 2 something correct.
Now we will make a bridge here. If again we will check sixth 6 and four like again if uh we got a factor six and we will divide 24 by 6 we got four. So we already count six and four right. So it it will be duplicate if we count again.
So here we got a condition. What condition? We got if I is less than n upon i like we need to calculate calculate uh we need to count till uh i is less than n upon i. Correct?
I is less than n upon i. Correct?
Okay. And uh these all values we we uh no need to count because it's we got a greater sign. So maybe you can say we will start a loop and we will add a condition uh to add like uh I is less than n upon i and we will count the we will add c plus something. Right. Now we will take one more example here before going uh to the coding.
Yeah. So the next is n is equ= to 100. Okay. So here here you can say one second please.
We got the input as n is = to 100 and here i is = to 1. when I is 1, n upon i is 100. So here i is equals to i is less than 100.
Right?
I is less than 100. One second I will remove this is equals to otherwise you may be confused.
Okay. So here same we will come to two.
We will calculate n upon i. We got 50 something blah blah blah. And same for we got uh same we got for 4 and 25 and five for 20. Now next is 10. So if we divide 100 by 10 we got 10 right. So then we need to add here is equals to and one more thing we need to add here we need to add c + 1 because both side i and n divide uh n upon i both are same.
So we will make one more observation. We will have one more observation. We need to add less than and is equals to.
Correct. 1 is less than 100. 1 is less than is equals to 100. So we will we can say in short we will create a loop and we will add a condition if I is less than is equals to n upon i then we will make a count. And second thing we will add if I is equals to n upon i then we will add just c + 1 otherwise c + 2.
Got it? Now we will have one more one more thing we will have here. So we will iterate till i is less than n upon i. Then we have i into i less than equals to i under root.
I is less than under root. Then we have here like we can say we will iterate rate till 1 2 3 4 to under root of n correct. So now we can say last time we calculate right 10 is to the power 18.
So now if you calculate under root 10 to the power iteration we got output as 10 to the^ 9 iteration correct. If 10 is to the power 8 iteration is 1 second then 10 to the^ 9 iteration is 10 second. So earlier we are solving that question or maybe problem in 300 or some 317 years and now we are able to solve it in 10 second.
Got it? So this is the code for that. So here you can see we have int count factor.
So here we will create a integer like variable count. we will add the count of vectors and we will uh maybe uh run a for loop from 1 to under root of n. Here you can also write in this way. Okay, this is correct. But we can also write I into I is less than is equals to n. Okay, this is little bit simple and you maybe remember and after that we will add a condition if n% n mod i is equals to zero then it means i is a factor of n and n upon i. So you remember we we decided if i= to= to n upon i. If both i and n upon i is same we have a example 10 one right when we divide 100 by 10. So we will count we will increase the count with one only and otherwise we will count with two. So now at the end we will return the count correct. So this is for the count of factors. Now second thing is uh prime numbers but before this if you have any query please post in the comment section. I will quickly check and reply you maybe you can also use our WhatsApp community channel. You can post there also.
So next is prime number. Okay, I think everyone know what is prime number. We can say number divisible by one itself. One and itself is prime number and has exactly two factors is a prime number. Correct? I think these two definition we always uh saw maybe maybe we will saw in first second class or maybe from the beginning we always see these two things. Okay.
But now one thing for example one one is a prime number right? If user pass n is equals to 1 which is a prime number.
Okay. So if user pass here n is equals to 1 which is not a prime number correct but it will pass this condition number divisible by one and itself.
So the first condition is wrong.
Correct? So we can say we have only one condition has exactly two factors.
So if we have only one condition has exactly two factors. Recently we completed one thing the factor one we just need to calculate the factor. If factor is equals to two then it means it's a prime number otherwise it is not a prime number. Before that we will give you uh one quiz.
Please answer it in the comment section if you know the answer. How many prime numbers are there? 10 11 23 27 and 31.
Okay. So I'll tell you if you see 10 we have a uh we can say we have a factors of 10 like 1 2 5 and 10. Four factors we have.
So this is not a prime number. 11. 11 we have a factor as 1 and 11. So we have two factors. This is a prime number. Now second uh the next one is 23. Same for 23 we have two factors 1 and 23. So we have can say this is a prime number.
Same for two. 1. So factor of two is 1 and two. So this is also a prime number.
Now 25. factor of 25 is maybe more than two right we can say 1 then five then it's a 25 also so it is not a prime number now next is 27 27 we can say 1 then 3 then 9 then 27 also so this is also not a prime number and the last is 31 31 factors are 1 and 31 so this is a prime number so we can say four is the correct answer correct And these all the 1123 2 and 31 is a prime number.
So now only we discuss how we can find the prime number. We will just uh add one condition to check the count factors with the given value n. If the count factor is two, if the count factor is two, it means it is a prime number. If the count factor is maybe greater than two or maybe less than two, it is not a prime symbol.
So that's all we are covering as part of this class. Maybe we will cover iteration and uh uh we can say geometric uh GP AP we will cover in the next video.
Okay. Thank you so much. Please share your feedback.
Vidéos Similaires
Re: 🗣️📍theprophedu📍2026 GST 103 CLASS (E-EXAM REVISION)
theprophedu
636 views•2026-06-04
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
Making Minecraft Clone with C++ & Raylib
PecaCSLive
686 views•2026-06-04
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Instagram accounts got PWNed
EricParker
13K views•2026-06-03
So What's Odin Lang Even Good For
TechOverTea
131 views•2026-06-01











