Skip to content

Commit ce62825

Browse files
authored
Created PrimeNumbers.c
// Question : Calculate number of prime factors of all numbers from 1 to nos
1 parent e5bf4b3 commit ce62825

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Math/PrimeNumbers.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include<stdio.h>
2+
int lp[1000000];
3+
int main()
4+
{
5+
// Question : Calculate number of prime factors of all numbers from 1 to nos
6+
// nos >=1 && nos<=100000
7+
// For example if nos = 5 , Ans = (1-> 0) + (2->1) + (3->1) + (4->2) + (5->1) = 5
8+
// Time Complexity = (O(nlogn))
9+
int nos;
10+
scanf("%d",&nos);
11+
for(int i=2;i<=nos;i++)
12+
{
13+
// lp array stores the lowest prime factor that divides nos
14+
// for example lp[4]=2 lp[49]=7
15+
if(lp[i]==0)
16+
{
17+
lp[i]=i;
18+
for(int j=2*i;j<=nos;j+=i)
19+
{
20+
if(lp[j]==0)
21+
lp[j]=i;
22+
}
23+
}
24+
}
25+
int ans=0;
26+
for(int i=1;i<=nos;i++)
27+
{
28+
int temp=i;
29+
while(lp[temp]!=0)
30+
{
31+
temp/=lp[temp];
32+
ans++;
33+
}
34+
}
35+
printf("%d\n",ans);
36+
}

0 commit comments

Comments
 (0)