From 0557b17a5afb7a51fa80a2db49ee1dddda3e9218 Mon Sep 17 00:00:00 2001 From: Sourabh Subhod Date: Sun, 27 Oct 2019 01:31:39 +0530 Subject: [PATCH 1/2] Added Solution for Problem 3 in C++ --- Problems/Problem_3/Problem_3.cpp | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Problems/Problem_3/Problem_3.cpp diff --git a/Problems/Problem_3/Problem_3.cpp b/Problems/Problem_3/Problem_3.cpp new file mode 100644 index 0000000..ec94725 --- /dev/null +++ b/Problems/Problem_3/Problem_3.cpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +bool isPrime(long long int N){ + //2 is the only even prime + if(N==2){ + return true; + } + //all other even numbers are non-prime + else if(N % 2 == 0){ + return false; + } + //we only need to check if any odd number less + //than the square root of N divides N + for(long long int i=3; i*i <= N; i+=2){ + if(N % i == 0){ + return false; + } + } + return true; +} + +int main(){ + long long int N,i; + cin>>N; + for(i=2; N>0; i++) { + if(isPrime(i)) { + N--; + } + } + cout< Date: Thu, 31 Oct 2019 01:30:45 +0530 Subject: [PATCH 2/2] Updated to remove non-neighbours of 6 --- Problems/Problem_3/Problem_3.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Problems/Problem_3/Problem_3.cpp b/Problems/Problem_3/Problem_3.cpp index ec94725..239f54f 100644 --- a/Problems/Problem_3/Problem_3.cpp +++ b/Problems/Problem_3/Problem_3.cpp @@ -4,11 +4,11 @@ using namespace std; bool isPrime(long long int N){ //2 is the only even prime - if(N==2){ + if(N==2 || N==3){ return true; } - //all other even numbers are non-prime - else if(N % 2 == 0){ + //Non-neighbours of 6 except 2 and 3 are non-primes + else if(N%6!=1 && N%6!=5){ return false; } //we only need to check if any odd number less