From 97ae17f1ccab06578486c45d36edafe7e0fc926a Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Sun, 22 Apr 2018 23:27:00 +0100 Subject: [PATCH] goldbachs_conjecture.cpp --- .../training/goldbachs_conjecture.cpp | 79 +++++++++++++++++++ puzzles/interviews/training/permutation.py | 18 +++-- 2 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 puzzles/interviews/training/goldbachs_conjecture.cpp diff --git a/puzzles/interviews/training/goldbachs_conjecture.cpp b/puzzles/interviews/training/goldbachs_conjecture.cpp new file mode 100644 index 0000000..79513df --- /dev/null +++ b/puzzles/interviews/training/goldbachs_conjecture.cpp @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include + +using namespace std; + +static std::vector GetPrimes(const size_t n) { + std::vector primes; + std::vector lp(n + 1, 0); + +for (auto i = 2u; i <= n; ++i) { + if (lp[i] == 0) { + lp[i] = i; + primes.push_back(i); + } + for (auto j = 0u; j < primes.size() && primes[j] <= lp[i] && i * primes[j] <= n; ++j) + lp[i * primes[j]] = primes[j]; +} +return primes; + +} + +inline bool is_prime( int n, const vector& primes ){ + int sqrt_n = floor(sqrt(double(n))); + for ( int i = 0; i < primes.size() && primes[i] <= sqrt_n; ++i ){ + if ( n % primes[i] == 0 ) { + return false; + } + } + return true; +} + +vector primesum(int A) { + + vector primes; + +#if 0 + for ( int i = 2; i < A; ++i){ + if ( is_prime(i,primes) ){ + primes.push_back(i); + } + } +#endif + + primes = GetPrimes(A); + +#if 1 + unordered_set primes_set(primes.begin(),primes.end()); + for ( int i = 0; i < primes.size(); ++i ){ + if ( primes_set.find(A-primes[i]) != primes_set.end() ) { + return {primes[i], A-primes[i]}; + } + } +#else + for ( int i = 0; i < primes.size(); ++i ) { + auto it = std::lower_bound(primes.begin(), primes.end(), A - primes[i]); + if ( it != primes.end() && *it == A-primes[i] ) { + return {primes[i], A-primes[i]}; + } + } +#endif + + return {}; +} + +void solve_and_print(int A){ + auto r = primesum(A); + std::cout << r[0] << ' ' << r[1] << std::endl; +} + +int main() +{ + solve_and_print(4); + solve_and_print(10); + + solve_and_print(16777214); +} diff --git a/puzzles/interviews/training/permutation.py b/puzzles/interviews/training/permutation.py index dc6e11d..33b5404 100644 --- a/puzzles/interviews/training/permutation.py +++ b/puzzles/interviews/training/permutation.py @@ -1,7 +1,7 @@ -# -# Prints all permutations. -# def permutation(s, b, e): + """ + Prints all permutations. + """ if b+1 == e: print(s) else: @@ -16,12 +16,13 @@ def permutation(s, b, e): s[b] = s[e-1] s[e-1] = c -# -# Prints all permutations. But in addition doesn't print -# multiple instances of same value. -# e.g. abb bab bba ( doesn't print two abb ) -# + def permutation2(s): + """ + Prints all permutations. But in addition doesn't print + multiple instances of same value. + e.g. abb bab bba ( doesn't print two abb ) + """ print(s) l = len(s) @@ -41,6 +42,7 @@ def permutation2(s): if i == 0: return + if __name__ == "__main__": s = ['a', 'b', 'c', 'd', 'e', 'e'] permutation(s, 0, len(s))