From 975b3dc27e0f30b7e476e7eb4cdcf7626e8d796f Mon Sep 17 00:00:00 2001 From: Lavish Saluja Date: Tue, 12 Jun 2018 16:22:21 +0530 Subject: [PATCH 1/3] Added SPOJ --- Problem Solutions/SPOJ/thelastdigit.cpp | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Problem Solutions/SPOJ/thelastdigit.cpp diff --git a/Problem Solutions/SPOJ/thelastdigit.cpp b/Problem Solutions/SPOJ/thelastdigit.cpp new file mode 100644 index 00000000..111f45f9 --- /dev/null +++ b/Problem Solutions/SPOJ/thelastdigit.cpp @@ -0,0 +1,30 @@ +#include +#include + +using namespace std; + +int main() { + int t; + scanf("%d",&t); + + while(t--) + { + int x,y,res=1; + + scanf("%d",&x); + scanf("%d",&y); + + x=x%10; + + while(y>0) + { + if(y%2!=0) + res=(res*x)%10; + + y=y/2; + x=(x*x)%10; + } + + printf("%d\n",res); + } + } \ No newline at end of file From 623c389efc9da0ea97b5d00890a36d16af5a50bd Mon Sep 17 00:00:00 2001 From: Lavish Saluja Date: Tue, 12 Jun 2018 17:22:35 +0530 Subject: [PATCH 2/3] added comments, changed the logic. --- Problem Solutions/SPOJ/thelastdigit.cpp | 46 +++++++++++-------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/Problem Solutions/SPOJ/thelastdigit.cpp b/Problem Solutions/SPOJ/thelastdigit.cpp index 111f45f9..ae794a1b 100644 --- a/Problem Solutions/SPOJ/thelastdigit.cpp +++ b/Problem Solutions/SPOJ/thelastdigit.cpp @@ -1,30 +1,24 @@ #include -#include - using namespace std; - +int mod(int,int);//modular function int main() { - int t; - scanf("%d",&t); - - while(t--) - { - int x,y,res=1; - - scanf("%d",&x); - scanf("%d",&y); - - x=x%10; - - while(y>0) - { - if(y%2!=0) - res=(res*x)%10; - - y=y/2; - x=(x*x)%10; - } - - printf("%d\n",res); + //will be using modular exponentiation (check wikipedia for details over the algo) + int t;cin>>t; + while(t--){ + int a,b;cin>>a>>b;//we need to find last digit of a^b + cout< Date: Wed, 13 Jun 2018 11:39:05 +0530 Subject: [PATCH 3/3] Algo to find all the primes Adding Sieve of Eratosthenes. --- .../FindingAllPrimes/SieveofEratosthenes.cpp | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Algorithms/NumberTheory/FindingAllPrimes/SieveofEratosthenes.cpp diff --git a/Algorithms/NumberTheory/FindingAllPrimes/SieveofEratosthenes.cpp b/Algorithms/NumberTheory/FindingAllPrimes/SieveofEratosthenes.cpp new file mode 100644 index 00000000..2afefb3d --- /dev/null +++ b/Algorithms/NumberTheory/FindingAllPrimes/SieveofEratosthenes.cpp @@ -0,0 +1,33 @@ +// Sieve Of Eratosthenes +// Time Complexity O(nlog(log(n))) whereas following brute force will end you up with O(n*sqrt(n)) +#include +#include +using namespace std; + +int main() { + int n;cin>>n; + int prime[n+1];//prime[i]=1 will imply that i is prime & prime[i]=0 will imply that i is not prime + for(int i=0;i<=n;i++) + prime[i]=1;//considering all as primes as of now. + prime[0]=0; + prime[1]=0;//since 0 & 1 are not prime ofcourse + + /* + Here we will be running are first loop only till sqrt(n) instead of n because we know that + if a number is prime it does not have any factor till its square root so we + can use it for optimization. + */ + for(int i=2;i<=sqrt(n);i++){ + if(prime[i]==1){//if i is prime + for(int j=2;j*i<=n;j++){ + prime[i*j]=0;//setting the multiples of i to 0 because they can't be prime ofcourse + } + } + } + for(int i=0;i<=n;i++){ + if(prime[i]!=0){ + cout<