Skip to content
14 changes: 14 additions & 0 deletions Week1/Swarnima_Shishodia/Que1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include<iostream>
using namespace std;

int main()
{
long long int n;

cout<<"Enter a number ";
//Input a number
cin>>n;
//Output the square of a number
cout<<n*n;
return 0;
}
20 changes: 20 additions & 0 deletions Week1/Swarnima_Shishodia/Que10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// To calculate gcd of a number
#include<iostream>
using namespace std;

int gcd(int num1,int num2)
{
if(num2==0)
return num1;
else
return gcd(num2,num1%num2);
}
int main()
{
int num1,num2,result;
cin>>num1;
cin>>num2;
result=gcd(num1,num2);
cout<<result<<endl;
return 0;
}
36 changes: 36 additions & 0 deletions Week1/Swarnima_Shishodia/Que11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include <iostream>
using namespace std;

void sieve_of_erasthones(int a[])
{
int p=10000,i,q=2,j;
for(i=0;i<=p;i++)//Mark all the array position as 1
a[i]=1;
a[0]=0;
a[1]=0;
while(q*q<=p) //Mark the sieve positions as 0 which gets divided by q
{
for(j=q*q;j<=p;j=j+q)
a[j]=0;
q=q+1;
}
//The numbers which are prime, 1 will be present
}
int main() {
int t,i,n,j;
cin>>t;
int a[10000];
sieve_of_erasthones(a); //Call the sieve function
for(i=0;i<t;i++)
{
cin>>n;
for(j=0;j<=n;j++)
{
if(a[j]==1)
cout<<j<<" ";//Print the prime numbers
}
cout<<endl;
}
return 0;
}
62 changes: 62 additions & 0 deletions Week1/Swarnima_Shishodia/Que12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//Segmented sieve is used when we need to print the prime in a given range because simple sieve is not memory efficient
#include <bits/stdc++.h>
using namespace std;
void simpleSieve(int limit, vector<int> &prime)
{
bool mark[limit+1];
memset(mark, true, sizeof(mark));
for (int p=2; p*p<limit; p++)
{
if (mark[p] == true)
{
for (int i=p*2; i<limit; i=i+p)
mark[i] = false;//Mark the composite number as false
}
}
for (int p=2; p<limit; p++)
{
if (mark[p] == true)
{
prime.push_back(p);
cout << p << " ";
}
}
}

void segmentedSieve(int n)
{
int limit = floor(sqrt(n))+1;
vector<int> prime;
simpleSieve(limit, prime);
int low = limit;
int high = 2*limit;
while (low < n)
{
if (high >= n)
high = n;
bool mark[limit+1];
memset(mark, true, sizeof(mark));
for (int i = 0; i < prime.size(); i++)
{
int loLim = floor(low/prime[i]) * prime[i];
if (loLim < low)
loLim += prime[i];
for (int j=loLim; j<high; j+=prime[i])
mark[j-low] = false;
}
for (int i = low; i<high; i++)
if (mark[i - low] == true)
cout << i << " ";
low = low + limit;
high = high + limit;
}
}

int main()
{
int n;
cout<<"Enter a number\n";
cin>>n;
segmentedSieve(n);
return 0;
}
42 changes: 42 additions & 0 deletions Week1/Swarnima_Shishodia/Que13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//To calculate the power of a number
#include<iostream>
using namespace std;
int power_(int base,int power)
{
int result=1;
while(power!=0)
{
if(power%2==0)
{
base=base*base; //If power is even then take square of the base and divide the power by 2
power=power/2;
}
else
{
result=result*base; //If power is odd then multiply base by result
power=power-1; //and decrease the power by 1
}

}
return result;
}
int main()
{
int base,power,m,y,i,p;
cin>>base;
cin>>power;
cin>>m;
p=power;
if(power%2==0)
power=power/2; //If power is even divide it by 2
else
power=power-1; // If power is odd subtract it by 1

y=power_(base,power);
if(p%2==0)
i=(((y%m)*(y%m))%m);//Using the properties
else
i=(((base%m)*(y%m))%m);
cout<<i;
return 0;
}
36 changes: 36 additions & 0 deletions Week1/Swarnima_Shishodia/Que14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//To check whether n is prime or not
/*Logic- All prime numbers can be represented in the form of 6k+1 and 6k-1 except 2 and 3 and does not get divided by a number which is represented as 6k-1
and 6k+1*/
#include<iostream>
using namespace std;
int main()
{
int t,i,j,n,flag;
cin>>t;
for(i=0;i<t;i++)
{
cin>>n;
if(n==1)
cout<<"No"<<endl;
else if(n==2 || n==3)
cout<<"Yes"<<endl; //To print 2 and 3
else if(n%2==0 && n%3==0)
cout<<"No"<<endl;
else
{
flag=0;
for(j=5;j<=n*n;j=j+6)
{
if(n%j==0 or n%(j+2)==0)
{
flag=1;
break;
}
}
if(flag==0)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
}
40 changes: 40 additions & 0 deletions Week1/Swarnima_Shishodia/Que15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//To print pair if primes
#include<iostream>
using namespace std;
void sieve_of_erasthones(int a[]) //Use sieve to calculate prime number
{
int p=2,i;
for(i=0;i<401;i++) //Mark all array index as 1
a[i]=1;
a[0]=0;
a[1]=0;
while(p*p<=400)
{
for(i=p*p;i<401;i=i+p)
a[i]=0; //The numbers which are not prime will be marked as 0
p=p+1;
}
}
int main()
{
int t,i,j,n,k;
cin>>t;
int a[401];

sieve_of_erasthones(a); //Call the sieve function

for(i=0;i<t;i++)
{
cin>>n;
for(j=2;j<=n;j++)
{
for(k=2;k<=n;k++)
{
if(k*j<=n && a[k]==1 && a[j]==1) //To print the pairs
cout<<j<<" "<<k<<" ";
}
}
cout<<endl;
}
return 0;
}
24 changes: 24 additions & 0 deletions Week1/Swarnima_Shishodia/Que16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//To check whether a number can be represented as power of 2 or not
#include<iostream>
using namespace std;
int main()
{
int t,i,j,a,b;
cin>>t;
uint64_t n;
if(n==0)
cout<<"NO";
else
{
for(i=0;i<t;i++)
{
cin>>n;
if((n&(n-1))==0) //If a number and with number-1 is 0, it means that it can be represented as power of 2
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;

}
}
return 0;
}
19 changes: 19 additions & 0 deletions Week1/Swarnima_Shishodia/Que2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#To print the table of a number

#include<iostream>
using namespace std;
int main()
{
long long int n;
int i;

cout<<"Enter a number ";
cin>>n;

//Print the table
for(i=1;i<=10;i++)
cout<<n<<"*"<<i<<"="<<n*i<<endl;

return 0;
}

22 changes: 22 additions & 0 deletions Week1/Swarnima_Shishodia/Que3a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//To print a upward Star Triangle

#include<iostream>
using namespace std;
int main()
{
int n,i,j;
cout<<"Enter a number ";
cin>>n;

//Run first loop till n and inside that print the number of star for each line which is always equal to the i

for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}

return 0;
}

27 changes: 27 additions & 0 deletions Week1/Swarnima_Shishodia/Que3b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//To print Upward Centered Triangle
#include <iostream>
using namespace std;
int main()
{
int space, rows,i,k;
cout <<"Enter number of rows: ";
cin >> rows;
for(i=1;i<= rows;i++)
{
k=0;
//To print the space in left side
for(space=1;space<=rows-i;space++)
{
cout <<" ";
}

//To print the stars
while(k != i)
{
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}
20 changes: 20 additions & 0 deletions Week1/Swarnima_Shishodia/Que4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//To print Downward Star Triangle

#include<iostream>
using namespace std;
int main()
{
int n,i,j;
cout<<"Enter a number ";
cin>>n;
//Run a loop from n to 1.Inside it run another loop which will print exactly i stars
for(i=n;i>=1;i--)
{
for(j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}

return 0;
}

Loading